package lh;

import android.util.Log;
import com.bumptech.glide.i;
import fi.a;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import lh.f;
import lh.i;

/* loaded from: classes4.dex */
class h implements f.a, Runnable, Comparable, a.f {
    private int A;
    private int B;
    private j C;
    private jh.h D;
    private b E;
    private int F;
    private EnumC0630h G;
    private g H;
    private long I;
    private boolean J;
    private Object K;
    private Thread L;
    private jh.f M;
    private jh.f N;
    private Object O;
    private jh.a P;
    private com.bumptech.glide.load.data.d Q;
    private volatile lh.f R;
    private volatile boolean S;
    private volatile boolean T;
    private boolean U;

    /* renamed from: s, reason: collision with root package name */
    private final e f28144s;

    /* renamed from: t, reason: collision with root package name */
    private final s3.f f28145t;

    /* renamed from: w, reason: collision with root package name */
    private com.bumptech.glide.d f28148w;

    /* renamed from: x, reason: collision with root package name */
    private jh.f f28149x;

    /* renamed from: y, reason: collision with root package name */
    private com.bumptech.glide.g f28150y;

    /* renamed from: z, reason: collision with root package name */
    private n f28151z;

    /* renamed from: p, reason: collision with root package name */
    private final lh.g f28141p = new lh.g();

    /* renamed from: q, reason: collision with root package name */
    private final List f28142q = new ArrayList();

    /* renamed from: r, reason: collision with root package name */
    private final fi.c f28143r = fi.c.a();

    /* renamed from: u, reason: collision with root package name */
    private final d f28146u = new d();

    /* renamed from: v, reason: collision with root package name */
    private final f f28147v = new f();

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static /* synthetic */ class a {

        /* renamed from: a, reason: collision with root package name */
        static final /* synthetic */ int[] f28152a;

        /* renamed from: b, reason: collision with root package name */
        static final /* synthetic */ int[] f28153b;

        /* renamed from: c, reason: collision with root package name */
        static final /* synthetic */ int[] f28154c;

        static {
            int[] iArr = new int[jh.c.values().length];
            f28154c = iArr;
            try {
                iArr[jh.c.SOURCE.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                f28154c[jh.c.TRANSFORMED.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            int[] iArr2 = new int[EnumC0630h.values().length];
            f28153b = iArr2;
            try {
                iArr2[EnumC0630h.RESOURCE_CACHE.ordinal()] = 1;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                f28153b[EnumC0630h.DATA_CACHE.ordinal()] = 2;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                f28153b[EnumC0630h.SOURCE.ordinal()] = 3;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                f28153b[EnumC0630h.FINISHED.ordinal()] = 4;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                f28153b[EnumC0630h.INITIALIZE.ordinal()] = 5;
            } catch (NoSuchFieldError unused7) {
            }
            int[] iArr3 = new int[g.values().length];
            f28152a = iArr3;
            try {
                iArr3[g.INITIALIZE.ordinal()] = 1;
            } catch (NoSuchFieldError unused8) {
            }
            try {
                f28152a[g.SWITCH_TO_SOURCE_SERVICE.ordinal()] = 2;
            } catch (NoSuchFieldError unused9) {
            }
            try {
                f28152a[g.DECODE_DATA.ordinal()] = 3;
            } catch (NoSuchFieldError unused10) {
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public interface b {
        void a(v vVar, jh.a aVar, boolean z10);

        void b(h hVar);

        void c(q qVar);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes4.dex */
    public final class c implements i.a {

        /* renamed from: a, reason: collision with root package name */
        private final jh.a f28155a;

        c(jh.a aVar) {
            this.f28155a = aVar;
        }

        @Override // lh.i.a
        public v a(v vVar) {
            return h.this.I(this.f28155a, vVar);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes4.dex */
    public static class d {

        /* renamed from: a, reason: collision with root package name */
        private jh.f f28157a;

        /* renamed from: b, reason: collision with root package name */
        private jh.k f28158b;

        /* renamed from: c, reason: collision with root package name */
        private u f28159c;

        d() {
        }

        void a() {
            this.f28157a = null;
            this.f28158b = null;
            this.f28159c = null;
        }

        void b(e eVar, jh.h hVar) {
            fi.b.a("DecodeJob.encode");
            try {
                eVar.a().a(this.f28157a, new lh.e(this.f28158b, this.f28159c, hVar));
            } finally {
                this.f28159c.g();
                fi.b.e();
            }
        }

        boolean c() {
            return this.f28159c != null;
        }

        void d(jh.f fVar, jh.k kVar, u uVar) {
            this.f28157a = fVar;
            this.f28158b = kVar;
            this.f28159c = uVar;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public interface e {
        nh.a a();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes4.dex */
    public static class f {

        /* renamed from: a, reason: collision with root package name */
        private boolean f28160a;

        /* renamed from: b, reason: collision with root package name */
        private boolean f28161b;

        /* renamed from: c, reason: collision with root package name */
        private boolean f28162c;

        f() {
        }

        private boolean a(boolean z10) {
            return (this.f28162c || z10 || this.f28161b) && this.f28160a;
        }

        synchronized boolean b() {
            this.f28161b = true;
            return a(false);
        }

        synchronized boolean c() {
            this.f28162c = true;
            return a(false);
        }

        synchronized boolean d(boolean z10) {
            this.f28160a = true;
            return a(z10);
        }

        synchronized void e() {
            this.f28161b = false;
            this.f28160a = false;
            this.f28162c = false;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes4.dex */
    public enum g {
        INITIALIZE,
        SWITCH_TO_SOURCE_SERVICE,
        DECODE_DATA
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* renamed from: lh.h$h, reason: collision with other inner class name */
    /* loaded from: classes4.dex */
    public enum EnumC0630h {
        INITIALIZE,
        RESOURCE_CACHE,
        DATA_CACHE,
        SOURCE,
        ENCODE,
        FINISHED
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public h(e eVar, s3.f fVar) {
        this.f28144s = eVar;
        this.f28145t = fVar;
    }

    private void B(String str, long j10) {
        C(str, j10, null);
    }

    private void C(String str, long j10, String str2) {
        String str3;
        StringBuilder sb2 = new StringBuilder();
        sb2.append(str);
        sb2.append(" in ");
        sb2.append(ei.g.a(j10));
        sb2.append(", load key: ");
        sb2.append(this.f28151z);
        if (str2 != null) {
            str3 = ", " + str2;
        } else {
            str3 = "";
        }
        sb2.append(str3);
        sb2.append(", thread: ");
        sb2.append(Thread.currentThread().getName());
        Log.v("DecodeJob", sb2.toString());
    }

    private void D(v vVar, jh.a aVar, boolean z10) {
        P();
        this.E.a(vVar, aVar, z10);
    }

    /* JADX WARN: Multi-variable type inference failed */
    private void E(v vVar, jh.a aVar, boolean z10) {
        u uVar;
        fi.b.a("DecodeJob.notifyEncodeAndRelease");
        try {
            if (vVar instanceof r) {
                ((r) vVar).b();
            }
            if (this.f28146u.c()) {
                vVar = u.e(vVar);
                uVar = vVar;
            } else {
                uVar = 0;
            }
            D(vVar, aVar, z10);
            this.G = EnumC0630h.ENCODE;
            try {
                if (this.f28146u.c()) {
                    this.f28146u.b(this.f28144s, this.D);
                }
                G();
            } finally {
                if (uVar != 0) {
                    uVar.g();
                }
            }
        } finally {
            fi.b.e();
        }
    }

    private void F() {
        P();
        this.E.c(new q("Failed to load resource", new ArrayList(this.f28142q)));
        H();
    }

    private void G() {
        if (this.f28147v.b()) {
            K();
        }
    }

    private void H() {
        if (this.f28147v.c()) {
            K();
        }
    }

    private void K() {
        this.f28147v.e();
        this.f28146u.a();
        this.f28141p.a();
        this.S = false;
        this.f28148w = null;
        this.f28149x = null;
        this.D = null;
        this.f28150y = null;
        this.f28151z = null;
        this.E = null;
        this.G = null;
        this.R = null;
        this.L = null;
        this.M = null;
        this.O = null;
        this.P = null;
        this.Q = null;
        this.I = 0L;
        this.T = false;
        this.K = null;
        this.f28142q.clear();
        this.f28145t.a(this);
    }

    private void L(g gVar) {
        this.H = gVar;
        this.E.b(this);
    }

    private void M() {
        this.L = Thread.currentThread();
        this.I = ei.g.b();
        boolean z10 = false;
        while (!this.T && this.R != null && !(z10 = this.R.b())) {
            this.G = x(this.G);
            this.R = w();
            if (this.G == EnumC0630h.SOURCE) {
                L(g.SWITCH_TO_SOURCE_SERVICE);
                return;
            }
        }
        if ((this.G == EnumC0630h.FINISHED || this.T) && !z10) {
            F();
        }
    }

    private v N(Object obj, jh.a aVar, t tVar) {
        jh.h y10 = y(aVar);
        com.bumptech.glide.load.data.e l10 = this.f28148w.i().l(obj);
        try {
            return tVar.a(l10, y10, this.A, this.B, new c(aVar));
        } finally {
            l10.b();
        }
    }

    private void O() {
        int i10 = a.f28152a[this.H.ordinal()];
        if (i10 == 1) {
            this.G = x(EnumC0630h.INITIALIZE);
            this.R = w();
            M();
        } else if (i10 == 2) {
            M();
        } else {
            if (i10 == 3) {
                v();
                return;
            }
            throw new IllegalStateException("Unrecognized run reason: " + this.H);
        }
    }

    private void P() {
        Throwable th2;
        this.f28143r.c();
        if (!this.S) {
            this.S = true;
            return;
        }
        if (this.f28142q.isEmpty()) {
            th2 = null;
        } else {
            List list = this.f28142q;
            th2 = (Throwable) list.get(list.size() - 1);
        }
        throw new IllegalStateException("Already notified", th2);
    }

    private v t(com.bumptech.glide.load.data.d dVar, Object obj, jh.a aVar) {
        if (obj == null) {
            dVar.b();
            return null;
        }
        try {
            long b10 = ei.g.b();
            v u10 = u(obj, aVar);
            if (Log.isLoggable("DecodeJob", 2)) {
                B("Decoded result " + u10, b10);
            }
            return u10;
        } finally {
            dVar.b();
        }
    }

    private v u(Object obj, jh.a aVar) {
        return N(obj, aVar, this.f28141p.h(obj.getClass()));
    }

    private void v() {
        v vVar;
        if (Log.isLoggable("DecodeJob", 2)) {
            C("Retrieved data", this.I, "data: " + this.O + ", cache key: " + this.M + ", fetcher: " + this.Q);
        }
        try {
            vVar = t(this.Q, this.O, this.P);
        } catch (q e10) {
            e10.i(this.N, this.P);
            this.f28142q.add(e10);
            vVar = null;
        }
        if (vVar != null) {
            E(vVar, this.P, this.U);
        } else {
            M();
        }
    }

    private lh.f w() {
        int i10 = a.f28153b[this.G.ordinal()];
        if (i10 == 1) {
            return new w(this.f28141p, this);
        }
        if (i10 == 2) {
            return new lh.c(this.f28141p, this);
        }
        if (i10 == 3) {
            return new z(this.f28141p, this);
        }
        if (i10 == 4) {
            return null;
        }
        throw new IllegalStateException("Unrecognized stage: " + this.G);
    }

    private EnumC0630h x(EnumC0630h enumC0630h) {
        int i10 = a.f28153b[enumC0630h.ordinal()];
        if (i10 == 1) {
            return this.C.a() ? EnumC0630h.DATA_CACHE : x(EnumC0630h.DATA_CACHE);
        }
        if (i10 == 2) {
            return this.J ? EnumC0630h.FINISHED : EnumC0630h.SOURCE;
        }
        if (i10 == 3 || i10 == 4) {
            return EnumC0630h.FINISHED;
        }
        if (i10 == 5) {
            return this.C.b() ? EnumC0630h.RESOURCE_CACHE : x(EnumC0630h.RESOURCE_CACHE);
        }
        throw new IllegalArgumentException("Unrecognized stage: " + enumC0630h);
    }

    private jh.h y(jh.a aVar) {
        jh.h hVar = this.D;
        boolean z10 = aVar == jh.a.RESOURCE_DISK_CACHE || this.f28141p.x();
        jh.g gVar = sh.m.f35737j;
        Boolean bool = (Boolean) hVar.c(gVar);
        if (bool != null && (!bool.booleanValue() || z10)) {
            return hVar;
        }
        jh.h hVar2 = new jh.h();
        hVar2.d(this.D);
        hVar2.e(gVar, Boolean.valueOf(z10));
        return hVar2;
    }

    private int z() {
        return this.f28150y.ordinal();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public h A(com.bumptech.glide.d dVar, Object obj, n nVar, jh.f fVar, int i10, int i11, Class cls, Class cls2, com.bumptech.glide.g gVar, j jVar, Map map, boolean z10, boolean z11, boolean z12, jh.h hVar, b bVar, int i12) {
        this.f28141p.v(dVar, obj, fVar, i10, i11, jVar, cls, cls2, gVar, hVar, map, z10, z11, this.f28144s);
        this.f28148w = dVar;
        this.f28149x = fVar;
        this.f28150y = gVar;
        this.f28151z = nVar;
        this.A = i10;
        this.B = i11;
        this.C = jVar;
        this.J = z12;
        this.D = hVar;
        this.E = bVar;
        this.F = i12;
        this.H = g.INITIALIZE;
        this.K = obj;
        return this;
    }

    v I(jh.a aVar, v vVar) {
        v vVar2;
        jh.l lVar;
        jh.c cVar;
        jh.f dVar;
        Class<?> cls = vVar.get().getClass();
        jh.k kVar = null;
        if (aVar != jh.a.RESOURCE_DISK_CACHE) {
            jh.l s10 = this.f28141p.s(cls);
            lVar = s10;
            vVar2 = s10.b(this.f28148w, vVar, this.A, this.B);
        } else {
            vVar2 = vVar;
            lVar = null;
        }
        if (!vVar.equals(vVar2)) {
            vVar.c();
        }
        if (this.f28141p.w(vVar2)) {
            kVar = this.f28141p.n(vVar2);
            cVar = kVar.b(this.D);
        } else {
            cVar = jh.c.NONE;
        }
        jh.k kVar2 = kVar;
        if (!this.C.d(!this.f28141p.y(this.M), aVar, cVar)) {
            return vVar2;
        }
        if (kVar2 == null) {
            throw new i.d(vVar2.get().getClass());
        }
        int i10 = a.f28154c[cVar.ordinal()];
        if (i10 == 1) {
            dVar = new lh.d(this.M, this.f28149x);
        } else {
            if (i10 != 2) {
                throw new IllegalArgumentException("Unknown strategy: " + cVar);
            }
            dVar = new x(this.f28141p.b(), this.M, this.f28149x, this.A, this.B, lVar, cls, this.D);
        }
        u e10 = u.e(vVar2);
        this.f28146u.d(dVar, kVar2, e10);
        return e10;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void J(boolean z10) {
        if (this.f28147v.d(z10)) {
            K();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean Q() {
        EnumC0630h x10 = x(EnumC0630h.INITIALIZE);
        return x10 == EnumC0630h.RESOURCE_CACHE || x10 == EnumC0630h.DATA_CACHE;
    }

    @Override // lh.f.a
    public void a(jh.f fVar, Exception exc, com.bumptech.glide.load.data.d dVar, jh.a aVar) {
        dVar.b();
        q qVar = new q("Fetching data failed", exc);
        qVar.j(fVar, aVar, dVar.a());
        this.f28142q.add(qVar);
        if (Thread.currentThread() != this.L) {
            L(g.SWITCH_TO_SOURCE_SERVICE);
        } else {
            M();
        }
    }

    @Override // lh.f.a
    public void e() {
        L(g.SWITCH_TO_SOURCE_SERVICE);
    }

    @Override // lh.f.a
    public void g(jh.f fVar, Object obj, com.bumptech.glide.load.data.d dVar, jh.a aVar, jh.f fVar2) {
        this.M = fVar;
        this.O = obj;
        this.Q = dVar;
        this.P = aVar;
        this.N = fVar2;
        this.U = fVar != this.f28141p.c().get(0);
        if (Thread.currentThread() != this.L) {
            L(g.DECODE_DATA);
            return;
        }
        fi.b.a("DecodeJob.decodeFromRetrievedData");
        try {
            v();
        } finally {
            fi.b.e();
        }
    }

    @Override // fi.a.f
    public fi.c m() {
        return this.f28143r;
    }

    public void r() {
        this.T = true;
        lh.f fVar = this.R;
        if (fVar != null) {
            fVar.cancel();
        }
    }

    @Override // java.lang.Runnable
    public void run() {
        fi.b.c("DecodeJob#run(reason=%s, model=%s)", this.H, this.K);
        com.bumptech.glide.load.data.d dVar = this.Q;
        try {
            try {
                if (this.T) {
                    F();
                    return;
                }
                O();
                if (dVar != null) {
                    dVar.b();
                }
                fi.b.e();
            } finally {
                if (dVar != null) {
                    dVar.b();
                }
                fi.b.e();
            }
        } catch (lh.b e10) {
            throw e10;
        } catch (Throwable th2) {
            if (Log.isLoggable("DecodeJob", 3)) {
                Log.d("DecodeJob", "DecodeJob threw unexpectedly, isCancelled: " + this.T + ", stage: " + this.G, th2);
            }
            if (this.G != EnumC0630h.ENCODE) {
                this.f28142q.add(th2);
                F();
            }
            if (!this.T) {
                throw th2;
            }
            throw th2;
        }
    }

    @Override // java.lang.Comparable
    /* renamed from: s, reason: merged with bridge method [inline-methods] */
    public int compareTo(h hVar) {
        int z10 = z() - hVar.z();
        return z10 == 0 ? this.F - hVar.F : z10;
    }
}
