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

import android.os.Handler;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.effectmanager.EffectConfiguration;
import com.ss.android.ugc.effectmanager.common.EffectRequest;
import com.ss.android.ugc.effectmanager.common.exception.StatusCodeException;
import com.ss.android.ugc.effectmanager.common.listener.IJsonConverter;
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.EffectUtils;
import com.ss.android.ugc.effectmanager.common.utils.FileUtils;
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.Effect;
import com.ss.android.ugc.effectmanager.effect.model.EffectQRCode;
import com.ss.android.ugc.effectmanager.effect.model.net.ScanQRCodeResponse;
import com.ss.android.ugc.effectmanager.effect.task.result.ScanQRCodeTaskResult;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;

public class FetchEffectInfoByQRCodeTask extends NormalTask {
    public EffectConfiguration mConfiguration;
    public int mCurCnt = this.mConfiguration.getRetryCount();
    public EffectContext mEffectContext;
    public final EffectQRCode mEffectQRCode;
    public IJsonConverter mJsonConverter;

    static {
        Covode.recordClassIndex(125510);
    }

    private EffectRequest buildEffectListRequest() {
        HashMap hashMap = new HashMap();
        if (!TextUtils.isEmpty(this.mConfiguration.getSdkVersion())) {
            hashMap.put("sdk_version", this.mConfiguration.getSdkVersion());
        }
        hashMap.put("sec_id", URLEncoder.encode(new JSONObject(this.mEffectQRCode.getQrCodeText()).getString("SecId")));
        if (!TextUtils.isEmpty(this.mConfiguration.getAppID())) {
            hashMap.put("aid", this.mConfiguration.getAppID());
        }
        String bestHostUrl = this.mEffectContext.getBestHostUrl();
        return new EffectRequest("GET", NetworkUtils.buildRequestUrl(hashMap, bestHostUrl + this.mConfiguration.getApiAdress() + "/tidyEffect/secId"));
    }

    public void execute() {
        InputStream inputStream;
        try {
            EffectRequest buildEffectListRequest = buildEffectListRequest();
            while (true) {
                int i = this.mCurCnt;
                this.mCurCnt = i - 1;
                if (i != 0) {
                    try {
                        if (isCanceled()) {
                            onFail(new ExceptionResult(10001));
                            return;
                        }
                        try {
                            inputStream = this.mConfiguration.getEffectNetWorker().execute(buildEffectListRequest);
                            try {
                                ScanQRCodeResponse parse = this.mConfiguration.getEffectNetWorker().parse(buildEffectListRequest, inputStream, this.mJsonConverter, ScanQRCodeResponse.class);
                                if (parse.checkValue()) {
                                    ScanQRCodeResponse.DataNode dataNode = parse.data;
                                    Effect effect = dataNode.effect.toEffect(dataNode.url_prefix);
                                    EffectUtils.setEffectPath(this.mConfiguration.getEffectDir().getAbsolutePath(), Collections.singletonList(effect));
                                    sendMessage(25, new ScanQRCodeTaskResult(effect, (ExceptionResult) null));
                                    FileUtils.INSTANCE.closeQuietly(inputStream);
                                    return;
                                } else if (this.mCurCnt == 0) {
                                    onFail(new ExceptionResult(10002));
                                    FileUtils.INSTANCE.closeQuietly(inputStream);
                                    return;
                                } else {
                                    FileUtils.INSTANCE.closeQuietly(inputStream);
                                }
                            } catch (Throwable th) {
                                th = th;
                                FileUtils.INSTANCE.closeQuietly(inputStream);
                                throw th;
                            }
                        } catch (Throwable th2) {
                            th = th2;
                            inputStream = null;
                            FileUtils.INSTANCE.closeQuietly(inputStream);
                            throw th;
                        }
                    } catch (Exception e) {
                        if (this.mCurCnt == 0 || (e instanceof StatusCodeException)) {
                            onFail(new ExceptionResult(e));
                        }
                    }
                } else {
                    return;
                }
            }
            onFail(new ExceptionResult(e));
        } catch (JSONException e2) {
            onFail(new ExceptionResult((Exception) e2));
        }
    }

    private void onFail(ExceptionResult exceptionResult) {
        sendMessage(22, new ScanQRCodeTaskResult((Effect) null, exceptionResult));
    }

    public FetchEffectInfoByQRCodeTask(EffectContext effectContext, EffectQRCode effectQRCode, String str, Handler handler) {
        super(handler, str);
        this.mEffectContext = effectContext;
        EffectConfiguration effectConfiguration = effectContext.getEffectConfiguration();
        this.mConfiguration = effectConfiguration;
        this.mJsonConverter = effectConfiguration.getJsonConverter();
        this.mEffectQRCode = effectQRCode;
    }
}
