package com.google.android.play.core.assetpacks;

import X.0HH;
import com.bytedance.covode.number.Covode;
import com.google.android.play.core.b.b;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public final class bp {
    public static final b LIZ = new b("ExtractorTaskFinder");
    public final bm LIZIZ;
    public final ac LIZJ;
    public final aj LIZLLL;

    static {
        Covode.recordClassIndex(44149);
    }

    public bp(bm bmVar, ac acVar, aj ajVar) {
        this.LIZIZ = bmVar;
        this.LIZJ = acVar;
        this.LIZLLL = ajVar;
    }

    private final boolean LIZ(bj bjVar, bk bkVar) {
        ac acVar = this.LIZJ;
        bi biVar = bjVar.LIZJ;
        return new cn(acVar, biVar.LIZ, bjVar.LIZIZ, biVar.LIZIZ, bkVar.LIZ).LIZLLL();
    }

    public static boolean LIZ(bk bkVar) {
        int i = bkVar.LJFF;
        return i == 1 || i == 2;
    }

    public final bo LIZ() {
        int i;
        try {
            this.LIZIZ.LJFF.lock();
            ArrayList<bj> arrayList = new ArrayList<>();
            for (bj next : this.LIZIZ.LJ.values()) {
                if (bw.LIZJ(next.LIZJ.LIZJ)) {
                    arrayList.add(next);
                }
            }
            if (!arrayList.isEmpty()) {
                for (bj bjVar : arrayList) {
                    ac acVar = this.LIZJ;
                    bi biVar = bjVar.LIZJ;
                    if (acVar.LJ(biVar.LIZ, bjVar.LIZIZ, biVar.LIZIZ) == bjVar.LIZJ.LJ.size()) {
                        LIZ.LIZ("Found final move task for session %s with pack %s.", Integer.valueOf(bjVar.LIZ), bjVar.LIZJ.LIZ);
                        int i2 = bjVar.LIZ;
                        bi biVar2 = bjVar.LIZJ;
                        cc ccVar = new cc(i2, biVar2.LIZ, bjVar.LIZIZ, biVar2.LIZIZ);
                        this.LIZIZ.LJFF.unlock();
                        return ccVar;
                    }
                }
                for (bj bjVar2 : arrayList) {
                    if (bw.LIZJ(bjVar2.LIZJ.LIZJ)) {
                        for (bk next2 : bjVar2.LIZJ.LJ) {
                            ac acVar2 = this.LIZJ;
                            bi biVar3 = bjVar2.LIZJ;
                            if (acVar2.LIZIZ(biVar3.LIZ, bjVar2.LIZIZ, biVar3.LIZIZ, next2.LIZ).exists()) {
                                LIZ.LIZ("Found merge task for session %s with pack %s and slice %s.", Integer.valueOf(bjVar2.LIZ), bjVar2.LIZJ.LIZ, next2.LIZ);
                                int i3 = bjVar2.LIZ;
                                bi biVar4 = bjVar2.LIZJ;
                                bz bzVar = new bz(i3, biVar4.LIZ, bjVar2.LIZIZ, biVar4.LIZIZ, next2.LIZ);
                                this.LIZIZ.LJFF.unlock();
                                return bzVar;
                            }
                        }
                        continue;
                    }
                }
                for (bj bjVar3 : arrayList) {
                    if (bw.LIZJ(bjVar3.LIZJ.LIZJ)) {
                        for (bk next3 : bjVar3.LIZJ.LJ) {
                            if (LIZ(bjVar3, next3)) {
                                ac acVar3 = this.LIZJ;
                                bi biVar5 = bjVar3.LIZJ;
                                if (acVar3.LIZ(biVar5.LIZ, bjVar3.LIZIZ, biVar5.LIZIZ, next3.LIZ).exists()) {
                                    LIZ.LIZ("Found verify task for session %s with pack %s and slice %s.", Integer.valueOf(bjVar3.LIZ), bjVar3.LIZJ.LIZ, next3.LIZ);
                                    int i4 = bjVar3.LIZ;
                                    bi biVar6 = bjVar3.LIZJ;
                                    cq cqVar = new cq(i4, biVar6.LIZ, bjVar3.LIZIZ, biVar6.LIZIZ, next3.LIZ, next3.LIZIZ);
                                    this.LIZIZ.LJFF.unlock();
                                    return cqVar;
                                }
                            }
                        }
                        continue;
                    }
                }
                for (bj bjVar4 : arrayList) {
                    if (bw.LIZJ(bjVar4.LIZJ.LIZJ)) {
                        for (bk next4 : bjVar4.LIZJ.LJ) {
                            if (!LIZ(next4)) {
                                ac acVar4 = this.LIZJ;
                                bi biVar7 = bjVar4.LIZJ;
                                try {
                                    i = new cn(acVar4, biVar7.LIZ, bjVar4.LIZIZ, biVar7.LIZIZ, next4.LIZ).LIZJ();
                                    if (i == -1) {
                                        continue;
                                    } else if (next4.LIZLLL.get(i).LIZ) {
                                        LIZ.LIZ("Found extraction task using compression format %s for session %s, pack %s, slice %s, chunk %s.", Integer.valueOf(next4.LJ), Integer.valueOf(bjVar4.LIZ), bjVar4.LIZJ.LIZ, next4.LIZ, Integer.valueOf(i));
                                        InputStream LIZ2 = this.LIZLLL.LIZ(bjVar4.LIZ, bjVar4.LIZJ.LIZ, next4.LIZ, i);
                                        int i5 = bjVar4.LIZ;
                                        bi biVar8 = bjVar4.LIZJ;
                                        String str = biVar8.LIZ;
                                        int i6 = bjVar4.LIZIZ;
                                        long j = biVar8.LIZIZ;
                                        String str2 = next4.LIZ;
                                        int i7 = next4.LJ;
                                        int size = next4.LIZLLL.size();
                                        bi biVar9 = bjVar4.LIZJ;
                                        ar arVar = new ar(i5, str, i6, j, str2, i7, i, size, biVar9.LIZLLL, biVar9.LIZJ, LIZ2);
                                        this.LIZIZ.LJFF.unlock();
                                        return arVar;
                                    }
                                } catch (IOException e) {
                                    LIZ.LIZIZ("Slice checkpoint corrupt, restarting extraction. %s", e);
                                    i = 0;
                                }
                            }
                        }
                        continue;
                    }
                }
                for (bj bjVar5 : arrayList) {
                    if (bw.LIZJ(bjVar5.LIZJ.LIZJ)) {
                        for (bk next5 : bjVar5.LIZJ.LJ) {
                            if (LIZ(next5) && next5.LIZLLL.get(0).LIZ && !LIZ(bjVar5, next5)) {
                                LIZ.LIZ("Found patch slice task using patch format %s for session %s, pack %s, slice %s.", Integer.valueOf(next5.LJFF), Integer.valueOf(bjVar5.LIZ), bjVar5.LIZJ.LIZ, next5.LIZ);
                                InputStream LIZ3 = this.LIZLLL.LIZ(bjVar5.LIZ, bjVar5.LIZJ.LIZ, next5.LIZ, 0);
                                int i8 = bjVar5.LIZ;
                                String str3 = bjVar5.LIZJ.LIZ;
                                cj cjVar = new cj(i8, str3, this.LIZJ.LIZIZ(str3), this.LIZJ.LIZJ(bjVar5.LIZJ.LIZ), bjVar5.LIZIZ, bjVar5.LIZJ.LIZIZ, next5.LJFF, next5.LIZ, next5.LIZJ, LIZ3);
                                this.LIZIZ.LJFF.unlock();
                                return cjVar;
                            }
                        }
                        continue;
                    }
                }
                this.LIZIZ.LJFF.unlock();
                return null;
            }
            this.LIZIZ.LJFF.unlock();
            return null;
        } catch (IOException e2) {
            throw new au(0HH.LIZ("Failed to check number of completed merges for session %s, pack %s", new Object[]{Integer.valueOf(bjVar.LIZ), bjVar.LIZJ.LIZ}), e2, bjVar.LIZ);
        } catch (Throwable th) {
            this.LIZIZ.LJFF.unlock();
            throw th;
        }
    }
}
