package androidx.leanback.widget.picker;

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 androidx.leanback.widget.picker.b;
import com.google.android.material.timepicker.TimeModel;
import defpackage.bo7;
import defpackage.q67;
import defpackage.r67;
import defpackage.sia;
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 q67 {
    public static final int[] ae = {5, 2, 1};
    public final Calendar aa;
    public final Calendar ab;
    public final Calendar ac;
    public final Calendar ad;
    public String r;
    public r67 s;
    public r67 t;
    public r67 u;
    public int v;
    public int w;
    public int x;
    public final SimpleDateFormat y;
    public final b.a z;

    public DatePicker(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.y = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());
        Locale locale = Locale.getDefault();
        getContext().getResources();
        this.z = new b.a(locale);
        this.ad = b.a(this.ad, locale);
        this.aa = b.a(this.aa, this.z.a);
        this.ab = b.a(this.ab, this.z.a);
        this.ac = b.a(this.ac, this.z.a);
        r67 r67Var = this.s;
        if (r67Var != null) {
            r67Var.d = this.z.b;
            b(this.v, r67Var);
        }
        int[] iArr = bo7.n.f;
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, iArr);
        sia.y(this, context, iArr, attributeSet, typedArrayObtainStyledAttributes, 0, 0);
        try {
            String string = typedArrayObtainStyledAttributes.getString(0);
            String string2 = typedArrayObtainStyledAttributes.getString(1);
            String string3 = typedArrayObtainStyledAttributes.getString(2);
            typedArrayObtainStyledAttributes.recycle();
            this.ad.clear();
            if (TextUtils.isEmpty(string) || !i(string, this.ad)) {
                this.ad.set(1900, 0, 1);
            }
            this.aa.setTimeInMillis(this.ad.getTimeInMillis());
            this.ad.clear();
            if (TextUtils.isEmpty(string2) || !i(string2, this.ad)) {
                this.ad.set(2100, 0, 1);
            }
            this.ab.setTimeInMillis(this.ad.getTimeInMillis());
            setDatePickerFormat(TextUtils.isEmpty(string3) ? new String(DateFormat.getDateFormatOrder(context)) : string3);
        } catch (Throwable th) {
            typedArrayObtainStyledAttributes.recycle();
            throw th;
        }
    }

    @Override // defpackage.q67
    public final void a(int i, int i2) {
        this.ad.setTimeInMillis(this.ac.getTimeInMillis());
        ArrayList arrayList = this.c;
        int i3 = (arrayList == null ? null : (r67) arrayList.get(i)).a;
        if (i == this.w) {
            this.ad.add(5, i2 - i3);
        } else if (i == this.v) {
            this.ad.add(2, i2 - i3);
        } else {
            if (i != this.x) {
                throw new IllegalArgumentException();
            }
            this.ad.add(1, i2 - i3);
        }
        j(this.ad.get(1), this.ad.get(2), this.ad.get(5));
    }

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

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

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

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

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

    public final void j(int i, int i2, int i3) {
        if (this.ac.get(1) == i && this.ac.get(2) == i3 && this.ac.get(5) == i2) {
            return;
        }
        this.ac.set(i, i2, i3);
        if (this.ac.before(this.aa)) {
            this.ac.setTimeInMillis(this.aa.getTimeInMillis());
        } else if (this.ac.after(this.ab)) {
            this.ac.setTimeInMillis(this.ab.getTimeInMillis());
        }
        post(new a(this));
    }

    public void setDate(long j) {
        this.ad.setTimeInMillis(j);
        j(this.ad.get(1), this.ad.get(2), this.ad.get(5));
    }

    public void setDatePickerFormat(String str) {
        if (TextUtils.isEmpty(str)) {
            str = new String(DateFormat.getDateFormatOrder(getContext()));
        }
        if (TextUtils.equals(this.r, str)) {
            return;
        }
        this.r = str;
        b.a aVar = this.z;
        String bestDateTimePattern = DateFormat.getBestDateTimePattern(aVar.a, str);
        if (TextUtils.isEmpty(bestDateTimePattern)) {
            bestDateTimePattern = "MM/dd/yyyy";
        }
        ArrayList arrayList = new ArrayList();
        StringBuilder sb = new StringBuilder();
        char[] cArr = {'Y', 'y', 'M', 'm', 'D', 'd'};
        boolean z = false;
        char c = 0;
        for (int i = 0; i < bestDateTimePattern.length(); i++) {
            char cCharAt = bestDateTimePattern.charAt(i);
            if (cCharAt != ' ') {
                if (cCharAt != '\'') {
                    if (!z) {
                        int i2 = 0;
                        while (true) {
                            if (i2 >= 6) {
                                sb.append(cCharAt);
                                break;
                            } else if (cCharAt != cArr[i2]) {
                                i2++;
                            } else if (cCharAt != c) {
                                arrayList.add(sb.toString());
                                sb.setLength(0);
                            }
                        }
                    } else {
                        sb.append(cCharAt);
                    }
                    c = cCharAt;
                } else if (z) {
                    z = false;
                } else {
                    sb.setLength(0);
                    z = true;
                }
            }
        }
        arrayList.add(sb.toString());
        if (arrayList.size() != str.length() + 1) {
            throw new IllegalStateException("Separators size: " + arrayList.size() + " must equal the size of datePickerFormat: " + str.length() + " + 1");
        }
        setSeparators(arrayList);
        this.t = null;
        this.s = null;
        this.u = null;
        this.v = -1;
        this.w = -1;
        this.x = -1;
        String upperCase = str.toUpperCase(aVar.a);
        ArrayList arrayList2 = new ArrayList(3);
        for (int i3 = 0; i3 < upperCase.length(); i3++) {
            char cCharAt2 = upperCase.charAt(i3);
            if (cCharAt2 == 'D') {
                if (this.t != null) {
                    throw new IllegalArgumentException("datePicker format error");
                }
                r67 r67Var = new r67();
                this.t = r67Var;
                arrayList2.add(r67Var);
                this.t.e = TimeModel.ZERO_LEADING_NUMBER_FORMAT;
                this.w = i3;
            } else if (cCharAt2 != 'M') {
                if (cCharAt2 != 'Y') {
                    throw new IllegalArgumentException("datePicker format error");
                }
                if (this.u != null) {
                    throw new IllegalArgumentException("datePicker format error");
                }
                r67 r67Var2 = new r67();
                this.u = r67Var2;
                arrayList2.add(r67Var2);
                this.x = i3;
                this.u.e = TimeModel.NUMBER_FORMAT;
            } else {
                if (this.s != null) {
                    throw new IllegalArgumentException("datePicker format error");
                }
                r67 r67Var3 = new r67();
                this.s = r67Var3;
                arrayList2.add(r67Var3);
                this.s.d = aVar.b;
                this.v = i3;
            }
        }
        setColumns(arrayList2);
        post(new a(this));
    }

    public void setMaxDate(long j) {
        this.ad.setTimeInMillis(j);
        if (this.ad.get(1) != this.ab.get(1) || this.ad.get(6) == this.ab.get(6)) {
            this.ab.setTimeInMillis(j);
            if (this.ac.after(this.ab)) {
                this.ac.setTimeInMillis(this.ab.getTimeInMillis());
            }
            post(new a(this));
        }
    }

    public void setMinDate(long j) {
        this.ad.setTimeInMillis(j);
        if (this.ad.get(1) != this.aa.get(1) || this.ad.get(6) == this.aa.get(6)) {
            this.aa.setTimeInMillis(j);
            if (this.ac.before(this.aa)) {
                this.ac.setTimeInMillis(this.aa.getTimeInMillis());
            }
            post(new a(this));
        }
    }
}
