package t2;

import android.text.SpannableStringBuilder;
import android.util.Pair;
import androidx.annotation.Nullable;
import androidx.media3.common.text.Cue;
import androidx.media3.common.util.Assertions;
import androidx.media3.exoplayer.upstream.CmcdConfiguration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

/* JADX INFO: loaded from: classes.dex */
public final class c {

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

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

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

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

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

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

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

    @Nullable
    public final String imageId;

    @Nullable
    public final c parent;

    @Nullable
    public final i style;

    @Nullable
    private final String[] styleIds;

    @Nullable
    public final String tag;

    @Nullable
    public final String text;

    private c(@Nullable String str, @Nullable String str2, long j10, long j11, @Nullable i iVar, @Nullable String[] strArr, String str3, @Nullable String str4, @Nullable c cVar) {
        this.tag = str;
        this.text = str2;
        this.imageId = str4;
        this.style = iVar;
        this.styleIds = strArr;
        this.f32805a = str2 != null;
        this.f32806b = j10;
        this.f32807c = j11;
        this.f32808d = (String) Assertions.checkNotNull(str3);
        this.parent = cVar;
        this.f32809e = new HashMap();
        this.f32810f = new HashMap();
    }

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

    public static c buildNode(@Nullable String str, long j10, long j11, @Nullable i iVar, @Nullable String[] strArr, String str2, @Nullable String str3, @Nullable c cVar) {
        return new c(str, null, j10, j11, iVar, strArr, str2, str3, cVar);
    }

    public static SpannableStringBuilder e(String str, TreeMap treeMap) {
        if (!treeMap.containsKey(str)) {
            Cue.Builder builder = new Cue.Builder();
            builder.setText(new SpannableStringBuilder());
            treeMap.put(str, builder);
        }
        return (SpannableStringBuilder) Assertions.checkNotNull(((Cue.Builder) treeMap.get(str)).getText());
    }

    public final c b(int i10) {
        ArrayList arrayList = this.f32811g;
        if (arrayList != null) {
            return (c) arrayList.get(i10);
        }
        throw new IndexOutOfBoundsException();
    }

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

    public final void d(TreeSet treeSet, boolean z10) {
        boolean zEquals = "p".equals(this.tag);
        boolean zEquals2 = "div".equals(this.tag);
        if (z10 || zEquals || (zEquals2 && this.imageId != null)) {
            long j10 = this.f32806b;
            if (j10 != -9223372036854775807L) {
                treeSet.add(Long.valueOf(j10));
            }
            long j11 = this.f32807c;
            if (j11 != -9223372036854775807L) {
                treeSet.add(Long.valueOf(j11));
            }
        }
        if (this.f32811g == null) {
            return;
        }
        for (int i10 = 0; i10 < this.f32811g.size(); i10++) {
            ((c) this.f32811g.get(i10)).d(treeSet, z10 || zEquals);
        }
    }

    public final boolean f(long j10) {
        long j11 = this.f32806b;
        long j12 = this.f32807c;
        return (j11 == -9223372036854775807L && j12 == -9223372036854775807L) || (j11 <= j10 && j12 == -9223372036854775807L) || ((j11 == -9223372036854775807L && j10 < j12) || (j11 <= j10 && j10 < j12));
    }

    public final void g(String str, long j10, ArrayList arrayList) {
        String str2 = this.f32808d;
        if (!"".equals(str2)) {
            str = str2;
        }
        if (f(j10) && "div".equals(this.tag) && this.imageId != null) {
            arrayList.add(new Pair(str, this.imageId));
            return;
        }
        for (int i10 = 0; i10 < c(); i10++) {
            b(i10).g(str, j10, arrayList);
        }
    }

    @Nullable
    public String[] getStyleIds() {
        return this.styleIds;
    }

    public final void h(long j10, Map map, Map map2, String str, TreeMap treeMap) {
        if (f(j10)) {
            String str2 = this.f32808d;
            if ("".equals(str2)) {
                str2 = str;
            }
            Iterator it = this.f32810f.entrySet().iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                Map.Entry entry = (Map.Entry) it.next();
                String str3 = (String) entry.getKey();
                HashMap map3 = this.f32809e;
                int iIntValue = map3.containsKey(str3) ? ((Integer) map3.get(str3)).intValue() : 0;
                int iIntValue2 = ((Integer) entry.getValue()).intValue();
                if (iIntValue != iIntValue2) {
                    Cue.Builder builder = (Cue.Builder) Assertions.checkNotNull((Cue.Builder) treeMap.get(str3));
                    int i10 = ((g) Assertions.checkNotNull((g) map2.get(str2))).f32835j;
                    i iVarResolveStyle = h.resolveStyle(this.style, this.styleIds, map);
                    SpannableStringBuilder spannableStringBuilder = (SpannableStringBuilder) builder.getText();
                    if (spannableStringBuilder == null) {
                        spannableStringBuilder = new SpannableStringBuilder();
                        builder.setText(spannableStringBuilder);
                    }
                    SpannableStringBuilder spannableStringBuilder2 = spannableStringBuilder;
                    if (iVarResolveStyle != null) {
                        h.applyStylesToSpan(spannableStringBuilder2, iIntValue, iIntValue2, iVarResolveStyle, this.parent, map, i10);
                        if ("p".equals(this.tag)) {
                            float f10 = iVarResolveStyle.f32850n;
                            if (f10 != Float.MAX_VALUE) {
                                builder.setShearDegrees((f10 * (-90.0f)) / 100.0f);
                            }
                            if (iVarResolveStyle.getTextAlign() != null) {
                                builder.setTextAlignment(iVarResolveStyle.getTextAlign());
                            }
                            if (iVarResolveStyle.getMultiRowAlign() != null) {
                                builder.setMultiRowAlignment(iVarResolveStyle.getMultiRowAlign());
                            }
                        }
                    }
                }
            }
            for (int i11 = 0; i11 < c(); i11++) {
                b(i11).h(j10, map, map2, str2, treeMap);
            }
        }
    }

    public final void i(long j10, boolean z10, String str, TreeMap treeMap) {
        HashMap map = this.f32809e;
        map.clear();
        HashMap map2 = this.f32810f;
        map2.clear();
        if ("metadata".equals(this.tag)) {
            return;
        }
        String str2 = this.f32808d;
        String str3 = "".equals(str2) ? str : str2;
        if (this.f32805a && z10) {
            e(str3, treeMap).append((CharSequence) Assertions.checkNotNull(this.text));
            return;
        }
        if (CmcdConfiguration.KEY_BITRATE.equals(this.tag) && z10) {
            e(str3, treeMap).append('\n');
            return;
        }
        if (f(j10)) {
            for (Map.Entry entry : treeMap.entrySet()) {
                map.put((String) entry.getKey(), Integer.valueOf(((CharSequence) Assertions.checkNotNull(((Cue.Builder) entry.getValue()).getText())).length()));
            }
            boolean zEquals = "p".equals(this.tag);
            for (int i10 = 0; i10 < c(); i10++) {
                b(i10).i(j10, z10 || zEquals, str3, treeMap);
            }
            if (zEquals) {
                SpannableStringBuilder spannableStringBuilderE = e(str3, treeMap);
                int length = spannableStringBuilderE.length() - 1;
                while (length >= 0 && spannableStringBuilderE.charAt(length) == ' ') {
                    length--;
                }
                if (length >= 0 && spannableStringBuilderE.charAt(length) != '\n') {
                    spannableStringBuilderE.append('\n');
                }
            }
            for (Map.Entry entry2 : treeMap.entrySet()) {
                map2.put((String) entry2.getKey(), Integer.valueOf(((CharSequence) Assertions.checkNotNull(((Cue.Builder) entry2.getValue()).getText())).length()));
            }
        }
    }
}
