package com.google.android.material.button;

import A3.a;
import K4.c;
import M.J;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.LinearLayout;
import com.google.android.material.datepicker.i;
import com.google.android.material.timepicker.h;
import com.martsnew.tv.R;
import h3.AbstractC1031a;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.TreeMap;
import l6.C1139a;
import n3.d;
import n3.e;
import n3.f;
import s3.k;
import x3.C1713a;
import x3.j;

/* JADX INFO: loaded from: classes.dex */
public class MaterialButtonToggleGroup extends LinearLayout {

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public static final /* synthetic */ int f10172A = 0;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final ArrayList f10173q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final e f10174r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final C1139a f10175s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final LinkedHashSet f10176t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final d f10177u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public Integer[] f10178v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public boolean f10179w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public boolean f10180x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public boolean f10181y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public int f10182z;

    public MaterialButtonToggleGroup(Context context, AttributeSet attributeSet) {
        super(a.a(context, attributeSet, R.attr.materialButtonToggleGroupStyle, R.style.Widget_MaterialComponents_MaterialButtonToggleGroup), attributeSet, R.attr.materialButtonToggleGroupStyle);
        this.f10173q = new ArrayList();
        this.f10174r = new e(this);
        this.f10175s = new C1139a(this);
        this.f10176t = new LinkedHashSet();
        this.f10177u = new d(this);
        this.f10179w = false;
        TypedArray typedArrayE = k.e(getContext(), attributeSet, AbstractC1031a.f13406m, R.attr.materialButtonToggleGroupStyle, R.style.Widget_MaterialComponents_MaterialButtonToggleGroup, new int[0]);
        setSingleSelection(typedArrayE.getBoolean(2, false));
        this.f10182z = typedArrayE.getResourceId(0, -1);
        this.f10181y = typedArrayE.getBoolean(1, false);
        setChildrenDrawingOrderEnabled(true);
        typedArrayE.recycle();
        Field field = J.f4065a;
        setImportantForAccessibility(1);
    }

    private int getFirstVisibleChildIndex() {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (c(i)) {
                return i;
            }
        }
        return -1;
    }

    private int getLastVisibleChildIndex() {
        for (int childCount = getChildCount() - 1; childCount >= 0; childCount--) {
            if (c(childCount)) {
                return childCount;
            }
        }
        return -1;
    }

    private int getVisibleButtonCount() {
        int i = 0;
        for (int i5 = 0; i5 < getChildCount(); i5++) {
            if ((getChildAt(i5) instanceof MaterialButton) && c(i5)) {
                i++;
            }
        }
        return i;
    }

    private void setCheckedId(int i) {
        this.f10182z = i;
        b();
    }

    private void setGeneratedIdIfNeeded(MaterialButton materialButton) {
        if (materialButton.getId() == -1) {
            Field field = J.f4065a;
            materialButton.setId(View.generateViewId());
        }
    }

    private void setupButtonChild(MaterialButton materialButton) {
        materialButton.setMaxLines(1);
        materialButton.setEllipsize(TextUtils.TruncateAt.END);
        materialButton.setCheckable(true);
        materialButton.f10166u.add(this.f10174r);
        materialButton.setOnPressedChangeListenerInternal(this.f10175s);
        materialButton.setShouldDrawSurfaceColorStroke(true);
    }

    public final void a() {
        int firstVisibleChildIndex = getFirstVisibleChildIndex();
        if (firstVisibleChildIndex == -1) {
            return;
        }
        for (int i = firstVisibleChildIndex + 1; i < getChildCount(); i++) {
            MaterialButton materialButton = (MaterialButton) getChildAt(i);
            int iMin = Math.min(materialButton.getStrokeWidth(), ((MaterialButton) getChildAt(i - 1)).getStrokeWidth());
            ViewGroup.LayoutParams layoutParams = materialButton.getLayoutParams();
            LinearLayout.LayoutParams layoutParams2 = layoutParams instanceof LinearLayout.LayoutParams ? (LinearLayout.LayoutParams) layoutParams : new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height);
            if (getOrientation() == 0) {
                layoutParams2.setMarginEnd(0);
                layoutParams2.setMarginStart(-iMin);
                layoutParams2.topMargin = 0;
            } else {
                layoutParams2.bottomMargin = 0;
                layoutParams2.topMargin = -iMin;
                layoutParams2.setMarginStart(0);
            }
            materialButton.setLayoutParams(layoutParams2);
        }
        if (getChildCount() == 0 || firstVisibleChildIndex == -1) {
            return;
        }
        LinearLayout.LayoutParams layoutParams3 = (LinearLayout.LayoutParams) ((MaterialButton) getChildAt(firstVisibleChildIndex)).getLayoutParams();
        if (getOrientation() == 1) {
            layoutParams3.topMargin = 0;
            layoutParams3.bottomMargin = 0;
        } else {
            layoutParams3.setMarginEnd(0);
            layoutParams3.setMarginStart(0);
            layoutParams3.leftMargin = 0;
            layoutParams3.rightMargin = 0;
        }
    }

    @Override // android.view.ViewGroup
    public final void addView(View view, int i, ViewGroup.LayoutParams layoutParams) {
        if (!(view instanceof MaterialButton)) {
            Log.e("MaterialButtonToggleGroup", "Child views must be of type MaterialButton.");
            return;
        }
        super.addView(view, i, layoutParams);
        MaterialButton materialButton = (MaterialButton) view;
        setGeneratedIdIfNeeded(materialButton);
        setupButtonChild(materialButton);
        if (materialButton.D) {
            d(materialButton.getId(), true);
            setCheckedId(materialButton.getId());
        }
        x3.k shapeAppearanceModel = materialButton.getShapeAppearanceModel();
        this.f10173q.add(new f(shapeAppearanceModel.f18640e, shapeAppearanceModel.f18643h, shapeAppearanceModel.f18641f, shapeAppearanceModel.f18642g));
        J.j(materialButton, new i(1, this));
    }

    public final void b() {
        Iterator it = this.f10176t.iterator();
        while (it.hasNext()) {
            ((h) it.next()).a();
        }
    }

    public final boolean c(int i) {
        return getChildAt(i).getVisibility() != 8;
    }

    public final boolean d(int i, boolean z8) {
        List<Integer> checkedButtonIds = getCheckedButtonIds();
        if (this.f10181y && checkedButtonIds.isEmpty()) {
            View viewFindViewById = findViewById(i);
            if (viewFindViewById instanceof MaterialButton) {
                this.f10179w = true;
                ((MaterialButton) viewFindViewById).setChecked(true);
                this.f10179w = false;
            }
            this.f10182z = i;
            return false;
        }
        if (z8 && this.f10180x) {
            checkedButtonIds.remove(Integer.valueOf(i));
            Iterator<Integer> it = checkedButtonIds.iterator();
            while (it.hasNext()) {
                View viewFindViewById2 = findViewById(it.next().intValue());
                if (viewFindViewById2 instanceof MaterialButton) {
                    this.f10179w = true;
                    ((MaterialButton) viewFindViewById2).setChecked(false);
                    this.f10179w = false;
                }
                b();
            }
        }
        return true;
    }

    @Override // android.view.ViewGroup, android.view.View
    public final void dispatchDraw(Canvas canvas) {
        TreeMap treeMap = new TreeMap(this.f10177u);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            treeMap.put((MaterialButton) getChildAt(i), Integer.valueOf(i));
        }
        this.f10178v = (Integer[]) treeMap.values().toArray(new Integer[0]);
        super.dispatchDraw(canvas);
    }

    public final void e() {
        f fVar;
        int childCount = getChildCount();
        int firstVisibleChildIndex = getFirstVisibleChildIndex();
        int lastVisibleChildIndex = getLastVisibleChildIndex();
        for (int i = 0; i < childCount; i++) {
            MaterialButton materialButton = (MaterialButton) getChildAt(i);
            if (materialButton.getVisibility() != 8) {
                j jVarE = materialButton.getShapeAppearanceModel().e();
                f fVar2 = (f) this.f10173q.get(i);
                if (firstVisibleChildIndex != lastVisibleChildIndex) {
                    boolean z8 = getOrientation() == 0;
                    C1713a c1713a = f.f15158e;
                    if (i == firstVisibleChildIndex) {
                        if (z8) {
                            Field field = J.f4065a;
                            fVar = getLayoutDirection() == 1 ? new f(c1713a, c1713a, fVar2.f15160b, fVar2.f15161c) : new f(fVar2.f15159a, fVar2.f15162d, c1713a, c1713a);
                        } else {
                            fVar = new f(fVar2.f15159a, c1713a, fVar2.f15160b, c1713a);
                        }
                    } else if (i != lastVisibleChildIndex) {
                        fVar2 = null;
                    } else if (z8) {
                        Field field2 = J.f4065a;
                        fVar = getLayoutDirection() == 1 ? new f(fVar2.f15159a, fVar2.f15162d, c1713a, c1713a) : new f(c1713a, c1713a, fVar2.f15160b, fVar2.f15161c);
                    } else {
                        fVar = new f(c1713a, fVar2.f15162d, c1713a, fVar2.f15161c);
                    }
                    fVar2 = fVar;
                }
                if (fVar2 == null) {
                    jVarE.f18629e = new C1713a(0.0f);
                    jVarE.f18630f = new C1713a(0.0f);
                    jVarE.f18631g = new C1713a(0.0f);
                    jVarE.f18632h = new C1713a(0.0f);
                } else {
                    jVarE.f18629e = fVar2.f15159a;
                    jVarE.f18632h = fVar2.f15162d;
                    jVarE.f18630f = fVar2.f15160b;
                    jVarE.f18631g = fVar2.f15161c;
                }
                materialButton.setShapeAppearanceModel(jVarE.a());
            }
        }
    }

    @Override // android.widget.LinearLayout, android.view.ViewGroup, android.view.View
    public CharSequence getAccessibilityClassName() {
        return MaterialButtonToggleGroup.class.getName();
    }

    public int getCheckedButtonId() {
        if (this.f10180x) {
            return this.f10182z;
        }
        return -1;
    }

    public List<Integer> getCheckedButtonIds() {
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < getChildCount(); i++) {
            MaterialButton materialButton = (MaterialButton) getChildAt(i);
            if (materialButton.D) {
                arrayList.add(Integer.valueOf(materialButton.getId()));
            }
        }
        return arrayList;
    }

    @Override // android.view.ViewGroup
    public final int getChildDrawingOrder(int i, int i5) {
        Integer[] numArr = this.f10178v;
        if (numArr != null && i5 < numArr.length) {
            return numArr[i5].intValue();
        }
        Log.w("MaterialButtonToggleGroup", "Child order wasn't updated");
        return i5;
    }

    @Override // android.view.View
    public final void onFinishInflate() {
        MaterialButton materialButton;
        super.onFinishInflate();
        int i = this.f10182z;
        if (i == -1 || (materialButton = (MaterialButton) findViewById(i)) == null) {
            return;
        }
        materialButton.setChecked(true);
    }

    @Override // android.view.View
    public final void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo accessibilityNodeInfo) {
        super.onInitializeAccessibilityNodeInfo(accessibilityNodeInfo);
        accessibilityNodeInfo.setCollectionInfo((AccessibilityNodeInfo.CollectionInfo) c.E(1, getVisibleButtonCount(), this.f10180x ? 1 : 2).f3604r);
    }

    @Override // android.widget.LinearLayout, android.view.View
    public final void onMeasure(int i, int i5) {
        e();
        a();
        super.onMeasure(i, i5);
    }

    @Override // android.view.ViewGroup
    public final void onViewRemoved(View view) {
        super.onViewRemoved(view);
        if (view instanceof MaterialButton) {
            MaterialButton materialButton = (MaterialButton) view;
            materialButton.f10166u.remove(this.f10174r);
            materialButton.setOnPressedChangeListenerInternal(null);
        }
        int iIndexOfChild = indexOfChild(view);
        if (iIndexOfChild >= 0) {
            this.f10173q.remove(iIndexOfChild);
        }
        e();
        a();
    }

    public void setSelectionRequired(boolean z8) {
        this.f10181y = z8;
    }

    public void setSingleSelection(boolean z8) {
        if (this.f10180x != z8) {
            this.f10180x = z8;
            this.f10179w = true;
            for (int i = 0; i < getChildCount(); i++) {
                MaterialButton materialButton = (MaterialButton) getChildAt(i);
                materialButton.setChecked(false);
                materialButton.getId();
                b();
            }
            this.f10179w = false;
            setCheckedId(-1);
        }
    }

    public void setSingleSelection(int i) {
        setSingleSelection(getResources().getBoolean(i));
    }
}
