package androidx.leanback.widget.picker;

import I2.K;
import T.p;
import Z1.a;
import a2.d;
import a2.e;
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 java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

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

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

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public final p f12059A;

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

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

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

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

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

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public e f12065t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public e f12066u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public e f12067v;

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

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

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

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

    public DatePicker(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f12071z = new SimpleDateFormat("MM/dd/yyyy");
        Locale locale = Locale.getDefault();
        getContext().getResources();
        this.f12059A = new p(locale);
        this.f12063E = W.d.n(this.f12063E, locale);
        this.f12060B = W.d.n(this.f12060B, (Locale) this.f12059A.f6345b);
        this.f12061C = W.d.n(this.f12061C, (Locale) this.f12059A.f6345b);
        this.f12062D = W.d.n(this.f12062D, (Locale) this.f12059A.f6345b);
        e eVar = this.f12065t;
        if (eVar != null) {
            eVar.f10414d = (String[]) this.f12059A.f6346c;
            a(this.f12068w, eVar);
        }
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, a.f9144d);
        String string = typedArrayObtainStyledAttributes.getString(0);
        String string2 = typedArrayObtainStyledAttributes.getString(1);
        this.f12063E.clear();
        if (TextUtils.isEmpty(string) || !g(string, this.f12063E)) {
            this.f12063E.set(1900, 0, 1);
        }
        this.f12060B.setTimeInMillis(this.f12063E.getTimeInMillis());
        this.f12063E.clear();
        if (TextUtils.isEmpty(string2) || !g(string2, this.f12063E)) {
            this.f12063E.set(2100, 0, 1);
        }
        this.f12061C.setTimeInMillis(this.f12063E.getTimeInMillis());
        String string3 = typedArrayObtainStyledAttributes.getString(2);
        setDatePickerFormat(TextUtils.isEmpty(string3) ? new String(DateFormat.getDateFormatOrder(context)) : string3);
    }

    public final boolean g(String str, Calendar calendar) {
        try {
            calendar.setTime(this.f12071z.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.f12062D.getTimeInMillis();
    }

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

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

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

    public void setDatePickerFormat(String str) {
        int i6 = 6;
        p pVar = this.f12059A;
        String str2 = TextUtils.isEmpty(str) ? new String(DateFormat.getDateFormatOrder(getContext())) : str;
        if (TextUtils.equals(this.f12064s, str2)) {
            return;
        }
        this.f12064s = str2;
        String bestDateTimePattern = DateFormat.getBestDateTimePattern((Locale) pVar.f6345b, 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 i7 = 0;
        boolean z6 = false;
        char c6 = 0;
        while (i7 < bestDateTimePattern.length()) {
            char cCharAt = bestDateTimePattern.charAt(i7);
            if (cCharAt != ' ') {
                if (cCharAt != '\'') {
                    if (!z6) {
                        int i8 = 0;
                        while (true) {
                            if (i8 >= i6) {
                                sb.append(cCharAt);
                                break;
                            } else if (cCharAt != cArr[i8]) {
                                i8++;
                                i6 = 6;
                            } else if (cCharAt != c6) {
                                arrayList.add(sb.toString());
                                sb.setLength(0);
                            }
                        }
                    } else {
                        sb.append(cCharAt);
                    }
                    c6 = cCharAt;
                } else if (z6) {
                    z6 = false;
                } else {
                    sb.setLength(0);
                    z6 = true;
                }
            }
            i7++;
            i6 = 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.f12066u = null;
        this.f12065t = null;
        this.f12067v = null;
        this.f12068w = -1;
        this.f12069x = -1;
        this.f12070y = -1;
        String upperCase = str2.toUpperCase();
        ArrayList arrayList2 = new ArrayList(3);
        for (int i9 = 0; i9 < upperCase.length(); i9++) {
            char cCharAt2 = upperCase.charAt(i9);
            if (cCharAt2 == 'D') {
                if (this.f12066u != null) {
                    throw new IllegalArgumentException("datePicker format error");
                }
                e eVar = new e();
                this.f12066u = eVar;
                arrayList2.add(eVar);
                this.f12066u.f10415e = "%02d";
                this.f12069x = i9;
            } else if (cCharAt2 != 'M') {
                if (cCharAt2 != 'Y') {
                    throw new IllegalArgumentException("datePicker format error");
                }
                if (this.f12067v != null) {
                    throw new IllegalArgumentException("datePicker format error");
                }
                e eVar2 = new e();
                this.f12067v = eVar2;
                arrayList2.add(eVar2);
                this.f12070y = i9;
                this.f12067v.f10415e = "%d";
            } else {
                if (this.f12065t != null) {
                    throw new IllegalArgumentException("datePicker format error");
                }
                e eVar3 = new e();
                this.f12065t = eVar3;
                arrayList2.add(eVar3);
                this.f12065t.f10414d = (String[]) pVar.f6346c;
                this.f12068w = i9;
            }
        }
        setColumns(arrayList2);
        post(new K(this, 2));
    }

    public void setMaxDate(long j4) {
        this.f12063E.setTimeInMillis(j4);
        if (this.f12063E.get(1) != this.f12061C.get(1) || this.f12063E.get(6) == this.f12061C.get(6)) {
            this.f12061C.setTimeInMillis(j4);
            if (this.f12062D.after(this.f12061C)) {
                this.f12062D.setTimeInMillis(this.f12061C.getTimeInMillis());
            }
            post(new K(this, 2));
        }
    }

    public void setMinDate(long j4) {
        this.f12063E.setTimeInMillis(j4);
        if (this.f12063E.get(1) != this.f12060B.get(1) || this.f12063E.get(6) == this.f12060B.get(6)) {
            this.f12060B.setTimeInMillis(j4);
            if (this.f12062D.before(this.f12060B)) {
                this.f12062D.setTimeInMillis(this.f12060B.getTimeInMillis());
            }
            post(new K(this, 2));
        }
    }
}
