package p138.p176.p177.p190.p226.p230;

import android.graphics.PointF;
import android.text.Layout;
import android.text.TextUtils;
import android.util.Log;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import p000.p001.p002.p003.p004.C0567;
import p138.p139.p143.p144.C2803;
import p138.p176.p177.p190.p226.AbstractC3947;
import p138.p176.p177.p190.p226.C3946;
import p138.p176.p177.p190.p226.InterfaceC3949;
import p138.p176.p177.p190.p226.p230.C3985;
import p138.p176.p177.p190.p239.C4131;
import p138.p176.p177.p190.p239.C4140;
import p138.p176.p177.p190.p239.C4150;

/* JADX INFO: renamed from: ދ.ހ.֏.ؠ.ໟ.ތ.֏, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public final class C3983 extends AbstractC3947 {

    /* JADX INFO: renamed from: ޑ, reason: contains not printable characters */
    public static final Pattern f12565 = Pattern.compile("(?:(\\d+):)?(\\d+):(\\d+)[:.](\\d+)");

    /* JADX INFO: renamed from: ތ, reason: contains not printable characters */
    public final boolean f12566;

    /* JADX INFO: renamed from: ލ, reason: contains not printable characters */
    public final C3984 f12567;

    /* JADX INFO: renamed from: ގ, reason: contains not printable characters */
    public Map<String, C3985> f12568;

    /* JADX INFO: renamed from: ޏ, reason: contains not printable characters */
    public float f12569;

    /* JADX INFO: renamed from: ސ, reason: contains not printable characters */
    public float f12570;

    public C3983(List<byte[]> list) {
        super("SsaDecoder");
        this.f12569 = -3.4028235E38f;
        this.f12570 = -3.4028235E38f;
        if (list == null || list.isEmpty()) {
            this.f12566 = false;
            this.f12567 = null;
            return;
        }
        this.f12566 = true;
        String strM6760 = C4150.m6760(list.get(0));
        C0567.m1896(strM6760.startsWith("Format:"));
        C3984 c3984M6421 = C3984.m6421(strM6760);
        C0567.m1856(c3984M6421);
        this.f12567 = c3984M6421;
        m6420(new C4140(list.get(1)));
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static int m6417(long j, List<Long> list, List<List<C3946>> list2) {
        int i;
        int size = list.size() - 1;
        while (true) {
            if (size < 0) {
                i = 0;
                break;
            }
            if (list.get(size).longValue() == j) {
                return size;
            }
            if (list.get(size).longValue() < j) {
                i = size + 1;
                break;
            }
            size--;
        }
        list.add(i, Long.valueOf(j));
        list2.add(i, i == 0 ? new ArrayList() : new ArrayList(list2.get(i - 1)));
        return i;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static long m6418(String str) {
        Matcher matcher = f12565.matcher(str.trim());
        if (!matcher.matches()) {
            return -9223372036854775807L;
        }
        String strGroup = matcher.group(1);
        C4150.m6755(strGroup);
        return (Long.parseLong(matcher.group(4)) * 10000) + (Long.parseLong(matcher.group(3)) * 1000000) + (Long.parseLong(matcher.group(2)) * 60 * 1000000) + (Long.parseLong(strGroup) * 60 * 60 * 1000000);
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public static float m6419(int i) {
        if (i == 0) {
            return 0.05f;
        }
        if (i != 1) {
            return i != 2 ? -3.4028235E38f : 0.95f;
        }
        return 0.5f;
    }

    @Override // p138.p176.p177.p190.p226.AbstractC3947
    /* JADX INFO: renamed from: ֏ */
    public InterfaceC3949 mo6372(byte[] bArr, int i, boolean z) {
        C4140 c4140;
        int i2;
        int i3;
        float fM6419;
        float fM64192;
        Layout.Alignment alignment;
        int i4;
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        C4140 c41402 = new C4140(bArr, i);
        if (!this.f12566) {
            m6420(c41402);
        }
        C3984 c3984M6421 = this.f12566 ? this.f12567 : null;
        while (true) {
            String strM6690 = c41402.m6690();
            if (strM6690 == null) {
                return new C3988(arrayList, arrayList2);
            }
            if (strM6690.startsWith("Format:")) {
                c3984M6421 = C3984.m6421(strM6690);
            } else if (strM6690.startsWith("Dialogue:")) {
                if (c3984M6421 == null) {
                    C2803.m4901("Skipping dialogue line before complete format: ", strM6690, "SsaDecoder");
                } else {
                    C0567.m1896(strM6690.startsWith("Dialogue:"));
                    String[] strArrSplit = strM6690.substring(9).split(",", c3984M6421.f12575);
                    if (strArrSplit.length != c3984M6421.f12575) {
                        C2803.m4901("Skipping dialogue line with fewer columns than format: ", strM6690, "SsaDecoder");
                    } else {
                        long jM6418 = m6418(strArrSplit[c3984M6421.f12571]);
                        if (jM6418 == -9223372036854775807L) {
                            C2803.m4901("Skipping invalid timing: ", strM6690, "SsaDecoder");
                        } else {
                            long jM64182 = m6418(strArrSplit[c3984M6421.f12572]);
                            if (jM64182 == -9223372036854775807L) {
                                C2803.m4901("Skipping invalid timing: ", strM6690, "SsaDecoder");
                            } else {
                                Map<String, C3985> map = this.f12568;
                                C3985 c3985 = (map == null || (i4 = c3984M6421.f12573) == -1) ? null : map.get(strArrSplit[i4].trim());
                                String str = strArrSplit[c3984M6421.f12574];
                                Matcher matcher = C3985.C3987.f12581.matcher(str);
                                PointF pointF = null;
                                int i5 = -1;
                                while (true) {
                                    c4140 = c41402;
                                    if (matcher.find()) {
                                        String strGroup = matcher.group(1);
                                        try {
                                            PointF pointFM6423 = C3985.C3987.m6423(strGroup);
                                            if (pointFM6423 != null) {
                                                pointF = pointFM6423;
                                            }
                                        } catch (RuntimeException unused) {
                                        }
                                        try {
                                            Matcher matcher2 = C3985.C3987.f12584.matcher(strGroup);
                                            int iM6422 = matcher2.find() ? C3985.m6422(matcher2.group(1)) : -1;
                                            if (iM6422 != -1) {
                                                i5 = iM6422;
                                            }
                                        } catch (RuntimeException unused2) {
                                        }
                                        c41402 = c4140;
                                    } else {
                                        C3985.C3987 c3987 = new C3985.C3987(i5, pointF);
                                        String strReplaceAll = C3985.C3987.f12581.matcher(str).replaceAll("").replaceAll("\\\\N", "\n").replaceAll("\\\\n", "\n");
                                        float f = this.f12569;
                                        float f2 = this.f12570;
                                        int i6 = c3987.f12585;
                                        if (i6 == -1) {
                                            i6 = c3985 != null ? c3985.f12577 : -1;
                                        }
                                        switch (i6) {
                                            case 0:
                                            default:
                                                Log.w("SsaDecoder", C2803.m4871("Unknown alignment: ", i6));
                                            case -1:
                                                i2 = Integer.MIN_VALUE;
                                                break;
                                            case 1:
                                            case 4:
                                            case 7:
                                                i2 = 0;
                                                break;
                                            case 2:
                                            case 5:
                                            case 8:
                                                i2 = 1;
                                                break;
                                            case 3:
                                            case 6:
                                            case 9:
                                                i2 = 2;
                                                break;
                                        }
                                        switch (i6) {
                                            case 0:
                                            default:
                                                Log.w("SsaDecoder", C2803.m4871("Unknown alignment: ", i6));
                                            case -1:
                                                i3 = Integer.MIN_VALUE;
                                                break;
                                            case 1:
                                            case 2:
                                            case 3:
                                                i3 = 2;
                                                break;
                                            case 4:
                                            case 5:
                                            case 6:
                                                i3 = 1;
                                                break;
                                            case 7:
                                            case 8:
                                            case 9:
                                                i3 = 0;
                                                break;
                                        }
                                        PointF pointF2 = c3987.f12586;
                                        if (pointF2 == null || f2 == -3.4028235E38f || f == -3.4028235E38f) {
                                            fM6419 = m6419(i2);
                                            fM64192 = m6419(i3);
                                        } else {
                                            float f3 = pointF2.x / f;
                                            fM64192 = pointF2.y / f2;
                                            fM6419 = f3;
                                        }
                                        switch (i6) {
                                            case 0:
                                            default:
                                                Log.w("SsaDecoder", C2803.m4871("Unknown alignment: ", i6));
                                            case -1:
                                                alignment = null;
                                                break;
                                            case 1:
                                            case 4:
                                            case 7:
                                                alignment = Layout.Alignment.ALIGN_NORMAL;
                                                break;
                                            case 2:
                                            case 5:
                                            case 8:
                                                alignment = Layout.Alignment.ALIGN_CENTER;
                                                break;
                                            case 3:
                                            case 6:
                                            case 9:
                                                alignment = Layout.Alignment.ALIGN_OPPOSITE;
                                                break;
                                        }
                                        C3946 c3946 = new C3946(strReplaceAll, alignment, fM64192, 0, i3, fM6419, i2, -3.4028235E38f);
                                        int iM6417 = m6417(jM64182, arrayList2, arrayList);
                                        for (int iM64172 = m6417(jM6418, arrayList2, arrayList); iM64172 < iM6417; iM64172++) {
                                            ((List) arrayList.get(iM64172)).add(c3946);
                                        }
                                        c41402 = c4140;
                                    }
                                }
                            }
                        }
                    }
                }
                c4140 = c41402;
                c41402 = c4140;
            } else {
                c4140 = c41402;
                c41402 = c4140;
            }
        }
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    /* JADX WARN: Failed to restore switch over string. Please report as a decompilation issue */
    /* JADX WARN: Removed duplicated region for block: B:115:0x016e A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:118:0x008b A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:25:0x0058  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x00e3  */
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void m6420(C4140 c4140) {
        byte b;
        byte b2;
        C3985 c3985;
        while (true) {
            String strM6690 = c4140.m6690();
            if (strM6690 == null) {
                return;
            }
            if ("[Script Info]".equalsIgnoreCase(strM6690)) {
                while (true) {
                    String strM66902 = c4140.m6690();
                    if (strM66902 == null || (c4140.m6679() != 0 && c4140.m6686() == 91)) {
                        break;
                    }
                    String[] strArrSplit = strM66902.split(":");
                    if (strArrSplit.length == 2) {
                        String strM6809 = C4150.m6809(strArrSplit[0].trim());
                        switch (strM6809.hashCode()) {
                            case 1879649548:
                                b = strM6809.equals("playresx") ? (byte) 0 : (byte) -1;
                                break;
                            case 1879649549:
                                if (strM6809.equals("playresy")) {
                                    b = 1;
                                    break;
                                }
                                break;
                        }
                        if (b == 0) {
                            this.f12569 = Float.parseFloat(strArrSplit[1].trim());
                        } else if (b == 1) {
                            try {
                                this.f12570 = Float.parseFloat(strArrSplit[1].trim());
                            } catch (NumberFormatException unused) {
                            }
                        }
                    }
                }
            } else if ("[V4+ Styles]".equalsIgnoreCase(strM6690)) {
                LinkedHashMap linkedHashMap = new LinkedHashMap();
                while (true) {
                    C3985.C3986 c3986 = null;
                    while (true) {
                        String strM66903 = c4140.m6690();
                        if (strM66903 != null && (c4140.m6679() == 0 || c4140.m6686() != 91)) {
                            if (strM66903.startsWith("Format:")) {
                                String[] strArrSplit2 = TextUtils.split(strM66903.substring(7), ",");
                                int i = -1;
                                int i2 = -1;
                                for (int i3 = 0; i3 < strArrSplit2.length; i3++) {
                                    String strM68092 = C4150.m6809(strArrSplit2[i3].trim());
                                    int iHashCode = strM68092.hashCode();
                                    if (iHashCode != 3373707) {
                                        b2 = (iHashCode == 1767875043 && strM68092.equals("alignment")) ? (byte) 1 : (byte) -1;
                                    } else if (strM68092.equals("name")) {
                                        b2 = 0;
                                    }
                                    if (b2 == 0) {
                                        i = i3;
                                    } else if (b2 == 1) {
                                        i2 = i3;
                                    }
                                }
                                if (i != -1) {
                                    c3986 = new C3985.C3986(i, i2, strArrSplit2.length);
                                }
                            } else if (strM66903.startsWith("Style:")) {
                                if (c3986 == null) {
                                    C2803.m4901("Skipping 'Style:' line before 'Format:' line: ", strM66903, "SsaDecoder");
                                } else {
                                    C0567.m1896(strM66903.startsWith("Style:"));
                                    String[] strArrSplit3 = TextUtils.split(strM66903.substring(6), ",");
                                    int length = strArrSplit3.length;
                                    int i4 = c3986.f12580;
                                    if (length != i4) {
                                        Log.w("SsaStyle", C4150.m6758("Skipping malformed 'Style:' line (expected %s values, found %s): '%s'", Integer.valueOf(i4), Integer.valueOf(strArrSplit3.length), strM66903));
                                    } else {
                                        try {
                                            c3985 = new C3985(strArrSplit3[c3986.f12578].trim(), C3985.m6422(strArrSplit3[c3986.f12579]));
                                        } catch (RuntimeException e) {
                                            C4131.m6646("SsaStyle", "Skipping malformed 'Style:' line: '" + strM66903 + "'", e);
                                            c3985 = null;
                                        }
                                        if (c3985 == null) {
                                            linkedHashMap.put(c3985.f12576, c3985);
                                        }
                                    }
                                    c3985 = null;
                                    if (c3985 == null) {
                                    }
                                }
                            }
                        }
                    }
                }
                this.f12568 = linkedHashMap;
            } else if ("[V4 Styles]".equalsIgnoreCase(strM6690)) {
                Log.i("SsaDecoder", "[V4 Styles] are not supported");
            } else if ("[Events]".equalsIgnoreCase(strM6690)) {
                return;
            }
        }
    }
}
