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

import X.0HH;
import X.Q39;
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.download.DownloadListenerAdapter;
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.context.EffectContext;
import com.ss.android.ugc.effectmanager.effect.model.ProviderEffect;
import com.ss.android.ugc.effectmanager.effect.task.result.DownloadProviderEffectTaskResult;
import java.io.File;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

public class DownloadProviderEffectTask extends NormalTask {
    public EffectConfiguration mConfiguration;
    public String mDownLoadUrl;
    public ProviderEffect mEffect;
    public EffectContext mEffectContext;
    public String mRemoteIp;
    public String mRequestedUrl;
    public int mRetryCount;

    static {
        Covode.recordClassIndex(125502);
    }

    public void cancel() {
    }

    public void execute() {
        int i = this.mRetryCount;
        int i2 = 0;
        while (i2 < i) {
            if (isCanceled()) {
                sendMessage(19, new DownloadProviderEffectTaskResult(this.mEffect, new ExceptionResult(10001)));
                return;
            }
            try {
                if (TextUtils.isEmpty(this.mEffect.getPath())) {
                    ProviderEffect providerEffect = this.mEffect;
                    providerEffect.setPath(this.mConfiguration.getEffectDir() + File.separator + this.mEffect.getId() + ".gif");
                }
                this.mRequestedUrl = this.mDownLoadUrl;
                try {
                    this.mRemoteIp = InetAddress.getByName(new URL(this.mRequestedUrl).getHost()).getHostAddress();
                } catch (UnknownHostException e) {
                    0HH.LIZ(e);
                } catch (MalformedURLException e2) {
                    0HH.LIZ(e2);
                }
                ProviderEffect download = download(this.mDownLoadUrl, this.mEffect.getPath());
                if (download != null) {
                    sendMessage(19, new DownloadProviderEffectTaskResult(download, (ExceptionResult) null));
                    return;
                }
                i2++;
            } catch (Exception e3) {
                if (i2 == i - 1) {
                    0HH.LIZ(e3);
                    ExceptionResult exceptionResult = new ExceptionResult(e3);
                    exceptionResult.setTrackParams(this.mRequestedUrl, Q39.LIZJ, this.mRemoteIp);
                    sendMessage(19, new DownloadProviderEffectTaskResult(this.mEffect, exceptionResult));
                    return;
                }
            }
        }
    }

    private ProviderEffect download(String str, String str2) {
        EffectRequest effectRequest = new EffectRequest("GET", str, false);
        InputStream execute = this.mEffectContext.getEffectConfiguration().getEffectNetWorker().execute(effectRequest);
        final DownloadProviderEffectTaskResult downloadProviderEffectTaskResult = new DownloadProviderEffectTaskResult(this.mEffect, (ExceptionResult) null, 0, 0);
        EffectUtils.convertStreamToFile(execute, str2, effectRequest.getContentLength(), new DownloadListenerAdapter() {
            static {
                Covode.recordClassIndex(125503);
            }

            public void onProgress(int i, long j) {
                DownloadProviderEffectTaskResult downloadProviderEffectTaskResult = downloadProviderEffectTaskResult;
                downloadProviderEffectTaskResult.setProgress(i);
                downloadProviderEffectTaskResult.setTotalSize(j);
                DownloadProviderEffectTask.this.sendMessage(54, downloadProviderEffectTaskResult);
            }
        });
        return this.mEffect;
    }

    public DownloadProviderEffectTask(EffectContext effectContext, String str, ProviderEffect providerEffect, Handler handler) {
        super(handler, str);
        this.mEffect = providerEffect;
        this.mEffectContext = effectContext;
        this.mConfiguration = effectContext.getEffectConfiguration();
        this.mRetryCount = effectContext.getEffectConfiguration().getRetryCount();
        this.mDownLoadUrl = EffectUtils.getUrl(providerEffect);
    }
}
