package C0;

import B0.r;
import L0.C;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import u0.P;
import u0.Q;
import u0.S;
import x0.v;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final r f1115h = new r(2);
    public static final Random i = new Random();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public k f1119d;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public String f1121f;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Q f1116a = new Q();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final P f1117b = new P();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final HashMap f1118c = new HashMap();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public S f1120e = S.f17199a;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public long f1122g = -1;

    public final void a(g gVar) {
        long j8 = gVar.f1110c;
        if (j8 != -1) {
            this.f1122g = j8;
        }
        this.f1121f = null;
    }

    public final synchronized void b(a aVar) {
        k kVar;
        try {
            String str = this.f1121f;
            if (str != null) {
                g gVar = (g) this.f1118c.get(str);
                gVar.getClass();
                a(gVar);
            }
            Iterator it = this.f1118c.values().iterator();
            while (it.hasNext()) {
                g gVar2 = (g) it.next();
                it.remove();
                if (gVar2.f1112e && (kVar = this.f1119d) != null) {
                    kVar.d(aVar, gVar2.f1108a);
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x0045  */
    /* JADX WARN: Removed duplicated region for block: B:39:0x0087  */
    /* JADX WARN: Removed duplicated region for block: B:54:0x0099 A[ADDED_TO_REGION, REMOVE, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final g c(int i5, C c3) {
        long j8;
        long j9;
        HashMap map = this.f1118c;
        g gVar = null;
        long j10 = Long.MAX_VALUE;
        for (g gVar2 : map.values()) {
            if (gVar2.f1110c == -1 && i5 == gVar2.f1109b && c3 != null) {
                h hVar = gVar2.f1114g;
                g gVar3 = (g) hVar.f1118c.get(hVar.f1121f);
                if (gVar3 != null) {
                    long j11 = gVar3.f1110c;
                    if (j11 == -1) {
                        j11 = 1 + hVar.f1122g;
                    }
                    long j12 = c3.f3628d;
                    if (j12 >= j11) {
                        gVar2.f1110c = j12;
                    }
                }
            }
            C c8 = gVar2.f1111d;
            if (c3 != null) {
                long j13 = c3.f3628d;
                if (c8 != null) {
                    j8 = -1;
                    if (j13 == c8.f3628d && c3.f3626b == c8.f3626b && c3.f3627c == c8.f3627c) {
                        j9 = gVar2.f1110c;
                        if (j9 == j8) {
                        }
                        gVar = gVar2;
                        j10 = j9;
                    }
                } else if (!c3.b()) {
                    j8 = -1;
                    if (j13 == gVar2.f1110c) {
                        j9 = gVar2.f1110c;
                        if (j9 == j8) {
                        }
                        gVar = gVar2;
                        j10 = j9;
                    }
                }
            } else if (i5 == gVar2.f1109b) {
                j8 = -1;
                j9 = gVar2.f1110c;
                if (j9 == j8 || j9 < j10) {
                    gVar = gVar2;
                    j10 = j9;
                } else if (j9 == j10) {
                    int i8 = v.f18553a;
                    if (gVar.f1111d != null && c8 != null) {
                        gVar = gVar2;
                    }
                }
            }
        }
        if (gVar != null) {
            return gVar;
        }
        String str = (String) f1115h.get();
        g gVar4 = new g(this, str, i5, c3);
        map.put(str, gVar4);
        return gVar4;
    }

    public final synchronized String d(S s8, C c3) {
        return c(s8.g(c3.f3625a, this.f1117b).f17177c, c3).f1108a;
    }

    public final void e(a aVar) {
        C c3;
        boolean zP = aVar.f1083b.p();
        HashMap map = this.f1118c;
        if (zP) {
            String str = this.f1121f;
            if (str != null) {
                g gVar = (g) map.get(str);
                gVar.getClass();
                a(gVar);
                return;
            }
            return;
        }
        g gVar2 = (g) map.get(this.f1121f);
        int i5 = aVar.f1084c;
        C c8 = aVar.f1085d;
        this.f1121f = c(i5, c8).f1108a;
        f(aVar);
        if (c8 == null || !c8.b()) {
            return;
        }
        long j8 = c8.f3628d;
        if (gVar2 != null && gVar2.f1110c == j8 && (c3 = gVar2.f1111d) != null && c3.f3626b == c8.f3626b && c3.f3627c == c8.f3627c) {
            return;
        }
        c(i5, new C(j8, c8.f3625a));
        this.f1119d.getClass();
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x002b A[Catch: all -> 0x0050, TRY_LEAVE, TryCatch #0 {, blocks: (B:3:0x0001, B:7:0x0010, B:9:0x0014, B:11:0x0024, B:20:0x0036, B:22:0x0042, B:24:0x0048, B:14:0x002b, B:30:0x0053, B:32:0x005f, B:33:0x0063, B:35:0x0068, B:37:0x006e, B:39:0x0085, B:40:0x00b2, B:42:0x00b6, B:43:0x00bd, B:45:0x00c7, B:47:0x00cb, B:49:0x00d8, B:52:0x00df), top: B:57:0x0001 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final synchronized void f(a aVar) {
        this.f1119d.getClass();
        if (aVar.f1083b.p()) {
            return;
        }
        C c3 = aVar.f1085d;
        if (c3 != null) {
            long j8 = c3.f3628d;
            g gVar = (g) this.f1118c.get(this.f1121f);
            if (gVar != null) {
                long j9 = gVar.f1110c;
                if (j9 == -1) {
                    j9 = this.f1122g + 1;
                }
                if (j8 < j9) {
                    return;
                }
                g gVar2 = (g) this.f1118c.get(this.f1121f);
                if (gVar2 != null && gVar2.f1110c == -1 && gVar2.f1109b != aVar.f1084c) {
                    return;
                }
            }
        }
        g gVarC = c(aVar.f1084c, aVar.f1085d);
        if (this.f1121f == null) {
            this.f1121f = gVarC.f1108a;
        }
        C c8 = aVar.f1085d;
        if (c8 != null && c8.b()) {
            C c9 = aVar.f1085d;
            Object obj = c9.f3625a;
            g gVarC2 = c(aVar.f1084c, new C(c9.f3626b, c9.f3628d, obj));
            if (!gVarC2.f1112e) {
                gVarC2.f1112e = true;
                aVar.f1083b.g(aVar.f1085d.f3625a, this.f1117b);
                this.f1117b.d(aVar.f1085d.f3626b);
                Math.max(0L, v.W(0L) + v.W(this.f1117b.f17179e));
                this.f1119d.getClass();
            }
        }
        if (!gVarC.f1112e) {
            gVarC.f1112e = true;
            this.f1119d.getClass();
        }
        if (gVarC.f1108a.equals(this.f1121f) && !gVarC.f1113f) {
            gVarC.f1113f = true;
            k kVar = this.f1119d;
            String str = gVarC.f1108a;
            kVar.getClass();
            C c10 = aVar.f1085d;
            if (c10 == null || !c10.b()) {
                kVar.b();
                kVar.f1133j = str;
                kVar.f1134k = j.g().setPlayerName("AndroidXMedia3").setPlayerVersion("1.7.1");
                kVar.c(aVar.f1083b, aVar.f1085d);
            }
        }
    }

    public final synchronized void g(a aVar, int i5) {
        try {
            this.f1119d.getClass();
            boolean z8 = i5 == 0;
            Iterator it = this.f1118c.values().iterator();
            while (it.hasNext()) {
                g gVar = (g) it.next();
                if (gVar.a(aVar)) {
                    it.remove();
                    if (gVar.f1112e) {
                        boolean zEquals = gVar.f1108a.equals(this.f1121f);
                        if (z8 && zEquals) {
                            boolean z9 = gVar.f1113f;
                        }
                        if (zEquals) {
                            a(gVar);
                        }
                        this.f1119d.d(aVar, gVar.f1108a);
                    }
                }
            }
            e(aVar);
        } catch (Throwable th) {
            throw th;
        }
    }
}
