package androidx.media3.ui;

import D1.M;
import D1.N;
import D1.O;
import K4.c;
import android.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import u0.T;
import u0.U;
import u0.Y;

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

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public CheckedTextView[][] f8967A;

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final HashMap f8975w;

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

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

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

    public TrackSelectionView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet, 0);
        setOrientation(1);
        setSaveFromParentEnabled(false);
        TypedArray typedArrayObtainStyledAttributes = context.getTheme().obtainStyledAttributes(new int[]{R.attr.selectableItemBackground});
        int resourceId = typedArrayObtainStyledAttributes.getResourceId(0, 0);
        this.f8969q = resourceId;
        typedArrayObtainStyledAttributes.recycle();
        LayoutInflater layoutInflaterFrom = LayoutInflater.from(context);
        this.f8970r = layoutInflaterFrom;
        N n8 = new N(0, this);
        this.f8973u = n8;
        this.f8978z = new c(getResources());
        this.f8974v = new ArrayList();
        this.f8975w = new HashMap();
        CheckedTextView checkedTextView = (CheckedTextView) layoutInflaterFrom.inflate(R.layout.simple_list_item_single_choice, (ViewGroup) this, false);
        this.f8971s = checkedTextView;
        checkedTextView.setBackgroundResource(resourceId);
        checkedTextView.setText(com.martsnew.tv.R.string.exo_track_selection_none);
        checkedTextView.setEnabled(false);
        checkedTextView.setFocusable(true);
        checkedTextView.setOnClickListener(n8);
        checkedTextView.setVisibility(8);
        addView(checkedTextView);
        addView(layoutInflaterFrom.inflate(com.martsnew.tv.R.layout.exo_list_divider, (ViewGroup) this, false));
        CheckedTextView checkedTextView2 = (CheckedTextView) layoutInflaterFrom.inflate(R.layout.simple_list_item_single_choice, (ViewGroup) this, false);
        this.f8972t = checkedTextView2;
        checkedTextView2.setBackgroundResource(resourceId);
        checkedTextView2.setText(com.martsnew.tv.R.string.exo_track_selection_auto);
        checkedTextView2.setEnabled(false);
        checkedTextView2.setFocusable(true);
        checkedTextView2.setOnClickListener(n8);
        addView(checkedTextView2);
    }

    public final void a() {
        this.f8971s.setChecked(this.f8968B);
        boolean z8 = this.f8968B;
        HashMap map = this.f8975w;
        this.f8972t.setChecked(!z8 && map.size() == 0);
        for (int i = 0; i < this.f8967A.length; i++) {
            U u8 = (U) map.get(((Y) this.f8974v.get(i)).f17247b);
            int i5 = 0;
            while (true) {
                CheckedTextView[] checkedTextViewArr = this.f8967A[i];
                if (i5 < checkedTextViewArr.length) {
                    if (u8 != null) {
                        Object tag = checkedTextViewArr[i5].getTag();
                        tag.getClass();
                        this.f8967A[i][i5].setChecked(u8.f17206b.contains(Integer.valueOf(((O) tag).f1705b)));
                    } else {
                        checkedTextViewArr[i5].setChecked(false);
                    }
                    i5++;
                }
            }
        }
    }

    public final void b() {
        for (int childCount = getChildCount() - 1; childCount >= 3; childCount--) {
            removeViewAt(childCount);
        }
        ArrayList arrayList = this.f8974v;
        boolean zIsEmpty = arrayList.isEmpty();
        CheckedTextView checkedTextView = this.f8972t;
        CheckedTextView checkedTextView2 = this.f8971s;
        if (zIsEmpty) {
            checkedTextView2.setEnabled(false);
            checkedTextView.setEnabled(false);
            return;
        }
        checkedTextView2.setEnabled(true);
        checkedTextView.setEnabled(true);
        this.f8967A = new CheckedTextView[arrayList.size()][];
        boolean z8 = this.f8977y && arrayList.size() > 1;
        for (int i = 0; i < arrayList.size(); i++) {
            Y y8 = (Y) arrayList.get(i);
            boolean z9 = this.f8976x && y8.f17248c;
            CheckedTextView[][] checkedTextViewArr = this.f8967A;
            int i5 = y8.f17246a;
            checkedTextViewArr[i] = new CheckedTextView[i5];
            O[] oArr = new O[i5];
            for (int i8 = 0; i8 < y8.f17246a; i8++) {
                oArr[i8] = new O(y8, i8);
            }
            for (int i9 = 0; i9 < i5; i9++) {
                LayoutInflater layoutInflater = this.f8970r;
                if (i9 == 0) {
                    addView(layoutInflater.inflate(com.martsnew.tv.R.layout.exo_list_divider, (ViewGroup) this, false));
                }
                CheckedTextView checkedTextView3 = (CheckedTextView) layoutInflater.inflate((z9 || z8) ? R.layout.simple_list_item_multiple_choice : R.layout.simple_list_item_single_choice, (ViewGroup) this, false);
                checkedTextView3.setBackgroundResource(this.f8969q);
                M m8 = this.f8978z;
                O o8 = oArr[i9];
                checkedTextView3.setText(((c) m8).B(o8.f1704a.f17247b.f17203d[o8.f1705b]));
                checkedTextView3.setTag(oArr[i9]);
                if (y8.a(i9)) {
                    checkedTextView3.setFocusable(true);
                    checkedTextView3.setOnClickListener(this.f8973u);
                } else {
                    checkedTextView3.setFocusable(false);
                    checkedTextView3.setEnabled(false);
                }
                this.f8967A[i][i9] = checkedTextView3;
                addView(checkedTextView3);
            }
        }
        a();
    }

    public boolean getIsDisabled() {
        return this.f8968B;
    }

    public Map<T, U> getOverrides() {
        return this.f8975w;
    }

    public void setAllowAdaptiveSelections(boolean z8) {
        if (this.f8976x != z8) {
            this.f8976x = z8;
            b();
        }
    }

    public void setAllowMultipleOverrides(boolean z8) {
        if (this.f8977y != z8) {
            this.f8977y = z8;
            if (!z8) {
                HashMap map = this.f8975w;
                if (map.size() > 1) {
                    ArrayList arrayList = this.f8974v;
                    HashMap map2 = new HashMap();
                    for (int i = 0; i < arrayList.size(); i++) {
                        U u8 = (U) map.get(((Y) arrayList.get(i)).f17247b);
                        if (u8 != null && map2.isEmpty()) {
                            map2.put(u8.f17205a, u8);
                        }
                    }
                    map.clear();
                    map.putAll(map2);
                }
            }
            b();
        }
    }

    public void setShowDisableOption(boolean z8) {
        this.f8971s.setVisibility(z8 ? 0 : 8);
    }

    public void setTrackNameProvider(M m8) {
        m8.getClass();
        this.f8978z = m8;
        b();
    }
}
