package com.ss.android.ugc.effectmanager.algorithm;

import X.Q39;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.effectmanager.FetchModelType;
import com.ss.android.ugc.effectmanager.common.EffectRequest;
import com.ss.android.ugc.effectmanager.common.monitor.EPMonitor;
import com.ss.android.ugc.effectmanager.common.monitor.MonitorTrace;
import com.ss.android.ugc.effectmanager.common.network.EffectNetWorkerWrapper;
import com.ss.android.ugc.effectmanager.download.IModelDownloader;
import com.ss.android.ugc.effectmanager.knadapt.ModelConverterExtKt;
import com.ss.android.ugc.effectmanager.model.ModelInfo;
import java.io.InputStream;

public class ModelDownloader implements IModelDownloader {
    public DownloadedModelStorage mDownloadedModelStorage;
    public EffectNetWorkerWrapper mNetWorker;

    static {
        Covode.recordClassIndex(125204);
    }

    public ModelDownloader(DownloadedModelStorage downloadedModelStorage, EffectNetWorkerWrapper effectNetWorkerWrapper) {
        this.mDownloadedModelStorage = downloadedModelStorage;
        this.mNetWorker = effectNetWorkerWrapper;
    }

    private InputStream generateInputStream(ModelInfo modelInfo, FetchModelType fetchModelType, MonitorTrace monitorTrace) {
        String str = Q39.LIZJ;
        for (String next : modelInfo.getFile_url().getUrl(ModelConverterExtKt.toKNFetchModelType(fetchModelType))) {
            EPMonitor.traceStep(monitorTrace, "ModelDownloader#generateInputStream, downloadUrl = ".concat(String.valueOf(next)));
            try {
                return this.mNetWorker.execute(new EffectRequest("GET", next, false));
            } catch (Exception e) {
                str = "download model: with type " + fetchModelType + ", with url " + next + " failed " + e.getMessage();
                EPMonitor.traceStep(monitorTrace, "ModelDownloader#generateInputStream, exception occurred, errorMessage= ".concat(String.valueOf(str)));
            }
        }
        throw new RuntimeException(str);
    }

    /* JADX WARNING: Can't wrap try/catch for region: R(15:2|3|4|(1:8)|9|(1:13)|14|15|16|(5:18|19|20|51|24)|49|(2:26|27)|28|29|30) */
    /* JADX WARNING: Can't wrap try/catch for region: R(5:39|(2:41|42)|43|44|45) */
    /* JADX WARNING: Missing exception handler attribute for start block: B:28:0x00cb */
    /* JADX WARNING: Missing exception handler attribute for start block: B:43:0x0136 */
    /* JADX WARNING: Unknown top exception splitter block from list: {B:43:0x0136=Splitter:B:43:0x0136, B:28:0x00cb=Splitter:B:28:0x00cb} */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public synchronized long download(com.ss.android.ugc.effectmanager.model.ModelInfo r20, com.ss.android.ugc.effectmanager.FetchModelType r21, com.ss.android.ugc.effectmanager.common.monitor.MonitorTrace r22) {
        /*
            r19 = this;
            r3 = r19
            r13 = r21
            monitor-enter(r3)
            r11 = 8858(0x229a, float:1.2413E-41)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r11)     // Catch:{ all -> 0x013a }
            r2 = r20
            com.ss.android.ugc.effectmanager.model.ExtendedUrlModel r1 = r2.getFileUrl()     // Catch:{ all -> 0x013a }
            java.util.List r0 = r1.getUrlList()     // Catch:{ all -> 0x013a }
            if (r0 == 0) goto L_0x0020
            java.util.List r0 = r1.getUrlList()     // Catch:{ all -> 0x013a }
            boolean r0 = r0.isEmpty()     // Catch:{ all -> 0x013a }
            if (r0 == 0) goto L_0x0022
        L_0x0020:
            com.ss.android.ugc.effectmanager.FetchModelType r13 = com.ss.android.ugc.effectmanager.FetchModelType.ZIP     // Catch:{ all -> 0x013a }
        L_0x0022:
            java.util.List r0 = r1.getZipUrlList()     // Catch:{ all -> 0x013a }
            if (r0 == 0) goto L_0x0032
            java.util.List r0 = r1.getZip_url_list()     // Catch:{ all -> 0x013a }
            boolean r0 = r0.isEmpty()     // Catch:{ all -> 0x013a }
            if (r0 == 0) goto L_0x0034
        L_0x0032:
            com.ss.android.ugc.effectmanager.FetchModelType r13 = com.ss.android.ugc.effectmanager.FetchModelType.ORIGIN     // Catch:{ all -> 0x013a }
        L_0x0034:
            java.lang.String r14 = com.ss.android.ugc.effectmanager.common.ModelNameProcessor.getFullNameFromModelInfo(r2)     // Catch:{ all -> 0x013a }
            r4 = r22
            java.io.InputStream r15 = r3.generateInputStream(r2, r13, r4)     // Catch:{ all -> 0x013a }
            java.lang.String r0 = "ModelDownloader#download, network request success!"
            com.ss.android.ugc.effectmanager.common.monitor.EPMonitor.traceStep(r4, r0)     // Catch:{ all -> 0x013a }
            com.ss.android.ugc.effectmanager.algorithm.DownloadedModelStorage r1 = r3.mDownloadedModelStorage     // Catch:{ all -> 0x013a }
            java.lang.String r0 = r2.getName()     // Catch:{ all -> 0x013a }
            java.util.ArrayList r5 = r1.listLocalModelKeysByName(r0)     // Catch:{ all -> 0x013a }
            com.ss.android.ugc.effectmanager.algorithm.DownloadedModelStorage r12 = r3.mDownloadedModelStorage     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.String r0 = "MD5"
            java.security.MessageDigest r16 = java.security.MessageDigest.getInstance(r0)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            com.ss.ugc.effectplatform.model.algorithm.ExtendedUrlModel r0 = r2.getFile_url()     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.String r17 = r0.getUri()     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            r18 = r4
            long r9 = r12.writeModelToDisk(r13, r14, r15, r16, r17, r18)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.String r1 = "ModelDownloader#download, writeModelToDisk fileSize = "
            java.lang.String r0 = java.lang.String.valueOf(r9)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.String r0 = r1.concat(r0)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            com.ss.android.ugc.effectmanager.common.monitor.EPMonitor.traceStep(r4, r0)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.String r0 = "ModelDownloader#download, try clean old model, existingModels.size = "
            r1.<init>(r0)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            int r0 = r5.size()     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            r1.append(r0)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.String r0 = r1.toString()     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            com.ss.android.ugc.effectmanager.common.monitor.EPMonitor.traceStep(r4, r0)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            r7 = 0
            int r6 = r5.size()     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
        L_0x008a:
            if (r7 >= r6) goto L_0x00c6
            java.lang.Object r8 = r5.get(r7)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.String r8 = (java.lang.String) r8     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            com.ss.android.ugc.effectmanager.algorithm.DownloadedModelStorage r0 = r3.mDownloadedModelStorage     // Catch:{ Exception -> 0x00a5 }
            r0.clearModelFromDisk(r8)     // Catch:{ Exception -> 0x00a5 }
            java.lang.String r1 = "ModelDownloader#download, delete old model success, modelName = "
            java.lang.String r0 = java.lang.String.valueOf(r8)     // Catch:{ Exception -> 0x00a5 }
            java.lang.String r0 = r1.concat(r0)     // Catch:{ Exception -> 0x00a5 }
            com.ss.android.ugc.effectmanager.common.monitor.EPMonitor.traceStep(r4, r0)     // Catch:{ Exception -> 0x00a5 }
            goto L_0x00c3
        L_0x00a5:
            r2 = move-exception
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.String r0 = "ModelDownloader#download, delete old model failed, modelName = "
            r1.<init>(r0)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            r1.append(r8)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.String r0 = ", cause = "
            r1.append(r0)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.String r0 = r2.getMessage()     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            r1.append(r0)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            java.lang.String r0 = r1.toString()     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
            com.ss.android.ugc.effectmanager.common.monitor.EPMonitor.traceStep(r4, r0)     // Catch:{ NoSuchAlgorithmException -> 0x0111, RuntimeException -> 0x00d0 }
        L_0x00c3:
            int r7 = r7 + 1
            goto L_0x008a
        L_0x00c6:
            if (r15 == 0) goto L_0x00cb
            r15.close()     // Catch:{ IOException -> 0x00cb }
        L_0x00cb:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r11)     // Catch:{ all -> 0x013a }
            monitor-exit(r3)
            return r9
        L_0x00d0:
            r5 = move-exception
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ all -> 0x0130 }
            java.lang.String r0 = "ModelDownloader#download, RuntimeException occurred, cause = "
            r1.<init>(r0)     // Catch:{ all -> 0x0130 }
            java.lang.String r0 = r5.getMessage()     // Catch:{ all -> 0x0130 }
            r1.append(r0)     // Catch:{ all -> 0x0130 }
            java.lang.String r0 = r1.toString()     // Catch:{ all -> 0x0130 }
            com.ss.android.ugc.effectmanager.common.monitor.EPMonitor.traceStep(r4, r0)     // Catch:{ all -> 0x0130 }
            java.lang.RuntimeException r2 = new java.lang.RuntimeException     // Catch:{ all -> 0x0130 }
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ all -> 0x0130 }
            java.lang.String r0 = "convertStreamToFile: with type: "
            r1.<init>(r0)     // Catch:{ all -> 0x0130 }
            r1.append(r13)     // Catch:{ all -> 0x0130 }
            java.lang.String r0 = ",with name: "
            r1.append(r0)     // Catch:{ all -> 0x0130 }
            r1.append(r14)     // Catch:{ all -> 0x0130 }
            java.lang.String r0 = " failed. "
            r1.append(r0)     // Catch:{ all -> 0x0130 }
            java.lang.String r0 = r5.getMessage()     // Catch:{ all -> 0x0130 }
            r1.append(r0)     // Catch:{ all -> 0x0130 }
            java.lang.String r0 = r1.toString()     // Catch:{ all -> 0x0130 }
            r2.<init>(r0, r5)     // Catch:{ all -> 0x0130 }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r11)     // Catch:{ all -> 0x0130 }
            throw r2     // Catch:{ all -> 0x0130 }
        L_0x0111:
            r2 = move-exception
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ all -> 0x0130 }
            java.lang.String r0 = "ModelDownloader#download, NoSuchAlgorithmException occurred, cause = "
            r1.<init>(r0)     // Catch:{ all -> 0x0130 }
            java.lang.String r0 = r2.getMessage()     // Catch:{ all -> 0x0130 }
            r1.append(r0)     // Catch:{ all -> 0x0130 }
            java.lang.String r0 = r1.toString()     // Catch:{ all -> 0x0130 }
            com.ss.android.ugc.effectmanager.common.monitor.EPMonitor.traceStep(r4, r0)     // Catch:{ all -> 0x0130 }
            java.lang.AssertionError r0 = new java.lang.AssertionError     // Catch:{ all -> 0x0130 }
            r0.<init>()     // Catch:{ all -> 0x0130 }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r11)     // Catch:{ all -> 0x0130 }
            throw r0     // Catch:{ all -> 0x0130 }
        L_0x0130:
            r0 = move-exception
            if (r15 == 0) goto L_0x0136
            r15.close()     // Catch:{ IOException -> 0x0136 }
        L_0x0136:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r11)     // Catch:{ all -> 0x013a }
            throw r0     // Catch:{ all -> 0x013a }
        L_0x013a:
            r0 = move-exception
            monitor-exit(r3)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.effectmanager.algorithm.ModelDownloader.download(com.ss.android.ugc.effectmanager.model.ModelInfo, com.ss.android.ugc.effectmanager.FetchModelType, com.ss.android.ugc.effectmanager.common.monitor.MonitorTrace):long");
    }
}
