package androidx.appcompat.widget;

import android.R;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import androidx.appcompat.app.a;
import androidx.appcompat.widget.m0;

/* JADX INFO: compiled from: ScrollingTabContainerView.java */
/* JADX INFO: loaded from: classes.dex */
public class t0 extends HorizontalScrollView implements AdapterView.OnItemSelectedListener {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    Runnable f1084e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private c f1085f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    m0 f1086g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private Spinner f1087h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private boolean f1088i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    int f1089j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    int f1090k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private int f1091l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private int f1092m;

    /* JADX INFO: compiled from: ScrollingTabContainerView.java */
    class a implements Runnable {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        final /* synthetic */ View f1093e;

        a(View view) {
            this.f1093e = view;
        }

        @Override // java.lang.Runnable
        public void run() {
            t0.this.smoothScrollTo(this.f1093e.getLeft() - ((t0.this.getWidth() - this.f1093e.getWidth()) / 2), 0);
            t0.this.f1084e = null;
        }
    }

    /* JADX INFO: compiled from: ScrollingTabContainerView.java */
    private class b extends BaseAdapter {
        b() {
        }

        @Override // android.widget.Adapter
        public int getCount() {
            return t0.this.f1086g.getChildCount();
        }

        @Override // android.widget.Adapter
        public Object getItem(int i3) {
            return ((d) t0.this.f1086g.getChildAt(i3)).b();
        }

        @Override // android.widget.Adapter
        public long getItemId(int i3) {
            return i3;
        }

        @Override // android.widget.Adapter
        public View getView(int i3, View view, ViewGroup viewGroup) {
            if (view == null) {
                return t0.this.c((a.c) getItem(i3), true);
            }
            ((d) view).a((a.c) getItem(i3));
            return view;
        }
    }

    /* JADX INFO: compiled from: ScrollingTabContainerView.java */
    private class c implements View.OnClickListener {
        c() {
        }

        @Override // android.view.View.OnClickListener
        public void onClick(View view) {
            ((d) view).b().e();
            int childCount = t0.this.f1086g.getChildCount();
            for (int i3 = 0; i3 < childCount; i3++) {
                View childAt = t0.this.f1086g.getChildAt(i3);
                childAt.setSelected(childAt == view);
            }
        }
    }

    /* JADX INFO: compiled from: ScrollingTabContainerView.java */
    private class d extends LinearLayout {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private final int[] f1097e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private a.c f1098f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private TextView f1099g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        private ImageView f1100h;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        private View f1101i;

        /* JADX WARN: Illegal instructions before constructor call */
        public d(Context context, a.c cVar, boolean z2) {
            int i3 = c.a.f3153d;
            super(context, null, i3);
            int[] iArr = {R.attr.background};
            this.f1097e = iArr;
            this.f1098f = cVar;
            a1 a1VarV = a1.v(context, null, iArr, i3, 0);
            if (a1VarV.s(0)) {
                setBackgroundDrawable(a1VarV.g(0));
            }
            a1VarV.w();
            if (z2) {
                setGravity(8388627);
            }
            c();
        }

        public void a(a.c cVar) {
            this.f1098f = cVar;
            c();
        }

        public a.c b() {
            return this.f1098f;
        }

        public void c() {
            a.c cVar = this.f1098f;
            View viewB = cVar.b();
            if (viewB != null) {
                ViewParent parent = viewB.getParent();
                if (parent != this) {
                    if (parent != null) {
                        ((ViewGroup) parent).removeView(viewB);
                    }
                    addView(viewB);
                }
                this.f1101i = viewB;
                TextView textView = this.f1099g;
                if (textView != null) {
                    textView.setVisibility(8);
                }
                ImageView imageView = this.f1100h;
                if (imageView != null) {
                    imageView.setVisibility(8);
                    this.f1100h.setImageDrawable(null);
                    return;
                }
                return;
            }
            View view = this.f1101i;
            if (view != null) {
                removeView(view);
                this.f1101i = null;
            }
            Drawable drawableC = cVar.c();
            CharSequence charSequenceD = cVar.d();
            if (drawableC != null) {
                if (this.f1100h == null) {
                    r rVar = new r(getContext());
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
                    layoutParams.gravity = 16;
                    rVar.setLayoutParams(layoutParams);
                    addView(rVar, 0);
                    this.f1100h = rVar;
                }
                this.f1100h.setImageDrawable(drawableC);
                this.f1100h.setVisibility(0);
            } else {
                ImageView imageView2 = this.f1100h;
                if (imageView2 != null) {
                    imageView2.setVisibility(8);
                    this.f1100h.setImageDrawable(null);
                }
            }
            boolean z2 = !TextUtils.isEmpty(charSequenceD);
            if (z2) {
                if (this.f1099g == null) {
                    d0 d0Var = new d0(getContext(), null, c.a.f3154e);
                    d0Var.setEllipsize(TextUtils.TruncateAt.END);
                    LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(-2, -2);
                    layoutParams2.gravity = 16;
                    d0Var.setLayoutParams(layoutParams2);
                    addView(d0Var);
                    this.f1099g = d0Var;
                }
                this.f1099g.setText(charSequenceD);
                this.f1099g.setVisibility(0);
            } else {
                TextView textView2 = this.f1099g;
                if (textView2 != null) {
                    textView2.setVisibility(8);
                    this.f1099g.setText((CharSequence) null);
                }
            }
            ImageView imageView3 = this.f1100h;
            if (imageView3 != null) {
                imageView3.setContentDescription(cVar.a());
            }
            d1.a(this, z2 ? null : cVar.a());
        }

        @Override // android.view.View
        public void onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
            super.onInitializeAccessibilityEvent(accessibilityEvent);
            accessibilityEvent.setClassName("androidx.appcompat.app.ActionBar$Tab");
        }

        @Override // android.view.View
        public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo accessibilityNodeInfo) {
            super.onInitializeAccessibilityNodeInfo(accessibilityNodeInfo);
            accessibilityNodeInfo.setClassName("androidx.appcompat.app.ActionBar$Tab");
        }

        @Override // android.widget.LinearLayout, android.view.View
        public void onMeasure(int i3, int i4) {
            super.onMeasure(i3, i4);
            if (t0.this.f1089j > 0) {
                int measuredWidth = getMeasuredWidth();
                int i5 = t0.this.f1089j;
                if (measuredWidth > i5) {
                    super.onMeasure(View.MeasureSpec.makeMeasureSpec(i5, 1073741824), i4);
                }
            }
        }

        @Override // android.view.View
        public void setSelected(boolean z2) {
            boolean z3 = isSelected() != z2;
            super.setSelected(z2);
            if (z3 && z2) {
                sendAccessibilityEvent(4);
            }
        }
    }

    static {
        new DecelerateInterpolator();
    }

    private Spinner b() {
        a0 a0Var = new a0(getContext(), null, c.a.f3157h);
        a0Var.setLayoutParams(new m0.a(-2, -1));
        a0Var.setOnItemSelectedListener(this);
        return a0Var;
    }

    private boolean d() {
        Spinner spinner = this.f1087h;
        return spinner != null && spinner.getParent() == this;
    }

    private void e() {
        if (d()) {
            return;
        }
        if (this.f1087h == null) {
            this.f1087h = b();
        }
        removeView(this.f1086g);
        addView(this.f1087h, new ViewGroup.LayoutParams(-2, -1));
        if (this.f1087h.getAdapter() == null) {
            this.f1087h.setAdapter((SpinnerAdapter) new b());
        }
        Runnable runnable = this.f1084e;
        if (runnable != null) {
            removeCallbacks(runnable);
            this.f1084e = null;
        }
        this.f1087h.setSelection(this.f1092m);
    }

    private boolean f() {
        if (!d()) {
            return false;
        }
        removeView(this.f1087h);
        addView(this.f1086g, new ViewGroup.LayoutParams(-2, -1));
        setTabSelected(this.f1087h.getSelectedItemPosition());
        return false;
    }

    public void a(int i3) {
        View childAt = this.f1086g.getChildAt(i3);
        Runnable runnable = this.f1084e;
        if (runnable != null) {
            removeCallbacks(runnable);
        }
        a aVar = new a(childAt);
        this.f1084e = aVar;
        post(aVar);
    }

    d c(a.c cVar, boolean z2) {
        d dVar = new d(getContext(), cVar, z2);
        if (z2) {
            dVar.setBackgroundDrawable(null);
            dVar.setLayoutParams(new AbsListView.LayoutParams(-1, this.f1091l));
        } else {
            dVar.setFocusable(true);
            if (this.f1085f == null) {
                this.f1085f = new c();
            }
            dVar.setOnClickListener(this.f1085f);
        }
        return dVar;
    }

    @Override // android.view.ViewGroup, android.view.View
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Runnable runnable = this.f1084e;
        if (runnable != null) {
            post(runnable);
        }
    }

    @Override // android.view.View
    protected void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        androidx.appcompat.view.a aVarB = androidx.appcompat.view.a.b(getContext());
        setContentHeight(aVarB.f());
        this.f1090k = aVarB.e();
    }

    @Override // android.view.ViewGroup, android.view.View
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Runnable runnable = this.f1084e;
        if (runnable != null) {
            removeCallbacks(runnable);
        }
    }

    @Override // android.widget.AdapterView.OnItemSelectedListener
    public void onItemSelected(AdapterView<?> adapterView, View view, int i3, long j3) {
        ((d) view).b().e();
    }

    @Override // android.widget.HorizontalScrollView, android.widget.FrameLayout, android.view.View
    public void onMeasure(int i3, int i4) {
        int mode = View.MeasureSpec.getMode(i3);
        boolean z2 = mode == 1073741824;
        setFillViewport(z2);
        int childCount = this.f1086g.getChildCount();
        if (childCount <= 1 || !(mode == 1073741824 || mode == Integer.MIN_VALUE)) {
            this.f1089j = -1;
        } else {
            if (childCount > 2) {
                this.f1089j = (int) (View.MeasureSpec.getSize(i3) * 0.4f);
            } else {
                this.f1089j = View.MeasureSpec.getSize(i3) / 2;
            }
            this.f1089j = Math.min(this.f1089j, this.f1090k);
        }
        int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(this.f1091l, 1073741824);
        if (!z2 && this.f1088i) {
            this.f1086g.measure(0, iMakeMeasureSpec);
            if (this.f1086g.getMeasuredWidth() > View.MeasureSpec.getSize(i3)) {
                e();
            } else {
                f();
            }
        } else {
            f();
        }
        int measuredWidth = getMeasuredWidth();
        super.onMeasure(i3, iMakeMeasureSpec);
        int measuredWidth2 = getMeasuredWidth();
        if (!z2 || measuredWidth == measuredWidth2) {
            return;
        }
        setTabSelected(this.f1092m);
    }

    @Override // android.widget.AdapterView.OnItemSelectedListener
    public void onNothingSelected(AdapterView<?> adapterView) {
    }

    public void setAllowCollapse(boolean z2) {
        this.f1088i = z2;
    }

    public void setContentHeight(int i3) {
        this.f1091l = i3;
        requestLayout();
    }

    public void setTabSelected(int i3) {
        this.f1092m = i3;
        int childCount = this.f1086g.getChildCount();
        int i4 = 0;
        while (i4 < childCount) {
            View childAt = this.f1086g.getChildAt(i4);
            boolean z2 = i4 == i3;
            childAt.setSelected(z2);
            if (z2) {
                a(i3);
            }
            i4++;
        }
        Spinner spinner = this.f1087h;
        if (spinner == null || i3 < 0) {
            return;
        }
        spinner.setSelection(i3);
    }
}
