package L1;

import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/* JADX INFO: loaded from: classes2.dex */
public class f implements e, K1.a {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private static final int[] f1353f = {14, 13, 12, 11, 5, 2, 1};

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private SimpleDateFormat f1354a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private int f1355b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private SimpleDateFormat f1356c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private int f1357d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private boolean f1358e;

    public f() {
        g("MMM d yyyy", null);
        i("MMM d HH:mm", null);
    }

    private static int b(SimpleDateFormat simpleDateFormat) {
        if (simpleDateFormat == null) {
            return 0;
        }
        String pattern = simpleDateFormat.toPattern();
        for (char c2 : "SsmHdM".toCharArray()) {
            if (pattern.indexOf(c2) != -1) {
                if (c2 == 'H') {
                    return e(11);
                }
                if (c2 == 'M') {
                    return e(2);
                }
                if (c2 == 'S') {
                    return e(14);
                }
                if (c2 == 'd') {
                    return e(5);
                }
                if (c2 == 'm') {
                    return e(12);
                }
                if (c2 == 's') {
                    return e(13);
                }
            }
        }
        return 0;
    }

    private static int e(int i2) {
        int i3 = 0;
        while (true) {
            int[] iArr = f1353f;
            if (i3 >= iArr.length) {
                return 0;
            }
            if (i2 == iArr[i3]) {
                return i3;
            }
            i3++;
        }
    }

    private void g(String str, DateFormatSymbols dateFormatSymbols) {
        if (str != null) {
            if (dateFormatSymbols != null) {
                this.f1354a = new SimpleDateFormat(str, dateFormatSymbols);
            } else {
                this.f1354a = new SimpleDateFormat(str);
            }
            this.f1354a.setLenient(false);
        } else {
            this.f1354a = null;
        }
        this.f1355b = b(this.f1354a);
    }

    private static void h(int i2, Calendar calendar) {
        if (i2 <= 0) {
            return;
        }
        int i3 = f1353f[i2 - 1];
        if (calendar.get(i3) != 0) {
            return;
        }
        calendar.clear(i3);
    }

    private void i(String str, DateFormatSymbols dateFormatSymbols) {
        if (str != null) {
            if (dateFormatSymbols != null) {
                this.f1356c = new SimpleDateFormat(str, dateFormatSymbols);
            } else {
                this.f1356c = new SimpleDateFormat(str);
            }
            this.f1356c.setLenient(false);
        } else {
            this.f1356c = null;
        }
        this.f1357d = b(this.f1356c);
    }

    private void j(String str) {
        TimeZone timeZone = TimeZone.getDefault();
        if (str != null) {
            timeZone = TimeZone.getTimeZone(str);
        }
        this.f1354a.setTimeZone(timeZone);
        SimpleDateFormat simpleDateFormat = this.f1356c;
        if (simpleDateFormat != null) {
            simpleDateFormat.setTimeZone(timeZone);
        }
    }

    @Override // L1.e
    public Calendar a(String str) {
        return f(str, Calendar.getInstance());
    }

    public TimeZone c() {
        return this.f1354a.getTimeZone();
    }

    @Override // K1.a
    public void d(K1.f fVar) {
        String strD = fVar.d();
        String strG = fVar.g();
        DateFormatSymbols dateFormatSymbolsA = strG != null ? K1.f.a(strG) : strD != null ? K1.f.j(strD) : K1.f.j("en");
        i(fVar.c(), dateFormatSymbolsA);
        String strB = fVar.b();
        if (strB == null) {
            throw new IllegalArgumentException("defaultFormatString cannot be null");
        }
        g(strB, dateFormatSymbolsA);
        j(fVar.f());
        this.f1358e = fVar.i();
    }

    public Calendar f(String str, Calendar calendar) throws ParseException {
        Calendar calendar2 = (Calendar) calendar.clone();
        calendar2.setTimeZone(c());
        if (this.f1356c != null) {
            Calendar calendar3 = (Calendar) calendar.clone();
            calendar3.setTimeZone(c());
            if (this.f1358e) {
                calendar3.add(5, 1);
            }
            String str2 = str + " " + Integer.toString(calendar3.get(1));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(this.f1356c.toPattern() + " yyyy", this.f1356c.getDateFormatSymbols());
            simpleDateFormat.setLenient(false);
            simpleDateFormat.setTimeZone(this.f1356c.getTimeZone());
            ParsePosition parsePosition = new ParsePosition(0);
            Date date = simpleDateFormat.parse(str2, parsePosition);
            if (date != null && parsePosition.getIndex() == str2.length()) {
                calendar2.setTime(date);
                if (calendar2.after(calendar3)) {
                    calendar2.add(1, -1);
                }
                h(this.f1357d, calendar2);
                return calendar2;
            }
        }
        ParsePosition parsePosition2 = new ParsePosition(0);
        Date date2 = this.f1354a.parse(str, parsePosition2);
        if (date2 != null && parsePosition2.getIndex() == str.length()) {
            calendar2.setTime(date2);
            h(this.f1355b, calendar2);
            return calendar2;
        }
        throw new ParseException("Timestamp '" + str + "' could not be parsed using a server time of " + calendar.getTime().toString(), parsePosition2.getErrorIndex());
    }
}
