package androidx.leanback.widget.picker;

import M.J;
import M4.k;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.util.Log;
import e0.C0853a;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import k0.a;
import o0.AbstractC1323c;
import o0.C1324d;
import v0.AbstractC1664c;

/* JADX INFO: loaded from: classes.dex */
public class DatePicker extends AbstractC1323c {

    /* JADX INFO: renamed from: S, reason: collision with root package name */
    public static final int[] f8685S = {5, 2, 1};

    /* JADX INFO: renamed from: F, reason: collision with root package name */
    public String f8686F;

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    public C1324d f8687G;
    public C1324d H;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    public C1324d f8688I;

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

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

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

    /* JADX INFO: renamed from: M, reason: collision with root package name */
    public final SimpleDateFormat f8692M;

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

    /* JADX INFO: renamed from: O, reason: collision with root package name */
    public final Calendar f8694O;

    /* JADX INFO: renamed from: P, reason: collision with root package name */
    public final Calendar f8695P;

    /* JADX INFO: renamed from: Q, reason: collision with root package name */
    public final Calendar f8696Q;

    /* JADX INFO: renamed from: R, reason: collision with root package name */
    public final Calendar f8697R;

    public DatePicker(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f8692M = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());
        Locale locale = Locale.getDefault();
        getContext().getResources();
        this.f8693N = new C0853a(locale);
        this.f8697R = AbstractC1664c.r(this.f8697R, locale);
        this.f8694O = AbstractC1664c.r(this.f8694O, (Locale) this.f8693N.f12397r);
        this.f8695P = AbstractC1664c.r(this.f8695P, (Locale) this.f8693N.f12397r);
        this.f8696Q = AbstractC1664c.r(this.f8696Q, (Locale) this.f8693N.f12397r);
        C1324d c1324d = this.f8687G;
        if (c1324d != null) {
            c1324d.f15578d = (String[]) this.f8693N.f12398s;
            a(this.f8689J, c1324d);
        }
        int[] iArr = a.f13743d;
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, iArr);
        J.i(this, context, iArr, attributeSet, typedArrayObtainStyledAttributes, 0);
        try {
            String string = typedArrayObtainStyledAttributes.getString(0);
            String string2 = typedArrayObtainStyledAttributes.getString(1);
            String string3 = typedArrayObtainStyledAttributes.getString(2);
            typedArrayObtainStyledAttributes.recycle();
            this.f8697R.clear();
            if (TextUtils.isEmpty(string) || !g(string, this.f8697R)) {
                this.f8697R.set(1900, 0, 1);
            }
            this.f8694O.setTimeInMillis(this.f8697R.getTimeInMillis());
            this.f8697R.clear();
            if (TextUtils.isEmpty(string2) || !g(string2, this.f8697R)) {
                this.f8697R.set(2100, 0, 1);
            }
            this.f8695P.setTimeInMillis(this.f8697R.getTimeInMillis());
            setDatePickerFormat(TextUtils.isEmpty(string3) ? new String(DateFormat.getDateFormatOrder(context)) : string3);
        } catch (Throwable th) {
            typedArrayObtainStyledAttributes.recycle();
            throw th;
        }
    }

    public final boolean g(String str, Calendar calendar) {
        try {
            calendar.setTime(this.f8692M.parse(str));
            return true;
        } catch (ParseException unused) {
            Log.w("DatePicker", "Date: " + str + " not in format: MM/dd/yyyy");
            return false;
        }
    }

    public long getDate() {
        return this.f8696Q.getTimeInMillis();
    }

    public String getDatePickerFormat() {
        return this.f8686F;
    }

    public long getMaxDate() {
        return this.f8695P.getTimeInMillis();
    }

    public long getMinDate() {
        return this.f8694O.getTimeInMillis();
    }

    public final void h(int i, int i5, int i8) {
        if (this.f8696Q.get(1) == i && this.f8696Q.get(2) == i8 && this.f8696Q.get(5) == i5) {
            return;
        }
        this.f8696Q.set(i, i5, i8);
        if (this.f8696Q.before(this.f8694O)) {
            this.f8696Q.setTimeInMillis(this.f8694O.getTimeInMillis());
        } else if (this.f8696Q.after(this.f8695P)) {
            this.f8696Q.setTimeInMillis(this.f8695P.getTimeInMillis());
        }
        post(new k(29, this));
    }

    public void setDate(long j8) {
        this.f8697R.setTimeInMillis(j8);
        h(this.f8697R.get(1), this.f8697R.get(2), this.f8697R.get(5));
    }

    public void setDatePickerFormat(String str) {
        int i = 6;
        C0853a c0853a = this.f8693N;
        String str2 = TextUtils.isEmpty(str) ? new String(DateFormat.getDateFormatOrder(getContext())) : str;
        if (TextUtils.equals(this.f8686F, str2)) {
            return;
        }
        this.f8686F = str2;
        String bestDateTimePattern = DateFormat.getBestDateTimePattern((Locale) c0853a.f12397r, str2);
        if (TextUtils.isEmpty(bestDateTimePattern)) {
            bestDateTimePattern = "MM/dd/yyyy";
        }
        ArrayList arrayList = new ArrayList();
        StringBuilder sb = new StringBuilder();
        char[] cArr = {'Y', 'y', 'M', 'm', 'D', 'd'};
        int i5 = 0;
        boolean z8 = false;
        char c3 = 0;
        while (i5 < bestDateTimePattern.length()) {
            char cCharAt = bestDateTimePattern.charAt(i5);
            if (cCharAt != ' ') {
                if (cCharAt != '\'') {
                    if (!z8) {
                        int i8 = 0;
                        while (true) {
                            if (i8 >= i) {
                                sb.append(cCharAt);
                                break;
                            } else if (cCharAt != cArr[i8]) {
                                i8++;
                                i = 6;
                            } else if (cCharAt != c3) {
                                arrayList.add(sb.toString());
                                sb.setLength(0);
                            }
                        }
                    } else {
                        sb.append(cCharAt);
                    }
                    c3 = cCharAt;
                } else if (z8) {
                    z8 = false;
                } else {
                    sb.setLength(0);
                    z8 = true;
                }
            }
            i5++;
            i = 6;
        }
        arrayList.add(sb.toString());
        if (arrayList.size() != str2.length() + 1) {
            throw new IllegalStateException("Separators size: " + arrayList.size() + " must equal the size of datePickerFormat: " + str2.length() + " + 1");
        }
        setSeparators(arrayList);
        this.H = null;
        this.f8687G = null;
        this.f8688I = null;
        this.f8689J = -1;
        this.f8690K = -1;
        this.f8691L = -1;
        String upperCase = str2.toUpperCase((Locale) c0853a.f12397r);
        ArrayList arrayList2 = new ArrayList(3);
        for (int i9 = 0; i9 < upperCase.length(); i9++) {
            char cCharAt2 = upperCase.charAt(i9);
            if (cCharAt2 == 'D') {
                if (this.H != null) {
                    throw new IllegalArgumentException("datePicker format error");
                }
                C1324d c1324d = new C1324d();
                this.H = c1324d;
                arrayList2.add(c1324d);
                this.H.f15579e = "%02d";
                this.f8690K = i9;
            } else if (cCharAt2 != 'M') {
                if (cCharAt2 != 'Y') {
                    throw new IllegalArgumentException("datePicker format error");
                }
                if (this.f8688I != null) {
                    throw new IllegalArgumentException("datePicker format error");
                }
                C1324d c1324d2 = new C1324d();
                this.f8688I = c1324d2;
                arrayList2.add(c1324d2);
                this.f8691L = i9;
                this.f8688I.f15579e = "%d";
            } else {
                if (this.f8687G != null) {
                    throw new IllegalArgumentException("datePicker format error");
                }
                C1324d c1324d3 = new C1324d();
                this.f8687G = c1324d3;
                arrayList2.add(c1324d3);
                this.f8687G.f15578d = (String[]) c0853a.f12398s;
                this.f8689J = i9;
            }
        }
        setColumns(arrayList2);
        post(new k(29, this));
    }

    public void setMaxDate(long j8) {
        this.f8697R.setTimeInMillis(j8);
        if (this.f8697R.get(1) != this.f8695P.get(1) || this.f8697R.get(6) == this.f8695P.get(6)) {
            this.f8695P.setTimeInMillis(j8);
            if (this.f8696Q.after(this.f8695P)) {
                this.f8696Q.setTimeInMillis(this.f8695P.getTimeInMillis());
            }
            post(new k(29, this));
        }
    }

    public void setMinDate(long j8) {
        this.f8697R.setTimeInMillis(j8);
        if (this.f8697R.get(1) != this.f8694O.get(1) || this.f8697R.get(6) == this.f8694O.get(6)) {
            this.f8694O.setTimeInMillis(j8);
            if (this.f8696Q.before(this.f8694O)) {
                this.f8696Q.setTimeInMillis(this.f8694O.getTimeInMillis());
            }
            post(new k(29, this));
        }
    }
}
