package com.bytedance.ies.cutsame.cut_android;

import X.2Pr;
import X.2te;
import X.2th;
import X.7he;
import X.7lW;
import X.7zx;
import X.AnonymousClass6GV;
import X.AnonymousClass7F0;
import X.AnonymousClass7OE;
import X.AnonymousClass7OG;
import X.AnonymousClass7OI;
import X.AnonymousClass7OM;
import X.AnonymousClass7ON;
import X.AnonymousClass7OO;
import X.AnonymousClass7PL;
import X.C13355fB;
import X.C58057Oh;
import X.C58067Oi;
import X.C58077Oj;
import X.C58237Oz;
import X.Crj;
import X.M12;
import X.RSP;
import android.content.Context;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.ies.cutsame.resourcefetcher.NetworkFileFetcher;
import com.bytedance.ies.cutsame.resourcefetcher.ResourceFetcher;
import com.ss.android.ugc.cut_ui.CutSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import kotlin.h.b.n;

public final class NLETemplateSource {
    public static final C58057Oh LIZ = new C58057Oh((byte) 0);
    public CutSource LIZIZ;
    public final HashMap<AnonymousClass7OI, ResourceFetcher> LIZJ;
    public final List<NLEPrepareListener> LIZLLL;
    public C58237Oz LJ;
    public int LJFF;
    public Context LJI;
    public long LJII;
    public final Crj LJIIIIZZ;
    public final Crj LJIIIZ;

    private final native long nativeClone_nle(long j);

    private final native long nativeCreate_nle(String str, String str2, String str3, String str4);

    private final native void nativeSetDownloaderParams_nle(long j, long j2);

    private final native void nativeSetPrepareListener_nle(long j, NLEPrepareListener nLEPrepareListener);

    private final native void nativeSetResourceFetcher_nle(long j, ResourceFetcher resourceFetcher);

    public final native void nativePrepare_nle(long j, int i);

    public final native void nativeRelease_nle(long j);

    private final boolean LIZJ() {
        if (this.LJII == 0) {
            return true;
        }
        return false;
    }

    static {
        Covode.recordClassIndex(29037);
        AnonymousClass7F0.LIZ.LIZ();
        C13355fB.LIZJ.LIZ();
    }

    public NLETemplateSource() {
        this.LIZJ = new HashMap<>();
        this.LJIIIIZZ = 7he.LIZ(new AnonymousClass7OM(this));
        this.LIZLLL = new ArrayList();
        this.LJIIIZ = 7he.LIZ(new AnonymousClass7ON(this));
    }

    public final void LIZ() {
        MethodCollector.i(4248);
        AnonymousClass6GV.LIZ.LIZJ("NLETemplateSource", "prepareAsync");
        if (LIZJ()) {
            MethodCollector.o(4248);
            return;
        }
        new Thread(new AnonymousClass7OE(this, nativeClone_nle(this.LJII))).start();
        MethodCollector.o(4248);
    }

    public final void LIZIZ() {
        MethodCollector.i(4405);
        AnonymousClass6GV.LIZ.LIZJ("NLETemplateSource", n.LIZ("releaseObject : ", Long.valueOf(this.LJII)));
        this.LIZJ.clear();
        long j = this.LJII;
        if (j != 0) {
            nativeRelease_nle(j);
            this.LJII = 0;
        }
        MethodCollector.o(4405);
    }

    public final void finalize() {
        try {
            if (this.LJII != 0) {
                AnonymousClass6GV.LIZ.LIZLLL("NLETemplateSource", "You forget to release TemplateSource !!");
                LIZIZ();
            }
            2te.constructor-impl(2Pr.LIZ);
        } catch (Throwable th) {
            2te.constructor-impl(2th.LIZ(th));
        }
    }

    public final void LIZ(NLEPrepareListener nLEPrepareListener) {
        M12.LIZ(nLEPrepareListener);
        if (!LIZJ()) {
            this.LIZLLL.add(nLEPrepareListener);
        }
    }

    public final void LIZ(NetworkFileFetcher networkFileFetcher) {
        M12.LIZ(networkFileFetcher);
        AnonymousClass6GV.LIZ.LIZIZ("NLETemplateSource", "setNetworkFileFetcher");
        if (!LIZJ()) {
            this.LIZJ.put(AnonymousClass7OI.NORMAL, networkFileFetcher);
        }
    }

    /* JADX INFO: this call moved to the top of the method (can break code semantics) */
    public NLETemplateSource(Context context, CutSource cutSource, String str) {
        this();
        String str2;
        M12.LIZ(context, cutSource);
        MethodCollector.i(4176);
        Context applicationContext = context.getApplicationContext();
        if (7lW.LIZJ && applicationContext == null) {
            applicationContext = 7lW.LIZ;
        }
        this.LJI = applicationContext;
        this.LIZIZ = cutSource;
        M12.LIZ(context);
        Context applicationContext2 = context.getApplicationContext();
        if (7lW.LIZJ && applicationContext2 == null) {
            applicationContext2 = 7lW.LIZ;
        }
        AnonymousClass7F0.LIZIZ = applicationContext2;
        String str3 = cutSource.LIZIZ;
        if (!TextUtils.isEmpty(cutSource.LIZIZ)) {
            int i = AnonymousClass7OO.LIZ[cutSource.LIZJ.ordinal()];
            if (i == 1 || i == 2) {
                str2 = AnonymousClass7PL.LIZ.LIZ(context, AnonymousClass7OG.TEMPLATE_CACHE) + '/' + AnonymousClass7PL.LIZ.LIZ(cutSource.LIZIZ);
            } else if (i == 3) {
                str2 = AnonymousClass7PL.LIZ.LIZ(context, AnonymousClass7OG.TEMPLATE_WORKSPACE) + '/' + cutSource.LIZIZ;
                str3 = str2;
            } else if (i == 4 || i == 5) {
                str2 = "";
            } else {
                RuntimeException runtimeException = new RuntimeException("unknown CutSourceType");
                MethodCollector.o(4176);
                throw runtimeException;
            }
            AnonymousClass6GV.LIZ.LIZIZ("NLETemplateSource", "constructor : source=" + cutSource + ", initFolder=" + str2);
            CutSource cutSource2 = this.LIZIZ;
            if (cutSource2 == null) {
                n.LIZIZ();
            }
            long nativeCreate_nle = nativeCreate_nle(str2, cutSource2.LIZJ.getDesc(), str3, str == null ? "" : str);
            this.LJII = nativeCreate_nle;
            nativeSetResourceFetcher_nle(nativeCreate_nle, (C58077Oj) this.LJIIIIZZ.getValue());
            nativeSetDownloaderParams_nle(this.LJII, 7zx.LIZ(RSP.LIZ.LIZ(context)));
            nativeSetPrepareListener_nle(this.LJII, (C58067Oi) this.LJIIIZ.getValue());
            MethodCollector.o(4176);
            return;
        }
        NullPointerException nullPointerException = new NullPointerException("source is empty");
        MethodCollector.o(4176);
        throw nullPointerException;
    }
}
