package com.ss.android.ugc.effectmanager;

import X.0Gf;
import X.0Gi;
import X.0Gp;
import X.7xU;
import X.C2802Q4p;
import X.C2815Q5c;
import X.C2817Q5e;
import X.C2838Q5z;
import X.C2863Q6y;
import X.M12;
import X.OC4;
import X.Q39;
import X.Q53;
import X.Q5C;
import X.Q5H;
import X.Q62;
import X.Q66;
import X.Q67;
import X.Q68;
import X.Q69;
import X.Q6H;
import X.Q82;
import X.Q8W;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.effectmanager.DownloadableModelSupportLibraryLoader;
import com.ss.android.ugc.effectmanager.algorithm.AlgorithmModelManager;
import com.ss.android.ugc.effectmanager.algorithm.AssetManagerWrapper;
import com.ss.android.ugc.effectmanager.algorithm.DownloadedModelStorage;
import com.ss.android.ugc.effectmanager.algorithm.FetchResourcesListener;
import com.ss.android.ugc.effectmanager.algorithm.ModelConfigArbiter;
import com.ss.android.ugc.effectmanager.common.ModelNameProcessor;
import com.ss.android.ugc.effectmanager.common.logger.EPLog;
import com.ss.android.ugc.effectmanager.common.network.EffectNetWorkerWrapper;
import com.ss.android.ugc.effectmanager.common.utils.EffectUtils;
import com.ss.android.ugc.effectmanager.common.utils.Preconditions;
import com.ss.android.ugc.effectmanager.effect.bridge.EffectFetcher;
import com.ss.android.ugc.effectmanager.effect.model.Effect;
import com.ss.android.ugc.effectmanager.knadapt.KNLibraryLoader;
import com.ss.android.ugc.effectmanager.knadapt.KNResourceFinder;
import com.ss.android.ugc.effectmanager.knadapt.ModelListenerAdaptKt;
import com.ss.android.ugc.effectmanager.knadapt.UseKNPlatform;
import com.ss.android.ugc.effectmanager.model.LocalModelInfo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;

public final class DownloadableModelSupport {
    public static DownloadableModelSupport INSTANCE;
    public static DownloadableModelSupportLibraryLoader sLibraryLoader = new DownloadableModelSupportLibraryLoader.SystemLoader();
    public final DownloadableModelConfig config;
    public final AssetManagerWrapper mAssetManagerWrapper;
    public DownloadableModelSupportEffectFetcher mEffectFetcher;
    public boolean mEnableKotlinNative = UseKNPlatform.enableKNPlatform;
    public KNResourceFinder mKNResourceFinder;
    public final DownloadedModelStorage mModelCache;
    public ModelConfigArbiter mModelConfigArbiter;
    public final EffectNetWorkerWrapper mNetWorker;
    public DownloadableModelSupportResourceFinder mResourceFinder;
    public final String mSdkVersion;
    public final String mWorkspace;

    public interface EventListener extends ModelEventListener {
        static {
            Covode.recordClassIndex(125164);
        }
    }

    public static boolean isInitialized() {
        if (INSTANCE != null) {
            return true;
        }
        return false;
    }

    public final EffectFetcher getEffectFetcher() {
        return getEffectFetcherInternal();
    }

    public final DownloadableModelSupportEffectFetcher getEffectFetcherInternal() {
        return INSTANCE.getOrCreateEffectFetcher();
    }

    public final DownloadableModelSupportResourceFinder getResourceFinder() {
        return getOrCreateResourceFinder();
    }

    static {
        Covode.recordClassIndex(125158);
    }

    public static DownloadableModelSupport getInstance() {
        DownloadableModelSupport downloadableModelSupport = INSTANCE;
        if (downloadableModelSupport != null) {
            return downloadableModelSupport;
        }
        throw new IllegalStateException("downloadableModelSupport not initialize");
    }

    private void initializeOnStart() {
        ModelConfigArbiter modelConfigArbiter = new ModelConfigArbiter(this.config);
        this.mModelConfigArbiter = modelConfigArbiter;
        modelConfigArbiter.setIModelCache(this.mModelCache);
    }

    public static void loadSo() {
        if (UseKNPlatform.enableKNPlatform) {
            Q69.LIZIZ.LIZ();
        } else {
            DownloadableModelLibraryLoader.INSTANCE.loadLibrary();
        }
    }

    private DownloadableModelSupportResourceFinder getOrCreateKNResourceFinder() {
        if (this.mKNResourceFinder == null) {
            this.mKNResourceFinder = new KNResourceFinder(Q62.LJFF.LIZ().LIZ());
        }
        return this.mKNResourceFinder;
    }

    public final DownloadableModelSupportEffectFetcher getOrCreateEffectFetcher() {
        if (this.mEffectFetcher == null) {
            this.mEffectFetcher = new DownloadableModelSupportEffectFetcher(this.config, this.mModelCache, this.mNetWorker, this.mModelConfigArbiter);
        }
        return this.mEffectFetcher;
    }

    private DownloadableModelSupportResourceFinder getOrCreateResourceFinder() {
        if (this.mEnableKotlinNative && Q62.LJFF.LIZIZ()) {
            return getOrCreateKNResourceFinder();
        }
        if (this.mResourceFinder == null) {
            this.mResourceFinder = new DownloadableModelSupportResourceFinder(this.mModelConfigArbiter, this.mModelCache, this.config, this.mAssetManagerWrapper);
        }
        return this.mResourceFinder;
    }

    public final void requestModelInfoBackGround() {
        C2863Q6y q6y;
        if (this.mEnableKotlinNative && Q62.LJFF.LIZIZ()) {
            Q66 q66 = Q62.LJFF;
            if (Q5C.LIZLLL.get(Q5H.LIZ(0)) == null && (q6y = q66.LIZ().LIZJ.LJJIFFI) != null) {
                q6y.LIZ(new Q67(Q8W.LIZ.LIZ()));
            }
        } else if (this.mModelConfigArbiter.requireDecidedConfigNonBlockling(0) == null) {
            0Gp.LIZ(new Callable<Void>() {
                static {
                    Covode.recordClassIndex(125159);
                }

                public Void call() {
                    DownloadableModelSupport.this.mModelConfigArbiter.requireDecidedConfig(0);
                    return null;
                }
            });
        }
    }

    private boolean checkModelReadyLocal(String str) {
        boolean isResourceAvailable = getOrCreateResourceFinder().isResourceAvailable(str);
        try {
            if (!getEffectFetcherInternal().collectNeedDownloadModelsListNonBlocking(new String[]{str}).isEmpty()) {
                isResourceAvailable = false;
            }
            return isResourceAvailable;
        } catch (Exception unused) {
            return false;
        }
    }

    public static void setLibraryLoader(DownloadableModelSupportLibraryLoader downloadableModelSupportLibraryLoader) {
        Preconditions.checkNotNull(downloadableModelSupportLibraryLoader);
        sLibraryLoader = downloadableModelSupportLibraryLoader;
        KNLibraryLoader kNLibraryLoader = new KNLibraryLoader(downloadableModelSupportLibraryLoader);
        M12.LIZ(kNLibraryLoader);
        Q62.LJ = kNLibraryLoader;
        M12.LIZ(kNLibraryLoader);
        Q69.LIZ = kNLibraryLoader;
    }

    public DownloadableModelSupport(DownloadableModelConfig downloadableModelConfig) {
        AssetManagerWrapper assetManagerWrapper = new AssetManagerWrapper(downloadableModelConfig.getAssetManager(), downloadableModelConfig.getExclusionPattern());
        this.mAssetManagerWrapper = assetManagerWrapper;
        String workspace = downloadableModelConfig.getWorkspace();
        this.mWorkspace = workspace;
        this.mNetWorker = new EffectNetWorkerWrapper(downloadableModelConfig.getEffectNetWorker(), downloadableModelConfig.getContext());
        String sdkVersion = downloadableModelConfig.getSdkVersion();
        this.mSdkVersion = sdkVersion;
        this.config = downloadableModelConfig;
        this.mModelCache = DownloadedModelStorage.getInstance(workspace, sdkVersion, assetManagerWrapper);
    }

    public static void initialize(DownloadableModelConfig downloadableModelConfig) {
        Objects.requireNonNull(downloadableModelConfig);
        if (INSTANCE == null) {
            DownloadableModelSupport downloadableModelSupport = new DownloadableModelSupport(downloadableModelConfig);
            INSTANCE = downloadableModelSupport;
            downloadableModelSupport.initializeOnStart();
            if (UseKNPlatform.enableKNPlatform && downloadableModelConfig.getKNEffectConfig() != null && !Q62.LJFF.LIZIZ()) {
                Q62.LIZ(downloadableModelConfig.getKNEffectConfig());
                return;
            }
            return;
        }
        throw new IllegalStateException("Duplicate initialization");
    }

    public final List<LocalModelInfo> getLocalModelInfo(List<String> list) {
        List<C2802Q4p> list2;
        if (this.mEnableKotlinNative && Q62.LJFF.LIZIZ()) {
            C2817Q5e q5e = Q62.LJFF.LIZ().LIZ;
            if (list == null || list.isEmpty()) {
                list2 = new ArrayList<>();
            } else {
                Object[] array = list.toArray(new String[0]);
                if (array != null) {
                    String[] LIZ = Q68.LIZ((String[]) array);
                    C2815Q5c q5c = q5e.LIZ().LIZ;
                    list2 = new Q53(q5c.LIZLLL, q5c.LIZ, q5c.LIZIZ, q5c.LIZJ, (String[]) null, 0, 112).LIZ(LIZ);
                } else {
                    throw new 7xU("null cannot be cast to non-null type kotlin.Array<T>");
                }
            }
            ArrayList arrayList = new ArrayList();
            for (C2802Q4p q4p : list2) {
                LocalModelInfo fromFile = LocalModelInfo.fromFile(q4p.LIZLLL);
                fromFile.setSize(q4p.LIZJ());
                fromFile.setName(q4p.LIZ());
                fromFile.setVersion(q4p.LIZIZ());
                arrayList.add(fromFile);
            }
            return arrayList;
        } else if (list == null || list.isEmpty()) {
            return new ArrayList();
        } else {
            return getOrCreateEffectFetcher().collectLocalModelInfo(Q68.LIZ((String[]) list.toArray(new String[list.size()])));
        }
    }

    public final void fetchResourcesNeededByRequirements(final List<String> list, final IFetchModelListener iFetchModelListener) {
        if (!this.mEnableKotlinNative || !Q62.LJFF.LIZIZ()) {
            0Gp.LIZ(new Callable<Void>() {
                static {
                    Covode.recordClassIndex(125161);
                }

                public Void call() {
                    DownloadableModelSupport.this.getOrCreateEffectFetcher().fetchModels(list, (Map<String, List<String>>) null);
                    return null;
                }
            }, 0Gp.LIZ, (0Gf) null).LIZ(new 0Gi<Void, Void>() {
                static {
                    Covode.recordClassIndex(125160);
                }

                public Void then(0Gp<Void> r4) {
                    if (r4.LIZJ()) {
                        IFetchModelListener iFetchModelListener = iFetchModelListener;
                        if (iFetchModelListener == null) {
                            return null;
                        }
                        iFetchModelListener.onFailed(r4.LJ());
                        return null;
                    }
                    IFetchModelListener iFetchModelListener2 = iFetchModelListener;
                    if (iFetchModelListener2 == null) {
                        return null;
                    }
                    iFetchModelListener2.onSuccess((String[]) list.toArray(new String[0]));
                    return null;
                }
            });
        } else {
            Q62.LJFF.LIZ().LIZ(list, ModelListenerAdaptKt.toKNListener(iFetchModelListener));
        }
    }

    public final void fetchResourcesNeededByRequirements(String[] strArr, IFetchModelListener iFetchModelListener) {
        if (!this.mEnableKotlinNative || !Q62.LJFF.LIZIZ()) {
            fetchResourcesNeededByRequirements((List<String>) Arrays.asList(strArr), iFetchModelListener);
        } else {
            Q62.LJFF.LIZ().LIZ(Arrays.asList(strArr), ModelListenerAdaptKt.toKNListener(iFetchModelListener));
        }
    }

    public final boolean areRequirementsReady(EffectManager effectManager, Effect effect) {
        String str;
        if (this.mEnableKotlinNative && Q62.LJFF.LIZIZ()) {
            return Q62.LJFF.LIZ().LIZ((com.ss.ugc.effectplatform.model.Effect) effect);
        }
        if (EffectUtils.isRequirementsInvalid(effect)) {
            StringBuilder sb = new StringBuilder("decrypt error effect_id: ");
            sb.append(effect.getId());
            sb.append(", effect_name: ");
            sb.append(effect.getName());
            sb.append(", requirements_sec: ");
            if (effect.getRequirements_sec() != null) {
                str = effect.getRequirements_sec().toString();
            } else {
                str = Q39.LIZJ;
            }
            sb.append(str);
            EPLog.d("DownloadableModelSupport", sb.toString());
            return false;
        }
        String[] resourceNameArrayOfEffect = AlgorithmUtils.getResourceNameArrayOfEffect(effect);
        if (resourceNameArrayOfEffect == null || resourceNameArrayOfEffect.length == 0) {
            EPLog.d("DownloadableModelSupport", "effect: " + effect.getEffectId() + " name: " + effect.getName() + " returned empty resourceNameArrayOfEffect");
            return true;
        }
        this.mModelCache.checkDiskLruCache();
        for (String str2 : resourceNameArrayOfEffect) {
            if ((!AlgorithmModelInfoMemoryCache.hasBuiltCache() || !AlgorithmModelInfoMemoryCache.isModelReady(ModelNameProcessor.getNameOfModel(str2))) && !checkModelReadyLocal(str2)) {
                return false;
            }
        }
        return true;
    }

    public final boolean isEffectReady(EffectManager effectManager, Effect effect) {
        boolean z;
        if (!this.mEnableKotlinNative || !Q62.LJFF.LIZIZ() || effectManager.getKNEffectPlatform() == null) {
            long currentTimeMillis = System.currentTimeMillis();
            boolean isEffectDownloaded = effectManager.isEffectDownloaded(effect);
            if (isEffectDownloaded) {
                isEffectDownloaded = areRequirementsReady(effectManager, effect);
            }
            EPLog.d("DownloadableModelSupport", "effect: " + effect.getEffectId() + " name: " + effect.getName() + " result: " + isEffectDownloaded + " time cost: " + (System.currentTimeMillis() - currentTimeMillis) + "ms");
            return isEffectDownloaded;
        }
        Q62 LIZ = Q62.LJFF.LIZ();
        Q6H kNEffectPlatform = effectManager.getKNEffectPlatform();
        M12.LIZ(kNEffectPlatform, effect);
        long currentTimeMillis2 = System.currentTimeMillis();
        if (kNEffectPlatform.LIZ((com.ss.ugc.effectplatform.model.Effect) effect)) {
            z = LIZ.LIZ((com.ss.ugc.effectplatform.model.Effect) effect);
        } else {
            z = false;
        }
        Q82 q82 = Q82.LIZ;
        q82.LIZ("isEffectReady", "effect: " + effect.getEffect_id() + ", name:" + effect.getName() + ", result: " + z + ", time cost: " + (System.currentTimeMillis() - currentTimeMillis2) + " ms");
        return z;
    }

    public final void fetchResourcesWithModelNames(int i, String[] strArr, FetchResourcesListener fetchResourcesListener) {
        if (!AlgorithmModelManager.isInitialized()) {
            AlgorithmModelManager.initialize(this.config);
        }
        AlgorithmModelManager.getInstance().fetchResourcesWithModelNames(i, strArr, fetchResourcesListener);
    }

    public final void fetchResourcesByRequirementsAndModelNames(String[] strArr, Map<String, List<String>> map, IFetchResourceListener iFetchResourceListener) {
        final String[] strArr2 = strArr;
        final Map<String, List<String>> map2 = map;
        if (!this.mEnableKotlinNative || !Q62.LJFF.LIZIZ()) {
            0Gp.LIZ(new Callable<Void>() {
                static {
                    Covode.recordClassIndex(125163);
                }

                public Void call() {
                    DownloadableModelSupport.this.getOrCreateEffectFetcher().fetchModels(Arrays.asList(strArr2), map2);
                    return null;
                }
            }, 0Gp.LIZ, (0Gf) null).LIZ(new 4(this, iFetchResourceListener));
            return;
        }
        Q62 LIZ = Q62.LJFF.LIZ();
        OC4 kNListener = IFetchResourceListenerKt.toKNListener(iFetchResourceListener);
        M12.LIZ(strArr2, map2);
        C2817Q5e q5e = LIZ.LIZ;
        M12.LIZ(strArr2, map2);
        C2863Q6y q6y = q5e.LIZLLL.LJJIFFI;
        if (q6y != null) {
            q6y.LIZ(new C2838Q5z(q5e, strArr2, map2, kNListener, Q8W.LIZ.LIZ()));
        }
    }
}
