package com.google.android.material.button;

import a3.g0;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.RadioButton;
import android.widget.ToggleButton;
import androidx.annotation.BoolRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.s1;
import com.google.android.material.internal.q0;
import com.google.android.material.shape.l0;
import com.google.android.material.timepicker.j;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import q0.f;
import x8.l;
import x8.m;
import xyz.stream.utils.a0;

/* JADX INFO: loaded from: classes2.dex */
public class MaterialButtonToggleGroup extends MaterialButtonGroup {
    public static final int R = l.Widget_MaterialComponents_MaterialButtonToggleGroup;
    public HashSet Q;

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

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

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

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final int f22369m;

    public MaterialButtonToggleGroup(@NonNull Context context) {
        this(context, null);
    }

    @NonNull
    private String getChildrenA11yClassName() {
        return (this.f22367k ? RadioButton.class : ToggleButton.class).getName();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public int getIndexWithinVisibleButtons(@Nullable View view) {
        if (!(view instanceof MaterialButton)) {
            return -1;
        }
        int i10 = 0;
        for (int i11 = 0; i11 < getChildCount(); i11++) {
            if (getChildAt(i11) == view) {
                return i10;
            }
            if ((getChildAt(i11) instanceof MaterialButton) && getChildAt(i11).getVisibility() != 8) {
                i10++;
            }
        }
        return -1;
    }

    private int getVisibleButtonCount() {
        int i10 = 0;
        for (int i11 = 0; i11 < getChildCount(); i11++) {
            if ((getChildAt(i11) instanceof MaterialButton) && getChildAt(i11).getVisibility() != 8) {
                i10++;
            }
        }
        return i10;
    }

    private void setupButtonChild(@NonNull MaterialButton materialButton) {
        materialButton.setMaxLines(1);
        materialButton.setEllipsize(TextUtils.TruncateAt.END);
        materialButton.setCheckable(true);
        materialButton.setA11yClassName(getChildrenA11yClassName());
    }

    @Override // com.google.android.material.button.MaterialButtonGroup, android.view.ViewGroup
    public final void addView(View view, int i10, ViewGroup.LayoutParams layoutParams) {
        if (view instanceof MaterialButton) {
            super.addView(view, i10, layoutParams);
            MaterialButton materialButton = (MaterialButton) view;
            setupButtonChild(materialButton);
            h(materialButton.getId(), materialButton.isChecked());
            s1.setAccessibilityDelegate(materialButton, new g0(this, 5));
        }
    }

    @IdRes
    public int getCheckedButtonId() {
        if (!this.f22367k || this.Q.isEmpty()) {
            return -1;
        }
        return ((Integer) this.Q.iterator().next()).intValue();
    }

    @NonNull
    public List<Integer> getCheckedButtonIds() {
        ArrayList arrayList = new ArrayList();
        for (int i10 = 0; i10 < getChildCount(); i10++) {
            int id2 = ((MaterialButton) getChildAt(i10)).getId();
            if (this.Q.contains(Integer.valueOf(id2))) {
                arrayList.add(Integer.valueOf(id2));
            }
        }
        return arrayList;
    }

    public final void h(int i10, boolean z10) {
        if (i10 == -1) {
            return;
        }
        HashSet hashSet = new HashSet(this.Q);
        if (z10 && !hashSet.contains(Integer.valueOf(i10))) {
            if (this.f22367k && !hashSet.isEmpty()) {
                hashSet.clear();
            }
            hashSet.add(Integer.valueOf(i10));
        } else {
            if (z10 || !hashSet.contains(Integer.valueOf(i10))) {
                return;
            }
            if (!this.f22368l || hashSet.size() > 1) {
                hashSet.remove(Integer.valueOf(i10));
            }
        }
        i(hashSet);
    }

    public final void i(Set set) {
        HashSet hashSet = this.Q;
        this.Q = new HashSet(set);
        for (int i10 = 0; i10 < getChildCount(); i10++) {
            int id2 = ((MaterialButton) getChildAt(i10)).getId();
            boolean zContains = set.contains(Integer.valueOf(id2));
            View viewFindViewById = findViewById(id2);
            if (viewFindViewById instanceof MaterialButton) {
                this.f22366j = true;
                ((MaterialButton) viewFindViewById).setChecked(zContains);
                this.f22366j = false;
            }
            if (hashSet.contains(Integer.valueOf(id2)) != set.contains(Integer.valueOf(id2))) {
                set.contains(Integer.valueOf(id2));
                Iterator it = this.f22365i.iterator();
                while (it.hasNext()) {
                    ((j) it.next()).a();
                }
            }
        }
        invalidate();
    }

    @Override // android.view.View
    public final void onFinishInflate() {
        super.onFinishInflate();
        int i10 = this.f22369m;
        if (i10 != -1) {
            i(Collections.singleton(Integer.valueOf(i10)));
        }
    }

    @Override // android.view.View
    public final void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo accessibilityNodeInfo) {
        super.onInitializeAccessibilityNodeInfo(accessibilityNodeInfo);
        new f(accessibilityNodeInfo).k(a0.y(1, getVisibleButtonCount(), this.f22367k ? 1 : 2, false));
    }

    public void setSelectionRequired(boolean z10) {
        this.f22368l = z10;
    }

    public void setSingleSelection(boolean z10) {
        if (this.f22367k != z10) {
            this.f22367k = z10;
            i(new HashSet());
        }
        String childrenA11yClassName = getChildrenA11yClassName();
        for (int i10 = 0; i10 < getChildCount(); i10++) {
            ((MaterialButton) getChildAt(i10)).setA11yClassName(childrenA11yClassName);
        }
    }

    public MaterialButtonToggleGroup(@NonNull Context context, @Nullable AttributeSet attributeSet) {
        this(context, attributeSet, x8.c.materialButtonToggleGroupStyle);
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public MaterialButtonToggleGroup(@NonNull Context context, @Nullable AttributeSet attributeSet, int i10) {
        int i11 = R;
        super(x9.a.wrap(context, attributeSet, i10, i11), attributeSet, i10);
        this.f22365i = new LinkedHashSet();
        this.f22366j = false;
        this.Q = new HashSet();
        TypedArray typedArrayC = q0.c(getContext(), attributeSet, m.MaterialButtonToggleGroup, i10, i11, new int[0]);
        setSingleSelection(typedArrayC.getBoolean(m.MaterialButtonToggleGroup_singleSelection, false));
        this.f22369m = typedArrayC.getResourceId(m.MaterialButtonToggleGroup_checkedButton, -1);
        this.f22368l = typedArrayC.getBoolean(m.MaterialButtonToggleGroup_selectionRequired, false);
        if (this.innerCornerSize == null) {
            this.innerCornerSize = l0.c(new com.google.android.material.shape.a(0.0f));
        }
        setEnabled(typedArrayC.getBoolean(m.MaterialButtonToggleGroup_android_enabled, true));
        typedArrayC.recycle();
        setImportantForAccessibility(1);
    }

    public void setSingleSelection(@BoolRes int i10) {
        setSingleSelection(getResources().getBoolean(i10));
    }
}
