package android.support.design.widget;

import android.animation.Animator.AnimatorListener;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.support.v4.f.k$a;
import android.support.v4.f.k$b;
import android.support.v4.f.k.c;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.a;
import android.support.v4.view.p;
import android.support.v4.view.s;
import android.support.v4.widget.j;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import com.google.android.gms.common.util.CrashUtils.ErrorDialogData;
import java.util.ArrayList;
import java.util.Iterator;

@a
public class TabLayout extends HorizontalScrollView {
    private static final k$a<e> rR = new c(16);
    int mMode;
    private final ArrayList<e> rS;
    private e rT;
    private final d rU;
    int rV;
    int rW;
    int rX;
    int rY;
    int rZ;
    ColorStateList sa;
    float sc;
    float sd;
    final int se;
    int sf;
    private final int sg;
    private final int si;
    private final int sj;
    private int sk;
    int sl;
    private b sm;
    private final ArrayList<b> sn;
    private b so;
    private ValueAnimator sp;
    ViewPager sq;
    private p sr;
    private DataSetObserver ss;
    private f st;
    private a su;
    private boolean sv;
    private final k$a<g> sx;

    public TabLayout(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public TabLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.rS = new ArrayList();
        this.sf = Integer.MAX_VALUE;
        this.sn = new ArrayList();
        this.sx = new k$b(12);
        o.G(context);
        setHorizontalScrollBarEnabled(false);
        this.rU = new d(this, context);
        super.addView(this.rU, 0, new LayoutParams(-2, -1));
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, android.support.design.a.a.TabLayout, i, 2131492869);
        this.rU.ai(obtainStyledAttributes.getDimensionPixelSize(1, 0));
        this.rU.ah(obtainStyledAttributes.getColor(0, 0));
        int dimensionPixelSize = obtainStyledAttributes.getDimensionPixelSize(15, 0);
        this.rY = dimensionPixelSize;
        this.rX = dimensionPixelSize;
        this.rW = dimensionPixelSize;
        this.rV = dimensionPixelSize;
        this.rV = obtainStyledAttributes.getDimensionPixelSize(11, this.rV);
        this.rW = obtainStyledAttributes.getDimensionPixelSize(12, this.rW);
        this.rX = obtainStyledAttributes.getDimensionPixelSize(13, this.rX);
        this.rY = obtainStyledAttributes.getDimensionPixelSize(14, this.rY);
        this.rZ = obtainStyledAttributes.getResourceId(8, 2131493646);
        TypedArray obtainStyledAttributes2 = context.obtainStyledAttributes(this.rZ, android.support.v7.a.a.a.TextAppearance);
        try {
            this.sc = (float) obtainStyledAttributes2.getDimensionPixelSize(0, 0);
            this.sa = obtainStyledAttributes2.getColorStateList(3);
            if (obtainStyledAttributes.hasValue(9)) {
                this.sa = obtainStyledAttributes.getColorStateList(9);
            }
            if (obtainStyledAttributes.hasValue(10)) {
                dimensionPixelSize = obtainStyledAttributes.getColor(10, 0);
                int defaultColor = this.sa.getDefaultColor();
                r3 = new int[2][];
                int[] iArr = new int[]{SELECTED_STATE_SET, dimensionPixelSize};
                r3[1] = EMPTY_STATE_SET;
                iArr[1] = defaultColor;
                this.sa = new ColorStateList(r3, iArr);
            }
            this.sg = obtainStyledAttributes.getDimensionPixelSize(6, -1);
            this.si = obtainStyledAttributes.getDimensionPixelSize(7, -1);
            this.se = obtainStyledAttributes.getResourceId(3, 0);
            this.sk = obtainStyledAttributes.getDimensionPixelSize(2, 0);
            this.mMode = obtainStyledAttributes.getInt(4, 1);
            this.sl = obtainStyledAttributes.getInt(5, 0);
            obtainStyledAttributes.recycle();
            Resources resources = getResources();
            this.sd = (float) resources.getDimensionPixelSize(2131428299);
            this.sj = resources.getDimensionPixelSize(2131427394);
            cs();
        } finally {
            obtainStyledAttributes2.recycle();
        }
    }

    public void setSelectedTabIndicatorColor(int i) {
        this.rU.ah(i);
    }

    public void setSelectedTabIndicatorHeight(int i) {
        this.rU.ai(i);
    }

    private void setScrollPosition$4867b5c2(int i) {
        a(i, 0.0f, true, true);
    }

    /* Access modifiers changed, original: final */
    public final void a(int i, float f, boolean z, boolean z2) {
        int round = Math.round(((float) i) + f);
        if (round >= 0 && round < this.rU.getChildCount()) {
            if (z2) {
                this.rU.c(i, f);
            }
            if (this.sp != null && this.sp.isRunning()) {
                this.sp.cancel();
            }
            scrollTo(b(i, f), 0);
            if (z) {
                setSelectedTabView(round);
            }
        }
    }

    private float getScrollPosition() {
        d dVar = this.rU;
        return dVar.sD + ((float) dVar.sC);
    }

    public final void a(e eVar, boolean z) {
        int size = this.rS.size();
        if (eVar.sR != this) {
            throw new IllegalArgumentException("Tab belongs to a different TabLayout.");
        }
        a(eVar, size);
        g gVar = eVar.sS;
        d dVar = this.rU;
        int i = eVar.mPosition;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -1);
        a(layoutParams);
        dVar.addView(gVar, i, layoutParams);
        if (z) {
            eVar.select();
        }
    }

    private void a(TabItem tabItem) {
        e cp = cp();
        if (tabItem.mText != null) {
            cp.b(tabItem.mText);
        }
        if (tabItem.rP != null) {
            cp.rP = tabItem.rP;
            cp.cu();
        }
        if (tabItem.rQ != 0) {
            cp.aj(tabItem.rQ);
        }
        if (!TextUtils.isEmpty(tabItem.getContentDescription())) {
            cp.sP = tabItem.getContentDescription();
            cp.cu();
        }
        a(cp, this.rS.isEmpty());
    }

    @Deprecated
    public void setOnTabSelectedListener(b bVar) {
        if (this.sm != null) {
            b(this.sm);
        }
        this.sm = bVar;
        if (bVar != null) {
            a(bVar);
        }
    }

    public final void a(b bVar) {
        if (!this.sn.contains(bVar)) {
            this.sn.add(bVar);
        }
    }

    private void b(b bVar) {
        this.sn.remove(bVar);
    }

    public final e cp() {
        e eVar = (e) rR.aA();
        if (eVar == null) {
            eVar = new e();
        }
        eVar.sR = this;
        eVar.sS = a(eVar);
        return eVar;
    }

    public int getTabCount() {
        return this.rS.size();
    }

    public final e ac(int i) {
        return (i < 0 || i >= getTabCount()) ? null : (e) this.rS.get(i);
    }

    public int getSelectedTabPosition() {
        if (this.rT != null) {
            return this.rT.mPosition;
        }
        return -1;
    }

    private void removeAllTabs() {
        for (int childCount = this.rU.getChildCount() - 1; childCount >= 0; childCount--) {
            g gVar = (g) this.rU.getChildAt(childCount);
            this.rU.removeViewAt(childCount);
            if (gVar != null) {
                gVar.i(null);
                gVar.setSelected(false);
                this.sx.release(gVar);
            }
            requestLayout();
        }
        Iterator it = this.rS.iterator();
        while (it.hasNext()) {
            e eVar = (e) it.next();
            it.remove();
            eVar.sR = null;
            eVar.sS = null;
            eVar.sO = null;
            eVar.rP = null;
            eVar.mText = null;
            eVar.sP = null;
            eVar.mPosition = -1;
            eVar.sQ = null;
            rR.release(eVar);
        }
        this.rT = null;
    }

    public void setTabMode(int i) {
        if (i != this.mMode) {
            this.mMode = i;
            cs();
        }
    }

    public int getTabMode() {
        return this.mMode;
    }

    public void setTabGravity(int i) {
        if (this.sl != i) {
            this.sl = i;
            cs();
        }
    }

    public int getTabGravity() {
        return this.sl;
    }

    public void setTabTextColors(ColorStateList colorStateList) {
        if (this.sa != colorStateList) {
            this.sa = colorStateList;
            int size = this.rS.size();
            for (int i = 0; i < size; i++) {
                ((e) this.rS.get(i)).cu();
            }
        }
    }

    public ColorStateList getTabTextColors() {
        return this.sa;
    }

    private void a(ViewPager viewPager, boolean z) {
        if (this.sq != null) {
            if (this.st != null) {
                this.sq.removeOnPageChangeListener(this.st);
            }
            if (this.su != null) {
                this.sq.removeOnAdapterChangeListener(this.su);
            }
        }
        if (this.so != null) {
            b(this.so);
            this.so = null;
        }
        if (viewPager != null) {
            this.sq = viewPager;
            if (this.st == null) {
                this.st = new f(this);
            }
            this.st.reset();
            viewPager.addOnPageChangeListener(this.st);
            this.so = new h(viewPager);
            a(this.so);
            p adapter = viewPager.getAdapter();
            if (adapter != null) {
                a(adapter, true);
            }
            if (this.su == null) {
                this.su = new a(this);
            }
            this.su.sz = true;
            viewPager.addOnAdapterChangeListener(this.su);
            setScrollPosition$4867b5c2(viewPager.getCurrentItem());
        } else {
            this.sq = null;
            a(null, false);
        }
        this.sv = z;
    }

    @Deprecated
    public void setTabsFromPagerAdapter(p pVar) {
        a(pVar, false);
    }

    public boolean shouldDelayChildPressedState() {
        return getTabScrollRange() > 0;
    }

    /* Access modifiers changed, original: protected */
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (this.sq == null) {
            ViewParent parent = getParent();
            if (parent instanceof ViewPager) {
                a((ViewPager) parent, true);
            }
        }
    }

    /* Access modifiers changed, original: protected */
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (this.sv) {
            setupWithViewPager(null);
            this.sv = false;
        }
    }

    private int getTabScrollRange() {
        return Math.max(0, ((this.rU.getWidth() - getWidth()) - getPaddingLeft()) - getPaddingRight());
    }

    /* Access modifiers changed, original: final */
    public final void a(p pVar, boolean z) {
        if (!(this.sr == null || this.ss == null)) {
            this.sr.unregisterDataSetObserver(this.ss);
        }
        this.sr = pVar;
        if (z && pVar != null) {
            if (this.ss == null) {
                this.ss = new c(this);
            }
            pVar.registerDataSetObserver(this.ss);
        }
        cq();
    }

    /* Access modifiers changed, original: final */
    public final void cq() {
        removeAllTabs();
        if (this.sr != null) {
            int i;
            int count = this.sr.getCount();
            for (i = 0; i < count; i++) {
                a(cp().b(this.sr.getPageTitle(i)), false);
            }
            if (this.sq != null && count > 0) {
                i = this.sq.getCurrentItem();
                if (i != getSelectedTabPosition() && i < getTabCount()) {
                    b(ac(i), true);
                }
            }
        }
    }

    private g a(e eVar) {
        g gVar = this.sx != null ? (g) this.sx.aA() : null;
        if (gVar == null) {
            gVar = new g(this, getContext());
        }
        gVar.i(eVar);
        gVar.setFocusable(true);
        gVar.setMinimumWidth(getTabMinWidth());
        return gVar;
    }

    public void addView(View view) {
        t(view);
    }

    public void addView(View view, int i) {
        t(view);
    }

    public void addView(View view, ViewGroup.LayoutParams layoutParams) {
        t(view);
    }

    public void addView(View view, int i, ViewGroup.LayoutParams layoutParams) {
        t(view);
    }

    private void t(View view) {
        if (view instanceof TabItem) {
            a((TabItem) view);
            return;
        }
        throw new IllegalArgumentException("Only TabItem instances can be added to TabLayout");
    }

    private void a(LinearLayout.LayoutParams layoutParams) {
        if (this.mMode == 1 && this.sl == 0) {
            layoutParams.width = 0;
            layoutParams.weight = 1.0f;
            return;
        }
        layoutParams.width = -2;
        layoutParams.weight = 0.0f;
    }

    /* Access modifiers changed, original: final */
    public final int ad(int i) {
        return Math.round(getResources().getDisplayMetrics().density * ((float) i));
    }

    /* Access modifiers changed, original: protected */
    public void onMeasure(int i, int i2) {
        int i3 = 1;
        int ad = (ad(getDefaultHeight()) + getPaddingTop()) + getPaddingBottom();
        switch (MeasureSpec.getMode(i2)) {
            case j.INVALID_ID /*-2147483648*/:
                i2 = MeasureSpec.makeMeasureSpec(Math.min(ad, MeasureSpec.getSize(i2)), ErrorDialogData.SUPPRESSED);
                break;
            case 0:
                i2 = MeasureSpec.makeMeasureSpec(ad, ErrorDialogData.SUPPRESSED);
                break;
        }
        ad = MeasureSpec.getSize(i);
        if (MeasureSpec.getMode(i) != 0) {
            if (this.si > 0) {
                ad = this.si;
            } else {
                ad -= ad(56);
            }
            this.sf = ad;
        }
        super.onMeasure(i, i2);
        if (getChildCount() == 1) {
            View childAt = getChildAt(0);
            switch (this.mMode) {
                case 0:
                    if (childAt.getMeasuredWidth() >= getMeasuredWidth()) {
                        ad = 0;
                        break;
                    } else {
                        ad = 1;
                        break;
                    }
                case 1:
                    if (childAt.getMeasuredWidth() == getMeasuredWidth()) {
                        i3 = 0;
                    }
                    ad = i3;
                    break;
                default:
                    ad = 0;
                    break;
            }
            if (ad != 0) {
                childAt.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), ErrorDialogData.SUPPRESSED), getChildMeasureSpec(i2, getPaddingTop() + getPaddingBottom(), childAt.getLayoutParams().height));
            }
        }
    }

    private void ae(int i) {
        if (i != -1) {
            if (getWindowToken() != null && s.as(this)) {
                int i2;
                d dVar = this.rU;
                int childCount = dVar.getChildCount();
                for (i2 = 0; i2 < childCount; i2++) {
                    if (dVar.getChildAt(i2).getWidth() <= 0) {
                        i2 = 1;
                        break;
                    }
                }
                i2 = 0;
                if (i2 == 0) {
                    if (getScrollX() != b(i, 0.0f)) {
                        cr();
                        this.sp.setIntValues(new int[]{i2, r3});
                        this.sp.start();
                    }
                    this.rU.o(i, 300);
                    return;
                }
            }
            setScrollPosition$4867b5c2(i);
        }
    }

    private void cr() {
        if (this.sp == null) {
            this.sp = new ValueAnimator();
            this.sp.setInterpolator(a.mg);
            this.sp.setDuration(300);
            this.sp.addUpdateListener(new 1(this));
        }
    }

    /* Access modifiers changed, original: 0000 */
    public void setScrollAnimatorListener(AnimatorListener animatorListener) {
        cr();
        this.sp.addListener(animatorListener);
    }

    private void setSelectedTabView(int i) {
        int childCount = this.rU.getChildCount();
        if (i < childCount) {
            for (int i2 = 0; i2 < childCount; i2++) {
                boolean z;
                View childAt = this.rU.getChildAt(i2);
                if (i2 == i) {
                    z = true;
                } else {
                    z = false;
                }
                childAt.setSelected(z);
            }
        }
    }

    /* Access modifiers changed, original: final */
    public final void b(e eVar) {
        b(eVar, true);
    }

    /* Access modifiers changed, original: final */
    public final void b(e eVar, boolean z) {
        e eVar2 = this.rT;
        if (eVar2 != eVar) {
            int i;
            if (eVar != null) {
                i = eVar.mPosition;
            } else {
                i = -1;
            }
            if (z) {
                if ((eVar2 == null || eVar2.mPosition == -1) && i != -1) {
                    setScrollPosition$4867b5c2(i);
                } else {
                    ae(i);
                }
                if (i != -1) {
                    setSelectedTabView(i);
                }
            }
            if (eVar2 != null) {
                d(eVar2);
            }
            this.rT = eVar;
            if (eVar != null) {
                c(eVar);
            }
        } else if (eVar2 != null) {
            e(eVar);
            ae(eVar.mPosition);
        }
    }

    private void c(e eVar) {
        for (int size = this.sn.size() - 1; size >= 0; size--) {
            ((b) this.sn.get(size)).f(eVar);
        }
    }

    private void d(e eVar) {
        for (int size = this.sn.size() - 1; size >= 0; size--) {
            ((b) this.sn.get(size)).g(eVar);
        }
    }

    private void e(e eVar) {
        for (int size = this.sn.size() - 1; size >= 0; size--) {
            ((b) this.sn.get(size)).h(eVar);
        }
    }

    private int b(int i, float f) {
        int i2 = 0;
        if (this.mMode != 0) {
            return 0;
        }
        int width;
        View childAt = this.rU.getChildAt(i);
        View childAt2 = i + 1 < this.rU.getChildCount() ? this.rU.getChildAt(i + 1) : null;
        if (childAt != null) {
            width = childAt.getWidth();
        } else {
            width = 0;
        }
        if (childAt2 != null) {
            i2 = childAt2.getWidth();
        }
        int left = (childAt.getLeft() + (width / 2)) - (getWidth() / 2);
        i2 = (int) ((((float) (i2 + width)) * 0.5f) * f);
        return s.T(this) == 0 ? i2 + left : left - i2;
    }

    private void cs() {
        int max;
        if (this.mMode == 0) {
            max = Math.max(0, this.sk - this.rV);
        } else {
            max = 0;
        }
        s.d(this.rU, max, 0, 0, 0);
        switch (this.mMode) {
            case 0:
                this.rU.setGravity(8388611);
                break;
            case 1:
                this.rU.setGravity(1);
                break;
        }
        r(true);
    }

    /* Access modifiers changed, original: final */
    public final void r(boolean z) {
        int i = 0;
        while (true) {
            int i2 = i;
            if (i2 < this.rU.getChildCount()) {
                View childAt = this.rU.getChildAt(i2);
                childAt.setMinimumWidth(getTabMinWidth());
                a((LinearLayout.LayoutParams) childAt.getLayoutParams());
                if (z) {
                    childAt.requestLayout();
                }
                i = i2 + 1;
            } else {
                return;
            }
        }
    }

    private int getDefaultHeight() {
        Object obj;
        int size = this.rS.size();
        for (int i = 0; i < size; i++) {
            e eVar = (e) this.rS.get(i);
            if (eVar != null && eVar.rP != null && !TextUtils.isEmpty(eVar.mText)) {
                obj = 1;
                break;
            }
        }
        obj = null;
        if (obj != null) {
            return 72;
        }
        return 48;
    }

    private int getTabMinWidth() {
        if (this.sg != -1) {
            return this.sg;
        }
        return this.mMode == 0 ? this.sj : 0;
    }

    public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
        return generateDefaultLayoutParams();
    }

    /* Access modifiers changed, original: 0000 */
    public int getTabMaxWidth() {
        return this.sf;
    }

    public void setupWithViewPager(ViewPager viewPager) {
        a(viewPager, false);
    }

    private void a(e eVar, int i) {
        eVar.mPosition = i;
        this.rS.add(i, eVar);
        int size = this.rS.size();
        for (int i2 = i + 1; i2 < size; i2++) {
            ((e) this.rS.get(i2)).mPosition = i2;
        }
    }
}
