package com.google.android.material.navigation;

import android.R;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityNodeInfo;
import androidx.appcompat.view.menu.g;
import androidx.appcompat.view.menu.k;
import androidx.core.view.s0;
import com.google.android.material.internal.m;
import java.util.HashSet;
import s3.f;
import s3.h;
import t3.h;
import u5.p;
import u5.r;

/* loaded from: classes3.dex */
public abstract class c extends ViewGroup implements k {
    private static final int[] R = {R.attr.state_checked};
    private static final int[] S = {-16842910};
    private final ColorStateList A;
    private int B;
    private int C;
    private Drawable D;
    private int E;
    private final SparseArray F;
    private int G;
    private int H;
    private boolean I;
    private int J;
    private int K;
    private int L;
    private lj.k M;
    private boolean N;
    private ColorStateList O;
    private d P;
    private androidx.appcompat.view.menu.e Q;

    /* renamed from: p, reason: collision with root package name */
    private final r f13412p;

    /* renamed from: q, reason: collision with root package name */
    private final View.OnClickListener f13413q;

    /* renamed from: r, reason: collision with root package name */
    private final f f13414r;

    /* renamed from: s, reason: collision with root package name */
    private final SparseArray f13415s;

    /* renamed from: t, reason: collision with root package name */
    private int f13416t;

    /* renamed from: u, reason: collision with root package name */
    private com.google.android.material.navigation.a[] f13417u;

    /* renamed from: v, reason: collision with root package name */
    private int f13418v;

    /* renamed from: w, reason: collision with root package name */
    private int f13419w;

    /* renamed from: x, reason: collision with root package name */
    private ColorStateList f13420x;

    /* renamed from: y, reason: collision with root package name */
    private int f13421y;

    /* renamed from: z, reason: collision with root package name */
    private ColorStateList f13422z;

    /* loaded from: classes3.dex */
    class a implements View.OnClickListener {
        a() {
        }

        @Override // android.view.View.OnClickListener
        public void onClick(View view) {
            g itemData = ((com.google.android.material.navigation.a) view).getItemData();
            if (c.this.Q.O(itemData, c.this.P, 0)) {
                return;
            }
            itemData.setChecked(true);
        }
    }

    public c(Context context) {
        super(context);
        this.f13414r = new h(5);
        this.f13415s = new SparseArray(5);
        this.f13418v = 0;
        this.f13419w = 0;
        this.F = new SparseArray(5);
        this.G = -1;
        this.H = -1;
        this.N = false;
        this.A = e(R.attr.textColorSecondary);
        if (isInEditMode()) {
            this.f13412p = null;
        } else {
            u5.b bVar = new u5.b();
            this.f13412p = bVar;
            bVar.v0(0);
            bVar.b0(gj.a.d(getContext(), vi.b.G, getResources().getInteger(vi.g.f39831b)));
            bVar.d0(gj.a.e(getContext(), vi.b.H, wi.a.f49478b));
            bVar.m0(new m());
        }
        this.f13413q = new a();
        s0.x0(this, 1);
    }

    private Drawable f() {
        if (this.M == null || this.O == null) {
            return null;
        }
        lj.g gVar = new lj.g(this.M);
        gVar.W(this.O);
        return gVar;
    }

    private com.google.android.material.navigation.a getNewItem() {
        com.google.android.material.navigation.a aVar = (com.google.android.material.navigation.a) this.f13414r.b();
        return aVar == null ? g(getContext()) : aVar;
    }

    private boolean i(int i10) {
        return i10 != -1;
    }

    private void j() {
        HashSet hashSet = new HashSet();
        for (int i10 = 0; i10 < this.Q.size(); i10++) {
            hashSet.add(Integer.valueOf(this.Q.getItem(i10).getItemId()));
        }
        for (int i11 = 0; i11 < this.F.size(); i11++) {
            int keyAt = this.F.keyAt(i11);
            if (!hashSet.contains(Integer.valueOf(keyAt))) {
                this.F.delete(keyAt);
            }
        }
    }

    private void setBadgeIfNeeded(com.google.android.material.navigation.a aVar) {
        xi.a aVar2;
        int id2 = aVar.getId();
        if (i(id2) && (aVar2 = (xi.a) this.F.get(id2)) != null) {
            aVar.setBadge(aVar2);
        }
    }

    @Override // androidx.appcompat.view.menu.k
    public void b(androidx.appcompat.view.menu.e eVar) {
        this.Q = eVar;
    }

    public void d() {
        removeAllViews();
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                if (aVar != null) {
                    this.f13414r.a(aVar);
                    aVar.h();
                }
            }
        }
        if (this.Q.size() == 0) {
            this.f13418v = 0;
            this.f13419w = 0;
            this.f13417u = null;
            return;
        }
        j();
        this.f13417u = new com.google.android.material.navigation.a[this.Q.size()];
        boolean h10 = h(this.f13416t, this.Q.G().size());
        for (int i10 = 0; i10 < this.Q.size(); i10++) {
            this.P.k(true);
            this.Q.getItem(i10).setCheckable(true);
            this.P.k(false);
            com.google.android.material.navigation.a newItem = getNewItem();
            this.f13417u[i10] = newItem;
            newItem.setIconTintList(this.f13420x);
            newItem.setIconSize(this.f13421y);
            newItem.setTextColor(this.A);
            newItem.setTextAppearanceInactive(this.B);
            newItem.setTextAppearanceActive(this.C);
            newItem.setTextColor(this.f13422z);
            int i11 = this.G;
            if (i11 != -1) {
                newItem.setItemPaddingTop(i11);
            }
            int i12 = this.H;
            if (i12 != -1) {
                newItem.setItemPaddingBottom(i12);
            }
            newItem.setActiveIndicatorWidth(this.J);
            newItem.setActiveIndicatorHeight(this.K);
            newItem.setActiveIndicatorMarginHorizontal(this.L);
            newItem.setActiveIndicatorDrawable(f());
            newItem.setActiveIndicatorResizeable(this.N);
            newItem.setActiveIndicatorEnabled(this.I);
            Drawable drawable = this.D;
            if (drawable != null) {
                newItem.setItemBackground(drawable);
            } else {
                newItem.setItemBackground(this.E);
            }
            newItem.setShifting(h10);
            newItem.setLabelVisibilityMode(this.f13416t);
            g gVar = (g) this.Q.getItem(i10);
            newItem.e(gVar, 0);
            newItem.setItemPosition(i10);
            int itemId = gVar.getItemId();
            newItem.setOnTouchListener((View.OnTouchListener) this.f13415s.get(itemId));
            newItem.setOnClickListener(this.f13413q);
            int i13 = this.f13418v;
            if (i13 != 0 && itemId == i13) {
                this.f13419w = i10;
            }
            setBadgeIfNeeded(newItem);
            addView(newItem);
        }
        int min = Math.min(this.Q.size() - 1, this.f13419w);
        this.f13419w = min;
        this.Q.getItem(min).setChecked(true);
    }

    public ColorStateList e(int i10) {
        TypedValue typedValue = new TypedValue();
        if (!getContext().getTheme().resolveAttribute(i10, typedValue, true)) {
            return null;
        }
        ColorStateList a10 = j.a.a(getContext(), typedValue.resourceId);
        if (!getContext().getTheme().resolveAttribute(h.a.f20168v, typedValue, true)) {
            return null;
        }
        int i11 = typedValue.data;
        int defaultColor = a10.getDefaultColor();
        int[] iArr = S;
        return new ColorStateList(new int[][]{iArr, R, ViewGroup.EMPTY_STATE_SET}, new int[]{a10.getColorForState(iArr, defaultColor), i11, defaultColor});
    }

    protected abstract com.google.android.material.navigation.a g(Context context);

    /* JADX INFO: Access modifiers changed from: package-private */
    public SparseArray<xi.a> getBadgeDrawables() {
        return this.F;
    }

    public ColorStateList getIconTintList() {
        return this.f13420x;
    }

    public ColorStateList getItemActiveIndicatorColor() {
        return this.O;
    }

    public boolean getItemActiveIndicatorEnabled() {
        return this.I;
    }

    public int getItemActiveIndicatorHeight() {
        return this.K;
    }

    public int getItemActiveIndicatorMarginHorizontal() {
        return this.L;
    }

    public lj.k getItemActiveIndicatorShapeAppearance() {
        return this.M;
    }

    public int getItemActiveIndicatorWidth() {
        return this.J;
    }

    public Drawable getItemBackground() {
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        return (aVarArr == null || aVarArr.length <= 0) ? this.D : aVarArr[0].getBackground();
    }

    @Deprecated
    public int getItemBackgroundRes() {
        return this.E;
    }

    public int getItemIconSize() {
        return this.f13421y;
    }

    public int getItemPaddingBottom() {
        return this.H;
    }

    public int getItemPaddingTop() {
        return this.G;
    }

    public int getItemTextAppearanceActive() {
        return this.C;
    }

    public int getItemTextAppearanceInactive() {
        return this.B;
    }

    public ColorStateList getItemTextColor() {
        return this.f13422z;
    }

    public int getLabelVisibilityMode() {
        return this.f13416t;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public androidx.appcompat.view.menu.e getMenu() {
        return this.Q;
    }

    public int getSelectedItemId() {
        return this.f13418v;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public int getSelectedItemPosition() {
        return this.f13419w;
    }

    public int getWindowAnimations() {
        return 0;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public boolean h(int i10, int i11) {
        if (i10 == -1) {
            if (i11 > 3) {
                return true;
            }
        } else if (i10 == 0) {
            return true;
        }
        return false;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void k(SparseArray sparseArray) {
        for (int i10 = 0; i10 < sparseArray.size(); i10++) {
            int keyAt = sparseArray.keyAt(i10);
            if (this.F.indexOfKey(keyAt) < 0) {
                this.F.append(keyAt, sparseArray.get(keyAt));
            }
        }
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setBadge((xi.a) this.F.get(aVar.getId()));
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void l(int i10) {
        int size = this.Q.size();
        for (int i11 = 0; i11 < size; i11++) {
            MenuItem item = this.Q.getItem(i11);
            if (i10 == item.getItemId()) {
                this.f13418v = i10;
                this.f13419w = i11;
                item.setChecked(true);
                return;
            }
        }
    }

    public void m() {
        r rVar;
        androidx.appcompat.view.menu.e eVar = this.Q;
        if (eVar == null || this.f13417u == null) {
            return;
        }
        int size = eVar.size();
        if (size != this.f13417u.length) {
            d();
            return;
        }
        int i10 = this.f13418v;
        for (int i11 = 0; i11 < size; i11++) {
            MenuItem item = this.Q.getItem(i11);
            if (item.isChecked()) {
                this.f13418v = item.getItemId();
                this.f13419w = i11;
            }
        }
        if (i10 != this.f13418v && (rVar = this.f13412p) != null) {
            p.b(this, rVar);
        }
        boolean h10 = h(this.f13416t, this.Q.G().size());
        for (int i12 = 0; i12 < size; i12++) {
            this.P.k(true);
            this.f13417u[i12].setLabelVisibilityMode(this.f13416t);
            this.f13417u[i12].setShifting(h10);
            this.f13417u[i12].e((g) this.Q.getItem(i12), 0);
            this.P.k(false);
        }
    }

    @Override // android.view.View
    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo accessibilityNodeInfo) {
        super.onInitializeAccessibilityNodeInfo(accessibilityNodeInfo);
        t3.h.V0(accessibilityNodeInfo).i0(h.e.a(1, this.Q.G().size(), false, 1));
    }

    public void setIconTintList(ColorStateList colorStateList) {
        this.f13420x = colorStateList;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setIconTintList(colorStateList);
            }
        }
    }

    public void setItemActiveIndicatorColor(ColorStateList colorStateList) {
        this.O = colorStateList;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setActiveIndicatorDrawable(f());
            }
        }
    }

    public void setItemActiveIndicatorEnabled(boolean z10) {
        this.I = z10;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setActiveIndicatorEnabled(z10);
            }
        }
    }

    public void setItemActiveIndicatorHeight(int i10) {
        this.K = i10;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setActiveIndicatorHeight(i10);
            }
        }
    }

    public void setItemActiveIndicatorMarginHorizontal(int i10) {
        this.L = i10;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setActiveIndicatorMarginHorizontal(i10);
            }
        }
    }

    protected void setItemActiveIndicatorResizeable(boolean z10) {
        this.N = z10;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setActiveIndicatorResizeable(z10);
            }
        }
    }

    public void setItemActiveIndicatorShapeAppearance(lj.k kVar) {
        this.M = kVar;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setActiveIndicatorDrawable(f());
            }
        }
    }

    public void setItemActiveIndicatorWidth(int i10) {
        this.J = i10;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setActiveIndicatorWidth(i10);
            }
        }
    }

    public void setItemBackground(Drawable drawable) {
        this.D = drawable;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setItemBackground(drawable);
            }
        }
    }

    public void setItemBackgroundRes(int i10) {
        this.E = i10;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setItemBackground(i10);
            }
        }
    }

    public void setItemIconSize(int i10) {
        this.f13421y = i10;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setIconSize(i10);
            }
        }
    }

    public void setItemPaddingBottom(int i10) {
        this.H = i10;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setItemPaddingBottom(i10);
            }
        }
    }

    public void setItemPaddingTop(int i10) {
        this.G = i10;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setItemPaddingTop(i10);
            }
        }
    }

    public void setItemTextAppearanceActive(int i10) {
        this.C = i10;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setTextAppearanceActive(i10);
                ColorStateList colorStateList = this.f13422z;
                if (colorStateList != null) {
                    aVar.setTextColor(colorStateList);
                }
            }
        }
    }

    public void setItemTextAppearanceInactive(int i10) {
        this.B = i10;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setTextAppearanceInactive(i10);
                ColorStateList colorStateList = this.f13422z;
                if (colorStateList != null) {
                    aVar.setTextColor(colorStateList);
                }
            }
        }
    }

    public void setItemTextColor(ColorStateList colorStateList) {
        this.f13422z = colorStateList;
        com.google.android.material.navigation.a[] aVarArr = this.f13417u;
        if (aVarArr != null) {
            for (com.google.android.material.navigation.a aVar : aVarArr) {
                aVar.setTextColor(colorStateList);
            }
        }
    }

    public void setLabelVisibilityMode(int i10) {
        this.f13416t = i10;
    }

    public void setPresenter(d dVar) {
        this.P = dVar;
    }
}
