package w1;

import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan;
import android.util.Pair;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
import w0.h;
import x0.AbstractC1709a;
import x0.v;

/* JADX INFO: renamed from: w1.c, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1697c {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final String f18240a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final String f18241b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final boolean f18242c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final long f18243d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final long f18244e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final g f18245f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final String[] f18246g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final String f18247h;
    public final String i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final C1697c f18248j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final HashMap f18249k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final HashMap f18250l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public ArrayList f18251m;

    public C1697c(String str, String str2, long j8, long j9, g gVar, String[] strArr, String str3, String str4, C1697c c1697c) {
        this.f18240a = str;
        this.f18241b = str2;
        this.i = str4;
        this.f18245f = gVar;
        this.f18246g = strArr;
        this.f18242c = str2 != null;
        this.f18243d = j8;
        this.f18244e = j9;
        str3.getClass();
        this.f18247h = str3;
        this.f18248j = c1697c;
        this.f18249k = new HashMap();
        this.f18250l = new HashMap();
    }

    public static C1697c a(String str) {
        return new C1697c(null, str.replaceAll("\r\n", "\n").replaceAll(" *\n *", "\n").replaceAll("\n", " ").replaceAll("[ \t\\x0B\f\r]+", " "), -9223372036854775807L, -9223372036854775807L, null, null, "", null, null);
    }

    public static SpannableStringBuilder e(String str, TreeMap treeMap) {
        if (!treeMap.containsKey(str)) {
            w0.a aVar = new w0.a();
            aVar.f18164a = new SpannableStringBuilder();
            treeMap.put(str, aVar);
        }
        CharSequence charSequence = ((w0.a) treeMap.get(str)).f18164a;
        charSequence.getClass();
        return (SpannableStringBuilder) charSequence;
    }

    public final C1697c b(int i) {
        ArrayList arrayList = this.f18251m;
        if (arrayList != null) {
            return (C1697c) arrayList.get(i);
        }
        throw new IndexOutOfBoundsException();
    }

    public final int c() {
        ArrayList arrayList = this.f18251m;
        if (arrayList == null) {
            return 0;
        }
        return arrayList.size();
    }

    public final void d(TreeSet treeSet, boolean z8) {
        String str = this.f18240a;
        boolean zEquals = "p".equals(str);
        boolean zEquals2 = "div".equals(str);
        if (z8 || zEquals || (zEquals2 && this.i != null)) {
            long j8 = this.f18243d;
            if (j8 != -9223372036854775807L) {
                treeSet.add(Long.valueOf(j8));
            }
            long j9 = this.f18244e;
            if (j9 != -9223372036854775807L) {
                treeSet.add(Long.valueOf(j9));
            }
        }
        if (this.f18251m == null) {
            return;
        }
        for (int i = 0; i < this.f18251m.size(); i++) {
            ((C1697c) this.f18251m.get(i)).d(treeSet, z8 || zEquals);
        }
    }

    public final boolean f(long j8) {
        long j9 = this.f18243d;
        long j10 = this.f18244e;
        if (j9 == -9223372036854775807L && j10 == -9223372036854775807L) {
            return true;
        }
        if (j9 <= j8 && j10 == -9223372036854775807L) {
            return true;
        }
        if (j9 != -9223372036854775807L || j8 >= j10) {
            return j9 <= j8 && j8 < j10;
        }
        return true;
    }

    public final void g(long j8, String str, ArrayList arrayList) {
        String str2;
        String str3 = this.f18247h;
        if (!"".equals(str3)) {
            str = str3;
        }
        if (f(j8) && "div".equals(this.f18240a) && (str2 = this.i) != null) {
            arrayList.add(new Pair(str, str2));
            return;
        }
        for (int i = 0; i < c(); i++) {
            b(i).g(j8, str, arrayList);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:143:0x0215  */
    /* JADX WARN: Removed duplicated region for block: B:146:0x0223  */
    /* JADX WARN: Removed duplicated region for block: B:170:0x0297  */
    /* JADX WARN: Removed duplicated region for block: B:173:0x02b1  */
    /* JADX WARN: Removed duplicated region for block: B:194:0x02d4 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:43:0x00ae  */
    /* JADX WARN: Removed duplicated region for block: B:44:0x00b1  */
    /* JADX WARN: Removed duplicated region for block: B:47:0x00b7  */
    /* JADX WARN: Removed duplicated region for block: B:48:0x00b9  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void h(long j8, Map map, HashMap map2, String str, TreeMap treeMap) {
        Iterator it;
        int i;
        int i5;
        C1697c c1697c;
        int i8;
        int i9;
        g gVarG;
        int i10;
        float f3;
        int i11;
        int i12;
        Map map3 = map;
        int i13 = -1;
        int i14 = 1;
        if (f(j8)) {
            String str2 = this.f18247h;
            String str3 = "".equals(str2) ? str : str2;
            Iterator it2 = this.f18250l.entrySet().iterator();
            while (it2.hasNext()) {
                Map.Entry entry = (Map.Entry) it2.next();
                String str4 = (String) entry.getKey();
                HashMap map4 = this.f18249k;
                int iIntValue = map4.containsKey(str4) ? ((Integer) map4.get(str4)).intValue() : 0;
                int iIntValue2 = ((Integer) entry.getValue()).intValue();
                if (iIntValue != iIntValue2) {
                    w0.a aVar = (w0.a) treeMap.get(str4);
                    aVar.getClass();
                    f fVar = (f) map2.get(str3);
                    fVar.getClass();
                    g gVarG2 = W2.g.G(this.f18245f, this.f18246g, map3);
                    SpannableStringBuilder spannableStringBuilder = (SpannableStringBuilder) aVar.f18164a;
                    if (spannableStringBuilder == null) {
                        spannableStringBuilder = new SpannableStringBuilder();
                        aVar.f18164a = spannableStringBuilder;
                    }
                    if (gVarG2 != null) {
                        int i15 = gVarG2.f18280h;
                        if (((i15 == i13 && gVarG2.i == i13) ? i13 : (i15 == i14 ? i14 : 0) | (gVarG2.i == i14 ? 2 : 0)) != i13) {
                            int i16 = gVarG2.f18280h;
                            if (i16 != i13) {
                                i12 = (i16 != i14 ? i14 : 0) | (gVarG2.i != i14 ? 2 : 0);
                                StyleSpan styleSpan = new StyleSpan(i12);
                                i = 33;
                                spannableStringBuilder.setSpan(styleSpan, iIntValue, iIntValue2, 33);
                            } else if (gVarG2.i == i13) {
                                i12 = i13;
                                i14 = 1;
                                StyleSpan styleSpan2 = new StyleSpan(i12);
                                i = 33;
                                spannableStringBuilder.setSpan(styleSpan2, iIntValue, iIntValue2, 33);
                            } else {
                                i14 = 1;
                                i12 = (i16 != i14 ? i14 : 0) | (gVarG2.i != i14 ? 2 : 0);
                                StyleSpan styleSpan22 = new StyleSpan(i12);
                                i = 33;
                                spannableStringBuilder.setSpan(styleSpan22, iIntValue, iIntValue2, 33);
                            }
                        } else {
                            i = 33;
                        }
                        if (gVarG2.f18278f == i14) {
                            spannableStringBuilder.setSpan(new StrikethroughSpan(), iIntValue, iIntValue2, i);
                        }
                        if (gVarG2.f18279g == i14) {
                            spannableStringBuilder.setSpan(new UnderlineSpan(), iIntValue, iIntValue2, i);
                        }
                        if (gVarG2.f18275c) {
                            if (!gVarG2.f18275c) {
                                throw new IllegalStateException("Font color has not been defined.");
                            }
                            U2.a.b(spannableStringBuilder, new ForegroundColorSpan(gVarG2.f18274b), iIntValue, iIntValue2);
                        }
                        if (gVarG2.f18277e) {
                            if (!gVarG2.f18277e) {
                                throw new IllegalStateException("Background color has not been defined.");
                            }
                            U2.a.b(spannableStringBuilder, new BackgroundColorSpan(gVarG2.f18276d), iIntValue, iIntValue2);
                        }
                        if (gVarG2.f18273a != null) {
                            U2.a.b(spannableStringBuilder, new TypefaceSpan(gVarG2.f18273a), iIntValue, iIntValue2);
                        }
                        C1696b c1696b = gVarG2.f18289r;
                        if (c1696b != null) {
                            int i17 = c1696b.f18237a;
                            if (i17 == i13) {
                                int i18 = fVar.f18272j;
                                i17 = (i18 == 2 || i18 == 1) ? 3 : 1;
                                i11 = 1;
                            } else {
                                i11 = c1696b.f18238b;
                            }
                            i5 = i13;
                            int i19 = c1696b.f18239c;
                            if (i19 == -2) {
                                i19 = 1;
                            }
                            U2.a.b(spannableStringBuilder, new h(i17, i11, i19), iIntValue, iIntValue2);
                        } else {
                            i5 = i13;
                        }
                        int i20 = gVarG2.f18284m;
                        if (i20 == 2) {
                            C1697c c1697c2 = this.f18248j;
                            while (true) {
                                if (c1697c2 == null) {
                                    c1697c2 = null;
                                    break;
                                }
                                g gVarG3 = W2.g.G(c1697c2.f18245f, c1697c2.f18246g, map3);
                                if (gVarG3 != null && gVarG3.f18284m == 1) {
                                    break;
                                } else {
                                    c1697c2 = c1697c2.f18248j;
                                }
                            }
                            if (c1697c2 != null) {
                                ArrayDeque arrayDeque = new ArrayDeque();
                                arrayDeque.push(c1697c2);
                                while (true) {
                                    if (arrayDeque.isEmpty()) {
                                        c1697c = null;
                                        break;
                                    }
                                    C1697c c1697c3 = (C1697c) arrayDeque.pop();
                                    g gVarG4 = W2.g.G(c1697c3.f18245f, c1697c3.f18246g, map3);
                                    if (gVarG4 != null && gVarG4.f18284m == 3) {
                                        c1697c = c1697c3;
                                        break;
                                    }
                                    for (int iC = c1697c3.c() - 1; iC >= 0; iC--) {
                                        arrayDeque.push(c1697c3.b(iC));
                                    }
                                }
                                if (c1697c != null) {
                                    if (c1697c.c() != 1 || c1697c.b(0).f18241b == null) {
                                        AbstractC1709a.t("TtmlRenderUtil", "Skipping rubyText node without exactly one text child.");
                                    } else {
                                        String str5 = c1697c.b(0).f18241b;
                                        int i21 = v.f18553a;
                                        g gVarG5 = W2.g.G(c1697c.f18245f, c1697c.f18246g, map3);
                                        if (gVarG5 != null) {
                                            i8 = gVarG5.f18285n;
                                            i9 = i5;
                                        } else {
                                            i8 = i5;
                                            i9 = i8;
                                        }
                                        if (i8 == i9 && (gVarG = W2.g.G(c1697c2.f18245f, c1697c2.f18246g, map3)) != null) {
                                            i8 = gVarG.f18285n;
                                        }
                                        spannableStringBuilder.setSpan(new w0.g(i8, str5), iIntValue, iIntValue2, 33);
                                    }
                                }
                                if (gVarG2.f18288q == 1) {
                                    U2.a.b(spannableStringBuilder, new w0.e(), iIntValue, iIntValue2);
                                }
                                i10 = gVarG2.f18281j;
                                if (i10 != 1) {
                                    it = it2;
                                    f3 = 100.0f;
                                    U2.a.b(spannableStringBuilder, new AbsoluteSizeSpan((int) gVarG2.f18282k, true), iIntValue, iIntValue2);
                                } else if (i10 == 2) {
                                    it = it2;
                                    f3 = 100.0f;
                                    U2.a.b(spannableStringBuilder, new RelativeSizeSpan(gVarG2.f18282k), iIntValue, iIntValue2);
                                } else if (i10 != 3) {
                                    it = it2;
                                    f3 = 100.0f;
                                } else {
                                    float sizeChange = gVarG2.f18282k / 100.0f;
                                    RelativeSizeSpan[] relativeSizeSpanArr = (RelativeSizeSpan[]) spannableStringBuilder.getSpans(iIntValue, iIntValue2, RelativeSizeSpan.class);
                                    int length = relativeSizeSpanArr.length;
                                    f3 = 100.0f;
                                    int i22 = 0;
                                    while (i22 < length) {
                                        RelativeSizeSpan relativeSizeSpan = relativeSizeSpanArr[i22];
                                        Iterator it3 = it2;
                                        if (spannableStringBuilder.getSpanStart(relativeSizeSpan) <= iIntValue && spannableStringBuilder.getSpanEnd(relativeSizeSpan) >= iIntValue2) {
                                            sizeChange = relativeSizeSpan.getSizeChange() * sizeChange;
                                        }
                                        if (spannableStringBuilder.getSpanStart(relativeSizeSpan) == iIntValue && spannableStringBuilder.getSpanEnd(relativeSizeSpan) == iIntValue2) {
                                            if (spannableStringBuilder.getSpanFlags(relativeSizeSpan) == 33) {
                                                spannableStringBuilder.removeSpan(relativeSizeSpan);
                                            }
                                        }
                                        i22++;
                                        it2 = it3;
                                    }
                                    it = it2;
                                    spannableStringBuilder.setSpan(new RelativeSizeSpan(sizeChange), iIntValue, iIntValue2, 33);
                                }
                                if (!"p".equals(this.f18240a)) {
                                    float f7 = gVarG2.f18290s;
                                    if (f7 != Float.MAX_VALUE) {
                                        aVar.f18179q = (f7 * (-90.0f)) / f3;
                                    }
                                    Layout.Alignment alignment = gVarG2.f18286o;
                                    if (alignment != null) {
                                        aVar.f18166c = alignment;
                                    }
                                    Layout.Alignment alignment2 = gVarG2.f18287p;
                                    if (alignment2 != null) {
                                        aVar.f18167d = alignment2;
                                    }
                                }
                            }
                        } else if (i20 == 3 || i20 == 4) {
                            spannableStringBuilder.setSpan(new C1695a(), iIntValue, iIntValue2, 33);
                        }
                        if (gVarG2.f18288q == 1) {
                        }
                        i10 = gVarG2.f18281j;
                        if (i10 != 1) {
                        }
                        if (!"p".equals(this.f18240a)) {
                        }
                    }
                    map3 = map;
                    it2 = it;
                    i13 = -1;
                    i14 = 1;
                }
                it = it2;
                map3 = map;
                it2 = it;
                i13 = -1;
                i14 = 1;
            }
            for (int i23 = 0; i23 < c(); i23++) {
                b(i23).h(j8, map, map2, str3, treeMap);
            }
        }
    }

    public final void i(long j8, boolean z8, String str, TreeMap treeMap) {
        boolean z9;
        TreeMap treeMap2;
        long j9;
        HashMap map = this.f18249k;
        map.clear();
        HashMap map2 = this.f18250l;
        map2.clear();
        String str2 = this.f18240a;
        if ("metadata".equals(str2)) {
            return;
        }
        String str3 = this.f18247h;
        String str4 = "".equals(str3) ? str : str3;
        if (this.f18242c && z8) {
            SpannableStringBuilder spannableStringBuilderE = e(str4, treeMap);
            String str5 = this.f18241b;
            str5.getClass();
            spannableStringBuilderE.append((CharSequence) str5);
            return;
        }
        if ("br".equals(str2) && z8) {
            e(str4, treeMap).append('\n');
            return;
        }
        if (f(j8)) {
            for (Map.Entry entry : treeMap.entrySet()) {
                String str6 = (String) entry.getKey();
                CharSequence charSequence = ((w0.a) entry.getValue()).f18164a;
                charSequence.getClass();
                map.put(str6, Integer.valueOf(charSequence.length()));
            }
            boolean zEquals = "p".equals(str2);
            for (int i = 0; i < c(); i++) {
                C1697c c1697cB = b(i);
                if (z8 || zEquals) {
                    z9 = true;
                    treeMap2 = treeMap;
                    j9 = j8;
                } else {
                    z9 = false;
                    j9 = j8;
                    treeMap2 = treeMap;
                }
                c1697cB.i(j9, z9, str4, treeMap2);
            }
            if (zEquals) {
                SpannableStringBuilder spannableStringBuilderE2 = e(str4, treeMap);
                int length = spannableStringBuilderE2.length() - 1;
                while (length >= 0 && spannableStringBuilderE2.charAt(length) == ' ') {
                    length--;
                }
                if (length >= 0 && spannableStringBuilderE2.charAt(length) != '\n') {
                    spannableStringBuilderE2.append('\n');
                }
            }
            for (Map.Entry entry2 : treeMap.entrySet()) {
                String str7 = (String) entry2.getKey();
                CharSequence charSequence2 = ((w0.a) entry2.getValue()).f18164a;
                charSequence2.getClass();
                map2.put(str7, Integer.valueOf(charSequence2.length()));
            }
        }
    }
}
