package com.ss.android.ugc.effectmanager.effect.task.task.oldtask;

import X.0HH;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.effectmanager.EffectConfiguration;
import com.ss.android.ugc.effectmanager.common.EffectConstants;
import com.ss.android.ugc.effectmanager.common.EffectRequest;
import com.ss.android.ugc.effectmanager.common.cachemanager.ICache;
import com.ss.android.ugc.effectmanager.common.listener.IJsonConverter;
import com.ss.android.ugc.effectmanager.common.listener.IMonitorService;
import com.ss.android.ugc.effectmanager.common.logger.EPLog;
import com.ss.android.ugc.effectmanager.common.task.ExceptionResult;
import com.ss.android.ugc.effectmanager.common.task.NormalTask;
import com.ss.android.ugc.effectmanager.common.utils.EffectCacheKeyGenerator;
import com.ss.android.ugc.effectmanager.common.utils.EffectRequestUtil;
import com.ss.android.ugc.effectmanager.common.utils.EventJsonBuilder;
import com.ss.android.ugc.effectmanager.common.utils.NetworkUtils;
import com.ss.android.ugc.effectmanager.context.EffectContext;
import com.ss.android.ugc.effectmanager.effect.model.EffectChannelModel;
import com.ss.android.ugc.effectmanager.effect.model.EffectChannelResponse;
import com.ss.android.ugc.effectmanager.effect.task.result.EffectChannelTaskResult;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;

public class FetchEffectChannelTask extends NormalTask {
    public EffectConfiguration mConfiguration;
    public int mCurCnt = this.mConfiguration.getRetryCount();
    public EffectContext mEffectContext;
    public ICache mFileCache;
    public IJsonConverter mJsonConverter = this.mConfiguration.getJsonConverter();
    public String mRemoteIp;
    public String mRequestedUrl;
    public String mSelectedHost;
    public IMonitorService monitorService = this.mConfiguration.getMonitorService();
    public String panel;
    public long size;

    static {
        Covode.recordClassIndex(125509);
    }

    private EffectRequest buildEffectListRequest() {
        HashMap<String, String> addCommonParams = EffectRequestUtil.INSTANCE.addCommonParams(this.mConfiguration);
        if (!TextUtils.isEmpty(this.panel)) {
            addCommonParams.put("panel", this.panel);
        }
        this.mSelectedHost = this.mEffectContext.getBestHostUrl();
        String buildRequestUrl = NetworkUtils.buildRequestUrl(addCommonParams, this.mSelectedHost + this.mConfiguration.getApiAdress() + "/v3/effects");
        this.mRequestedUrl = buildRequestUrl;
        try {
            this.mRemoteIp = InetAddress.getByName(new URL(buildRequestUrl).getHost()).getHostAddress();
        } catch (UnknownHostException e) {
            0HH.LIZ(e);
        } catch (MalformedURLException e2) {
            0HH.LIZ(e2);
        }
        return new EffectRequest("GET", buildRequestUrl);
    }

    /* JADX WARNING: Removed duplicated region for block: B:28:0x010b A[Catch:{ all -> 0x012c }, EDGE_INSN: B:45:0x010b->B:28:0x010b ?: BREAK  
    EDGE_INSN: B:46:0x010b->B:28:0x010b ?: BREAK  ] */
    /* JADX WARNING: Removed duplicated region for block: B:29:0x0114  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void execute() {
        /*
            r17 = this;
            r3 = r17
            long r15 = java.lang.System.currentTimeMillis()
            com.ss.android.ugc.effectmanager.common.EffectRequest r4 = r17.buildEffectListRequest()
        L_0x000a:
            int r1 = r3.mCurCnt
            int r0 = r1 + -1
            r3.mCurCnt = r0
            if (r1 == 0) goto L_0x0131
            r6 = 0
            boolean r0 = r17.isCanceled()     // Catch:{ Exception -> 0x0102 }
            if (r0 == 0) goto L_0x0025
            com.ss.android.ugc.effectmanager.common.task.ExceptionResult r1 = new com.ss.android.ugc.effectmanager.common.task.ExceptionResult     // Catch:{ Exception -> 0x0102 }
            r0 = 10001(0x2711, float:1.4014E-41)
            r1.<init>((int) r0)     // Catch:{ Exception -> 0x0102 }
            r3.onFail(r1)     // Catch:{ Exception -> 0x0102 }
            goto L_0x0119
        L_0x0025:
            com.ss.android.ugc.effectmanager.EffectConfiguration r0 = r3.mConfiguration     // Catch:{ Exception -> 0x0102 }
            com.ss.android.ugc.effectmanager.common.network.EffectNetWorkerWrapper r0 = r0.getEffectNetWorker()     // Catch:{ Exception -> 0x0102 }
            java.io.InputStream r5 = r0.execute(r4)     // Catch:{ Exception -> 0x0102 }
            long r13 = java.lang.System.currentTimeMillis()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            com.ss.android.ugc.effectmanager.EffectConfiguration r0 = r3.mConfiguration     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            com.ss.android.ugc.effectmanager.common.network.EffectNetWorkerWrapper r2 = r0.getEffectNetWorker()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            com.ss.android.ugc.effectmanager.common.listener.IJsonConverter r1 = r3.mJsonConverter     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.Class<com.ss.android.ugc.effectmanager.effect.model.net.EffectNetListResponse> r0 = com.ss.android.ugc.effectmanager.effect.model.net.EffectNetListResponse.class
            com.ss.android.ugc.effectmanager.common.model.BaseNetResponse r1 = r2.parse(r4, r5, r1, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            com.ss.android.ugc.effectmanager.effect.model.net.EffectNetListResponse r1 = (com.ss.android.ugc.effectmanager.effect.model.net.EffectNetListResponse) r1     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            long r11 = java.lang.System.currentTimeMillis()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            boolean r0 = r1.checkValue()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            if (r0 != 0) goto L_0x0061
            int r0 = r3.mCurCnt     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            if (r0 != 0) goto L_0x005d
            com.ss.android.ugc.effectmanager.common.task.ExceptionResult r1 = new com.ss.android.ugc.effectmanager.common.task.ExceptionResult     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r0 = 10002(0x2712, float:1.4016E-41)
            r1.<init>((int) r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r3.onFail(r1)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            goto L_0x011d
        L_0x005d:
            com.ss.android.ugc.effectmanager.common.utils.CloseUtil.close(r5)
            goto L_0x000a
        L_0x0061:
            com.ss.android.ugc.effectmanager.effect.model.EffectChannelModel r8 = r1.getData()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            com.ss.android.ugc.effectmanager.effect.model.BuildEffectChannelResponse r7 = new com.ss.android.ugc.effectmanager.effect.model.BuildEffectChannelResponse     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r2 = r3.panel     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            com.ss.android.ugc.effectmanager.context.EffectContext r0 = r3.mEffectContext     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            com.ss.android.ugc.effectmanager.EffectConfiguration r0 = r0.getEffectConfiguration()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.io.File r0 = r0.getEffectDir()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r1 = r0.getAbsolutePath()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r0 = 0
            r7.<init>(r2, r1, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            com.ss.android.ugc.effectmanager.effect.model.EffectChannelResponse r2 = r7.buildChannelResponse(r8)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r3.saveEffectList(r8)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r1 = 14
            com.ss.android.ugc.effectmanager.effect.task.result.EffectChannelTaskResult r0 = new com.ss.android.ugc.effectmanager.effect.task.result.EffectChannelTaskResult     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r0.<init>(r2, r6)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r3.sendMessage(r1, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            long r9 = java.lang.System.currentTimeMillis()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            com.ss.android.ugc.effectmanager.common.listener.IMonitorService r7 = r3.monitorService     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            if (r7 == 0) goto L_0x0121
            java.lang.String r6 = "effect_list_success_rate"
            com.ss.android.ugc.effectmanager.common.utils.EventJsonBuilder r8 = com.ss.android.ugc.effectmanager.common.utils.EventJsonBuilder.newBuilder()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r1 = "app_id"
            com.ss.android.ugc.effectmanager.EffectConfiguration r0 = r3.mConfiguration     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r0 = r0.getAppID()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r8.addValuePair(r1, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r1 = "access_key"
            com.ss.android.ugc.effectmanager.EffectConfiguration r0 = r3.mConfiguration     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r0 = r0.getAccessKey()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r8.addValuePair(r1, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r1 = "panel"
            java.lang.String r0 = r3.panel     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r8.addValuePair(r1, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r2 = "duration"
            long r0 = r9 - r15
            java.lang.Long r0 = java.lang.Long.valueOf(r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r8.addValuePair(r2, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r2 = "network_time"
            long r0 = r13 - r15
            java.lang.Long r0 = java.lang.Long.valueOf(r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r8.addValuePair(r2, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r2 = "json_time"
            long r0 = r11 - r13
            java.lang.Long r0 = java.lang.Long.valueOf(r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r8.addValuePair(r2, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r1 = "io_time"
            long r9 = r9 - r11
            java.lang.Long r0 = java.lang.Long.valueOf(r9)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r8.addValuePair(r1, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r2 = "size"
            long r0 = r3.size     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.Long r0 = java.lang.Long.valueOf(r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r8.addValuePair(r2, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            java.lang.String r2 = "effect_platform_type"
            r1 = 0
            java.lang.Integer r0 = java.lang.Integer.valueOf(r1)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r8.addValuePair(r2, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            org.json.JSONObject r0 = r8.build()     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            r7.monitorStatusRate(r6, r1, r0)     // Catch:{ Exception -> 0x00ff, all -> 0x0125 }
            goto L_0x0121
        L_0x00ff:
            r1 = move-exception
            r6 = r5
            goto L_0x0103
        L_0x0102:
            r1 = move-exception
        L_0x0103:
            int r0 = r3.mCurCnt     // Catch:{ all -> 0x012c }
            if (r0 == 0) goto L_0x010b
            boolean r0 = r1 instanceof com.ss.android.ugc.effectmanager.common.exception.StatusCodeException     // Catch:{ all -> 0x012c }
            if (r0 == 0) goto L_0x0114
        L_0x010b:
            com.ss.android.ugc.effectmanager.common.task.ExceptionResult r0 = new com.ss.android.ugc.effectmanager.common.task.ExceptionResult     // Catch:{ all -> 0x012c }
            r0.<init>((java.lang.Exception) r1)     // Catch:{ all -> 0x012c }
            r3.onFail(r0)     // Catch:{ all -> 0x012c }
            goto L_0x0128
        L_0x0114:
            com.ss.android.ugc.effectmanager.common.utils.CloseUtil.close(r6)
            goto L_0x000a
        L_0x0119:
            com.ss.android.ugc.effectmanager.common.utils.CloseUtil.close(r6)
            return
        L_0x011d:
            com.ss.android.ugc.effectmanager.common.utils.CloseUtil.close(r5)
            return
        L_0x0121:
            com.ss.android.ugc.effectmanager.common.utils.CloseUtil.close(r5)
            return
        L_0x0125:
            r0 = move-exception
            r6 = r5
            goto L_0x012d
        L_0x0128:
            com.ss.android.ugc.effectmanager.common.utils.CloseUtil.close(r6)
            return
        L_0x012c:
            r0 = move-exception
        L_0x012d:
            com.ss.android.ugc.effectmanager.common.utils.CloseUtil.close(r6)
            throw r0
        L_0x0131:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.effectmanager.effect.task.task.oldtask.FetchEffectChannelTask.execute():void");
    }

    private void onFail(ExceptionResult exceptionResult) {
        exceptionResult.setTrackParams(this.mRequestedUrl, this.mSelectedHost, this.mRemoteIp);
        sendMessage(14, new EffectChannelTaskResult((EffectChannelResponse) null, exceptionResult));
        IMonitorService iMonitorService = this.monitorService;
        if (iMonitorService != null) {
            EventJsonBuilder newBuilder = EventJsonBuilder.newBuilder();
            newBuilder.addValuePair("app_id", this.mConfiguration.getAppID());
            newBuilder.addValuePair("access_key", this.mConfiguration.getAccessKey());
            newBuilder.addValuePair("panel", this.panel);
            newBuilder.addValuePair("error_code", Integer.valueOf(exceptionResult.getErrorCode()));
            newBuilder.addValuePair("error_msg", exceptionResult.getMsg());
            newBuilder.addValuePair("host_ip", this.mRemoteIp);
            newBuilder.addValuePair("download_url", this.mRequestedUrl);
            newBuilder.addValuePair("effect_platform_type", 0);
            iMonitorService.monitorStatusRate("effect_list_success_rate", 1, newBuilder.build());
        }
    }

    private void saveEffectList(EffectChannelModel effectChannelModel) {
        String generatePanelKey = EffectCacheKeyGenerator.generatePanelKey(this.mConfiguration.getChannel(), this.panel);
        try {
            this.size = this.mFileCache.save(generatePanelKey, this.mJsonConverter.convertObjToJson(effectChannelModel)) / ((long) EffectConstants.KB);
        } catch (Exception e) {
            EPLog.e("FetchEffectChannelTask", Log.getStackTraceString(e));
        }
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("version", effectChannelModel.getVersion());
            ICache iCache = this.mFileCache;
            iCache.save("effect_version" + this.panel, jSONObject.toString());
        } catch (JSONException e2) {
            0HH.LIZ(e2);
        }
    }

    public FetchEffectChannelTask(EffectContext effectContext, String str, String str2, Handler handler) {
        super(handler, str2);
        this.panel = str;
        this.mEffectContext = effectContext;
        EffectConfiguration effectConfiguration = effectContext.getEffectConfiguration();
        this.mConfiguration = effectConfiguration;
        this.mFileCache = effectConfiguration.getCache();
    }
}
