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.g0;
import io.netty.util.internal.shaded.org.jctools.util.Pow2;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        @Override // android.widget.Adapter
        public Object getItem(int i2) {
            return ((d) m0.this.f456g.getChildAt(i2)).b();
        }

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

        @Override // android.widget.Adapter
        public View getView(int i2, View view, ViewGroup viewGroup) {
            if (view == null) {
                return m0.this.c((a.c) getItem(i2), true);
            }
            ((d) view).a((a.c) getItem(i2));
            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 = m0.this.f456g.getChildCount();
            for (int i2 = 0; i2 < childCount; i2++) {
                View childAt = m0.this.f456g.getChildAt(i2);
                childAt.setSelected(childAt == view);
            }
        }
    }

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

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

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

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

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

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

        public d(Context context, a.c cVar, boolean z) {
            super(context, null, c.a.a.actionBarTabStyle);
            int[] iArr = {R.attr.background};
            this.f465b = iArr;
            this.f466f = cVar;
            t0 t0VarT = t0.t(context, null, iArr, c.a.a.actionBarTabStyle, 0);
            if (t0VarT.q(0)) {
                setBackgroundDrawable(t0VarT.f(0));
            }
            t0VarT.u();
            if (z) {
                setGravity(8388627);
            }
            c();
        }

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

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

        public void c() {
            a.c cVar = this.f466f;
            View viewB = cVar.b();
            if (viewB != null) {
                ViewParent parent = viewB.getParent();
                if (parent != this) {
                    if (parent != null) {
                        ((ViewGroup) parent).removeView(viewB);
                    }
                    addView(viewB);
                }
                this.f469i = viewB;
                TextView textView = this.f467g;
                if (textView != null) {
                    textView.setVisibility(8);
                }
                ImageView imageView = this.f468h;
                if (imageView != null) {
                    imageView.setVisibility(8);
                    this.f468h.setImageDrawable(null);
                    return;
                }
                return;
            }
            View view = this.f469i;
            if (view != null) {
                removeView(view);
                this.f469i = null;
            }
            Drawable drawableC = cVar.c();
            CharSequence charSequenceD = cVar.d();
            if (drawableC != null) {
                if (this.f468h == null) {
                    o oVar = new o(getContext());
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
                    layoutParams.gravity = 16;
                    oVar.setLayoutParams(layoutParams);
                    addView(oVar, 0);
                    this.f468h = oVar;
                }
                this.f468h.setImageDrawable(drawableC);
                this.f468h.setVisibility(0);
            } else {
                ImageView imageView2 = this.f468h;
                if (imageView2 != null) {
                    imageView2.setVisibility(8);
                    this.f468h.setImageDrawable(null);
                }
            }
            boolean z = !TextUtils.isEmpty(charSequenceD);
            if (z) {
                if (this.f467g == null) {
                    y yVar = new y(getContext(), null, c.a.a.actionBarTabTextStyle);
                    yVar.setEllipsize(TextUtils.TruncateAt.END);
                    LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(-2, -2);
                    layoutParams2.gravity = 16;
                    yVar.setLayoutParams(layoutParams2);
                    addView(yVar);
                    this.f467g = yVar;
                }
                this.f467g.setText(charSequenceD);
                this.f467g.setVisibility(0);
            } else {
                TextView textView2 = this.f467g;
                if (textView2 != null) {
                    textView2.setVisibility(8);
                    this.f467g.setText((CharSequence) null);
                }
            }
            ImageView imageView3 = this.f468h;
            if (imageView3 != null) {
                imageView3.setContentDescription(cVar.a());
            }
            v0.a(this, z ? null : cVar.a());
        }

        @Override // android.view.View
        public void onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
            super.onInitializeAccessibilityEvent(accessibilityEvent);
            accessibilityEvent.setClassName(a.c.class.getName());
        }

        @Override // android.view.View
        public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo accessibilityNodeInfo) {
            super.onInitializeAccessibilityNodeInfo(accessibilityNodeInfo);
            accessibilityNodeInfo.setClassName(a.c.class.getName());
        }

        @Override // android.widget.LinearLayout, android.view.View
        public void onMeasure(int i2, int i3) {
            super.onMeasure(i2, i3);
            if (m0.this.f459j > 0) {
                int measuredWidth = getMeasuredWidth();
                int i4 = m0.this.f459j;
                if (measuredWidth > i4) {
                    super.onMeasure(View.MeasureSpec.makeMeasureSpec(i4, Pow2.MAX_POW2), i3);
                }
            }
        }

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

    static {
        new DecelerateInterpolator();
    }

    private Spinner b() {
        w wVar = new w(getContext(), null, c.a.a.actionDropDownStyle);
        wVar.setLayoutParams(new g0.a(-2, -1));
        wVar.setOnItemSelectedListener(this);
        return wVar;
    }

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

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

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

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

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

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

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

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

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

    @Override // android.widget.HorizontalScrollView, android.widget.FrameLayout, android.view.View
    public void onMeasure(int i2, int i3) {
        int mode = View.MeasureSpec.getMode(i2);
        boolean z = mode == 1073741824;
        setFillViewport(z);
        int childCount = this.f456g.getChildCount();
        if (childCount <= 1 || !(mode == 1073741824 || mode == Integer.MIN_VALUE)) {
            this.f459j = -1;
        } else {
            if (childCount > 2) {
                this.f459j = (int) (View.MeasureSpec.getSize(i2) * 0.4f);
            } else {
                this.f459j = View.MeasureSpec.getSize(i2) / 2;
            }
            this.f459j = Math.min(this.f459j, this.f460k);
        }
        int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(this.l, Pow2.MAX_POW2);
        if (!z && this.f458i) {
            this.f456g.measure(0, iMakeMeasureSpec);
            if (this.f456g.getMeasuredWidth() > View.MeasureSpec.getSize(i2)) {
                e();
            } else {
                f();
            }
        } else {
            f();
        }
        int measuredWidth = getMeasuredWidth();
        super.onMeasure(i2, iMakeMeasureSpec);
        int measuredWidth2 = getMeasuredWidth();
        if (!z || measuredWidth == measuredWidth2) {
            return;
        }
        setTabSelected(this.m);
    }

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

    public void setAllowCollapse(boolean z) {
        this.f458i = z;
    }

    public void setContentHeight(int i2) {
        this.l = i2;
        requestLayout();
    }

    public void setTabSelected(int i2) {
        this.m = i2;
        int childCount = this.f456g.getChildCount();
        int i3 = 0;
        while (i3 < childCount) {
            View childAt = this.f456g.getChildAt(i3);
            boolean z = i3 == i2;
            childAt.setSelected(z);
            if (z) {
                a(i2);
            }
            i3++;
        }
        Spinner spinner = this.f457h;
        if (spinner == null || i2 < 0) {
            return;
        }
        spinner.setSelection(i2);
    }
}
