package defpackage;

import android.os.SystemClock;
import android.util.Log;
import defpackage.am2;
import defpackage.ex7;
import defpackage.gx1;
import defpackage.lv2;
import defpackage.oq1;
import defpackage.vc7;
import defpackage.vl2;
import defpackage.wl2;
import defpackage.z76;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
class fx1<R> implements oq1.a, Runnable, Comparable<fx1<?>>, lv2.d {
    public zq1 aa;
    public nq1 ab;
    public volatile oq1 ac;
    public volatile boolean ad;
    public volatile boolean ae;
    public boolean af;
    public final e d;
    public final vc7.a e;
    public com.bumptech.glide.d j;
    public v25 k;
    public di7 l;
    public yl2 m;
    public int n;
    public int o;
    public dc2 p;
    public my6 q;
    public wl2 r;
    public int s;
    public h t;
    public g u;
    public Object v;
    public Thread w;
    public v25 x;
    public v25 y;
    public Object z;
    public final cx1 a = new cx1();
    public final ArrayList b = new ArrayList();
    public final o49 c = o49.a();
    public final d f = new d();
    public final f i = new f();

    public static /* synthetic */ class a {
        public static final /* synthetic */ int[] a;
        public static final /* synthetic */ int[] b;
        public static final /* synthetic */ int[] c;

        static {
            int[] iArr = new int[xk2.values().length];
            c = iArr;
            try {
                iArr[0] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                c[1] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            int[] iArr2 = new int[h.values().length];
            b = iArr2;
            try {
                iArr2[1] = 1;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                b[2] = 2;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                b[3] = 3;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                b[5] = 4;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                b[0] = 5;
            } catch (NoSuchFieldError unused7) {
            }
            int[] iArr3 = new int[g.values().length];
            a = iArr3;
            try {
                iArr3[0] = 1;
            } catch (NoSuchFieldError unused8) {
            }
            try {
                a[1] = 2;
            } catch (NoSuchFieldError unused9) {
            }
            try {
                a[2] = 3;
            } catch (NoSuchFieldError unused10) {
            }
        }
    }

    public interface b<R> {
    }

    public final class c<Z> implements gx1.a<Z> {
        public final zq1 a;

        public c(zq1 zq1Var) {
            this.a = zq1Var;
        }

        public final b28 a(b28 b28Var) {
            b28 b28VarA;
            et9 et9Var;
            xk2 xk2VarB;
            boolean z;
            boolean z2;
            v25 bq1Var;
            fx1 fx1Var = fx1.this;
            cx1 cx1Var = fx1Var.a;
            Class<?> cls = b28Var.get().getClass();
            zq1 zq1Var = zq1.d;
            zq1 zq1Var2 = this.a;
            k28 k28VarB = null;
            if (zq1Var2 != zq1Var) {
                et9 et9VarD = cx1Var.d(cls);
                et9Var = et9VarD;
                b28VarA = et9VarD.a(fx1Var.j, b28Var, fx1Var.n, fx1Var.o);
            } else {
                b28VarA = b28Var;
                et9Var = null;
            }
            if (!b28Var.equals(b28VarA)) {
                b28Var.c();
            }
            if (cx1Var.c.a().d.b(b28VarA.d()) != null) {
                k28VarB = cx1Var.c.a().d.b(b28VarA.d());
                if (k28VarB == null) {
                    throw new ex7.d(b28VarA.d());
                }
                xk2VarB = k28VarB.b(fx1Var.q);
            } else {
                xk2VarB = xk2.c;
            }
            k28 k28Var = k28VarB;
            v25 v25Var = fx1Var.x;
            ArrayList arrayListB = cx1Var.b();
            int size = arrayListB.size();
            int i = 0;
            while (true) {
                if (i >= size) {
                    z = false;
                    break;
                }
                if (((z76.a) arrayListB.get(i)).a.equals(v25Var)) {
                    z = true;
                    break;
                }
                i++;
            }
            if (!fx1Var.p.d(!z, zq1Var2, xk2VarB)) {
                return b28VarA;
            }
            if (k28Var == null) {
                throw new ex7.d(b28VarA.get().getClass());
            }
            int iOrdinal = xk2VarB.ordinal();
            if (iOrdinal == 0) {
                z2 = true;
                bq1Var = new bq1(fx1Var.x, fx1Var.k);
            } else {
                if (iOrdinal != 1) {
                    throw new IllegalArgumentException("Unknown strategy: " + xk2VarB);
                }
                z2 = true;
                bq1Var = new e28(cx1Var.c.a, fx1Var.x, fx1Var.k, fx1Var.n, fx1Var.o, et9Var, cls, fx1Var.q);
            }
            lg5 lg5Var = (lg5) lg5.e.acquire();
            gf7.c(lg5Var, "Argument must not be null");
            lg5Var.d = false;
            lg5Var.c = z2;
            lg5Var.b = b28VarA;
            d dVar = fx1Var.f;
            dVar.a = bq1Var;
            dVar.b = k28Var;
            dVar.c = lg5Var;
            return lg5Var;
        }
    }

    public static class d<Z> {
        public v25 a;
        public k28 b;
        public lg5 c;
    }

    public interface e {
        bc2 a();
    }

    public static class f {
        public boolean a;
        public boolean b;
        public boolean c;

        public final boolean a() {
            return (this.c || this.b) && this.a;
        }
    }

    /* JADX WARN: Failed to restore enum class, 'enum' modifier and super class removed */
    /* JADX WARN: Unknown enum class pattern. Please report as an issue! */
    public static final class g {
        public static final g a;
        public static final g b;
        public static final g c;
        public static final /* synthetic */ g[] d;

        static {
            g gVar = new g("INITIALIZE", 0);
            a = gVar;
            g gVar2 = new g("SWITCH_TO_SOURCE_SERVICE", 1);
            b = gVar2;
            g gVar3 = new g("DECODE_DATA", 2);
            c = gVar3;
            d = new g[]{gVar, gVar2, gVar3};
        }

        public static g valueOf(String str) {
            return (g) Enum.valueOf(g.class, str);
        }

        public static g[] values() {
            return (g[]) d.clone();
        }
    }

    /* JADX WARN: Failed to restore enum class, 'enum' modifier and super class removed */
    /* JADX WARN: Unknown enum class pattern. Please report as an issue! */
    public static final class h {
        public static final h a;
        public static final h b;
        public static final h c;
        public static final h d;
        public static final h e;
        public static final h f;
        public static final /* synthetic */ h[] i;

        static {
            h hVar = new h("INITIALIZE", 0);
            a = hVar;
            h hVar2 = new h("RESOURCE_CACHE", 1);
            b = hVar2;
            h hVar3 = new h("DATA_CACHE", 2);
            c = hVar3;
            h hVar4 = new h("SOURCE", 3);
            d = hVar4;
            h hVar5 = new h("ENCODE", 4);
            e = hVar5;
            h hVar6 = new h("FINISHED", 5);
            f = hVar6;
            i = new h[]{hVar, hVar2, hVar3, hVar4, hVar5, hVar6};
        }

        public static h valueOf(String str) {
            return (h) Enum.valueOf(h.class, str);
        }

        public static h[] values() {
            return (h[]) i.clone();
        }
    }

    public fx1(vl2.c cVar, vc7.a aVar) {
        this.d = cVar;
        this.e = aVar;
    }

    @Override // oq1.a
    public final void a(v25 v25Var, Exception exc, nq1 nq1Var, zq1 zq1Var) {
        nq1Var.b();
        jv3 jv3Var = new jv3("Fetching data failed", Collections.singletonList(exc));
        Class clsA = nq1Var.a();
        jv3Var.b = v25Var;
        jv3Var.c = zq1Var;
        jv3Var.d = clsA;
        this.b.add(jv3Var);
        if (Thread.currentThread() == this.w) {
            k();
            return;
        }
        this.u = g.b;
        wl2 wl2Var = this.r;
        (wl2Var.o ? wl2Var.k : wl2Var.j).execute(this);
    }

    @Override // lv2.d
    public final o49 b() {
        return this.c;
    }

    @Override // oq1.a
    public final void c(v25 v25Var, Object obj, nq1 nq1Var, zq1 zq1Var, v25 v25Var2) {
        this.x = v25Var;
        this.z = obj;
        this.ab = nq1Var;
        this.aa = zq1Var;
        this.y = v25Var2;
        this.af = v25Var != this.a.a().get(0);
        if (Thread.currentThread() == this.w) {
            f();
            return;
        }
        this.u = g.c;
        wl2 wl2Var = this.r;
        (wl2Var.o ? wl2Var.k : wl2Var.j).execute(this);
    }

    @Override // java.lang.Comparable
    public final int compareTo(fx1<?> fx1Var) {
        fx1<?> fx1Var2 = fx1Var;
        int iOrdinal = this.l.ordinal() - fx1Var2.l.ordinal();
        return iOrdinal == 0 ? this.s - fx1Var2.s : iOrdinal;
    }

    public final b28 d(nq1 nq1Var, Object obj, zq1 zq1Var) {
        if (obj == null) {
            nq1Var.b();
            return null;
        }
        try {
            int i = zg5.a;
            SystemClock.elapsedRealtimeNanos();
            b28 b28VarE = e(obj, zq1Var);
            if (Log.isLoggable("DecodeJob", 2)) {
                b28VarE.toString();
                SystemClock.elapsedRealtimeNanos();
                Objects.toString(this.m);
                Thread.currentThread().getName();
            }
            return b28VarE;
        } finally {
            nq1Var.b();
        }
    }

    public final b28 e(Object obj, zq1 zq1Var) {
        Class<?> cls = obj.getClass();
        cx1 cx1Var = this.a;
        cd5 cd5VarC = cx1Var.c(cls);
        my6 my6Var = this.q;
        boolean z = zq1Var == zq1.d || cx1Var.r;
        tx6 tx6Var = bf2.i;
        Boolean bool = (Boolean) my6Var.c(tx6Var);
        if (bool == null || (bool.booleanValue() && !z)) {
            my6Var = new my6();
            cr0 cr0Var = this.q.b;
            cr0 cr0Var2 = my6Var.b;
            cr0Var2.g(cr0Var);
            cr0Var2.put(tx6Var, Boolean.valueOf(z));
        }
        my6 my6Var2 = my6Var;
        com.bumptech.glide.load.data.a aVarC = this.j.a().c(obj);
        try {
            int i = this.n;
            int i2 = this.o;
            c cVar = new c(zq1Var);
            vc7.a aVar = cd5VarC.a;
            Object objAcquire = aVar.acquire();
            gf7.c(objAcquire, "Argument must not be null");
            List list = (List) objAcquire;
            try {
                b28 b28VarA = cd5VarC.a(aVarC, my6Var2, i, i2, cVar, list);
                aVar.a(list);
                return b28VarA;
            } catch (Throwable th) {
                aVar.a(list);
                throw th;
            }
        } finally {
            aVarC.b();
        }
    }

    public final void f() {
        b28 b28VarD;
        boolean zA;
        if (Log.isLoggable("DecodeJob", 2)) {
            String str = "data: " + this.z + ", cache key: " + this.x + ", fetcher: " + this.ab;
            int i = zg5.a;
            SystemClock.elapsedRealtimeNanos();
            Objects.toString(this.m);
            if (str != null) {
                ", ".concat(str);
            }
            Thread.currentThread().getName();
        }
        lg5 lg5Var = null;
        try {
            b28VarD = d(this.ab, this.z, this.aa);
        } catch (jv3 e2) {
            v25 v25Var = this.y;
            zq1 zq1Var = this.aa;
            e2.b = v25Var;
            e2.c = zq1Var;
            e2.d = null;
            this.b.add(e2);
            b28VarD = null;
        }
        if (b28VarD == null) {
            k();
            return;
        }
        zq1 zq1Var2 = this.aa;
        boolean z = this.af;
        if (b28VarD instanceof rl4) {
            ((rl4) b28VarD).initialize();
        }
        if (this.f.c != null) {
            lg5Var = (lg5) lg5.e.acquire();
            gf7.c(lg5Var, "Argument must not be null");
            lg5Var.d = false;
            lg5Var.c = true;
            lg5Var.b = b28VarD;
            b28VarD = lg5Var;
        }
        n();
        wl2 wl2Var = this.r;
        synchronized (wl2Var) {
            wl2Var.p = b28VarD;
            wl2Var.q = zq1Var2;
            wl2Var.x = z;
        }
        synchronized (wl2Var) {
            try {
                wl2Var.b.b();
                if (wl2Var.w) {
                    wl2Var.p.c();
                    wl2Var.f();
                } else {
                    if (wl2Var.a.a.isEmpty()) {
                        throw new IllegalStateException("Received a resource without any callbacks to notify");
                    }
                    if (wl2Var.r) {
                        throw new IllegalStateException("Already have resource");
                    }
                    wl2.c cVar = wl2Var.e;
                    b28 b28Var = wl2Var.p;
                    boolean z2 = wl2Var.n;
                    v25 v25Var2 = wl2Var.m;
                    am2.a aVar = wl2Var.c;
                    cVar.getClass();
                    wl2Var.u = new am2(b28Var, z2, true, v25Var2, aVar);
                    wl2Var.r = true;
                    wl2.e eVar = wl2Var.a;
                    eVar.getClass();
                    ArrayList<wl2.d> arrayList = new ArrayList(eVar.a);
                    wl2Var.d(arrayList.size() + 1);
                    wl2Var.f.b(wl2Var, wl2Var.m, wl2Var.u);
                    for (wl2.d dVar : arrayList) {
                        dVar.b.execute(new wl2.b(dVar.a));
                    }
                    wl2Var.c();
                }
            } finally {
            }
        }
        this.t = h.e;
        try {
            d dVar2 = this.f;
            if (dVar2.c != null) {
                e eVar2 = this.d;
                my6 my6Var = this.q;
                dVar2.getClass();
                try {
                    eVar2.a().b(dVar2.a, new cq1(dVar2.b, dVar2.c, my6Var));
                    dVar2.c.e();
                } catch (Throwable th) {
                    dVar2.c.e();
                    throw th;
                }
            }
            f fVar = this.i;
            synchronized (fVar) {
                fVar.b = true;
                zA = fVar.a();
            }
            if (zA) {
                j();
            }
        } finally {
            if (lg5Var != null) {
                lg5Var.e();
            }
        }
    }

    public final oq1 g() {
        int iOrdinal = this.t.ordinal();
        cx1 cx1Var = this.a;
        if (iOrdinal == 1) {
            return new d28(cx1Var, this);
        }
        if (iOrdinal == 2) {
            return new aq1(cx1Var.a(), cx1Var, this);
        }
        if (iOrdinal == 3) {
            return new xx8(cx1Var, this);
        }
        if (iOrdinal == 5) {
            return null;
        }
        throw new IllegalStateException("Unrecognized stage: " + this.t);
    }

    public final h h(h hVar) {
        int iOrdinal = hVar.ordinal();
        if (iOrdinal == 0) {
            boolean zB = this.p.b();
            h hVar2 = h.b;
            return zB ? hVar2 : h(hVar2);
        }
        if (iOrdinal == 1) {
            boolean zA = this.p.a();
            h hVar3 = h.c;
            return zA ? hVar3 : h(hVar3);
        }
        if (iOrdinal == 2) {
            return h.d;
        }
        if (iOrdinal == 3 || iOrdinal == 5) {
            return h.f;
        }
        throw new IllegalArgumentException("Unrecognized stage: " + hVar);
    }

    public final void i() {
        boolean zA;
        n();
        jv3 jv3Var = new jv3("Failed to load resource", new ArrayList(this.b));
        wl2 wl2Var = this.r;
        synchronized (wl2Var) {
            wl2Var.s = jv3Var;
        }
        synchronized (wl2Var) {
            try {
                wl2Var.b.b();
                if (wl2Var.w) {
                    wl2Var.f();
                } else {
                    if (wl2Var.a.a.isEmpty()) {
                        throw new IllegalStateException("Received an exception without any callbacks to notify");
                    }
                    if (wl2Var.t) {
                        throw new IllegalStateException("Already failed once");
                    }
                    wl2Var.t = true;
                    v25 v25Var = wl2Var.m;
                    wl2.e eVar = wl2Var.a;
                    eVar.getClass();
                    ArrayList<wl2.d> arrayList = new ArrayList(eVar.a);
                    wl2Var.d(arrayList.size() + 1);
                    wl2Var.f.b(wl2Var, v25Var, null);
                    for (wl2.d dVar : arrayList) {
                        dVar.b.execute(new wl2.a(dVar.a));
                    }
                    wl2Var.c();
                }
            } finally {
            }
        }
        f fVar = this.i;
        synchronized (fVar) {
            fVar.c = true;
            zA = fVar.a();
        }
        if (zA) {
            j();
        }
    }

    public final void j() {
        f fVar = this.i;
        synchronized (fVar) {
            fVar.b = false;
            fVar.a = false;
            fVar.c = false;
        }
        d dVar = this.f;
        dVar.a = null;
        dVar.b = null;
        dVar.c = null;
        cx1 cx1Var = this.a;
        cx1Var.c = null;
        cx1Var.d = null;
        cx1Var.n = null;
        cx1Var.g = null;
        cx1Var.k = null;
        cx1Var.i = null;
        cx1Var.o = null;
        cx1Var.j = null;
        cx1Var.p = null;
        cx1Var.a.clear();
        cx1Var.l = false;
        cx1Var.b.clear();
        cx1Var.m = false;
        this.ad = false;
        this.j = null;
        this.k = null;
        this.q = null;
        this.l = null;
        this.m = null;
        this.r = null;
        this.t = null;
        this.ac = null;
        this.w = null;
        this.x = null;
        this.z = null;
        this.aa = null;
        this.ab = null;
        this.ae = false;
        this.v = null;
        this.b.clear();
        this.e.a(this);
    }

    public final void k() {
        this.w = Thread.currentThread();
        int i = zg5.a;
        SystemClock.elapsedRealtimeNanos();
        boolean zB = false;
        while (!this.ae && this.ac != null && !(zB = this.ac.b())) {
            this.t = h(this.t);
            this.ac = g();
            if (this.t == h.d) {
                this.u = g.b;
                wl2 wl2Var = this.r;
                (wl2Var.o ? wl2Var.k : wl2Var.j).execute(this);
                return;
            }
        }
        if ((this.t == h.f || this.ae) && !zB) {
            i();
        }
    }

    public final void m() {
        int iOrdinal = this.u.ordinal();
        if (iOrdinal == 0) {
            this.t = h(h.a);
            this.ac = g();
            k();
        } else if (iOrdinal == 1) {
            k();
        } else if (iOrdinal == 2) {
            f();
        } else {
            throw new IllegalStateException("Unrecognized run reason: " + this.u);
        }
    }

    public final void n() {
        Throwable th;
        this.c.b();
        if (!this.ad) {
            this.ad = true;
            return;
        }
        if (this.b.isEmpty()) {
            th = null;
        } else {
            ArrayList arrayList = this.b;
            th = (Throwable) arrayList.get(arrayList.size() - 1);
        }
        throw new IllegalStateException("Already notified", th);
    }

    @Override // java.lang.Runnable
    public final void run() {
        nq1 nq1Var = this.ab;
        try {
            try {
                if (this.ae) {
                    i();
                    if (nq1Var != null) {
                        nq1Var.b();
                        return;
                    }
                    return;
                }
                m();
                if (nq1Var != null) {
                    nq1Var.b();
                }
            } catch (Throwable th) {
                if (nq1Var != null) {
                    nq1Var.b();
                }
                throw th;
            }
        } catch (rr0 e2) {
            throw e2;
        } catch (Throwable th2) {
            if (Log.isLoggable("DecodeJob", 3)) {
                Objects.toString(this.t);
            }
            if (this.t != h.e) {
                this.b.add(th2);
                i();
            }
            if (!this.ae) {
                throw th2;
            }
            throw th2;
        }
    }
}
