package epson.print.gdconv;

import android.content.Context;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.epson.iprint.storage.Network;
import com.epson.iprint.storage.gdrivev3.DriveWriter;
import epson.common.ExternalFileUtils;
import epson.common.Utils;
import epson.print.CommonDefine;
import java.io.File;
import java.lang.ref.WeakReference;

public class ConvertTask extends AsyncTask<String, ConvertStatus, Integer> {
    private static final String FOLDER_FOR_CONVERT = "Epson iPrint";
    private final WeakReference<Context> mContextReference;
    private File mOutFile;
    private final WeakReference<TaskCallback> mTaskCallbackWeakReference;

    public interface TaskCallback {
        void messageChange(ConvertStatus convertStatus);

        void notifyTaskEnd(int i);
    }

    @Nullable
    public synchronized File getOutFile() {
        return this.mOutFile;
    }

    public ConvertTask(@NonNull Context context, TaskCallback taskCallback) {
        this.mContextReference = new WeakReference<>(context.getApplicationContext());
        this.mTaskCallbackWeakReference = new WeakReference<>(taskCallback);
    }

    protected Integer doInBackground(String[] strArr) {
        if (strArr == null || strArr[0] == null) {
            return 101;
        }
        String str = strArr[0];
        Context context = (Context) this.mContextReference.get();
        if (context == null) {
            return 103;
        }
        new Network();
        if (!Utils.isConnecting(context)) {
            return 102;
        }
        try {
            DriveWriter createDriveWriter = DriveWriter.createDriveWriter(context);
            String mimeType = Utils.getMimeType(str);
            if (mimeType == null) {
                return 101;
            }
            publishProgress(new ConvertStatus[]{ConvertStatus.UPLOAD});
            File createOutFilename = createOutFilename(context, str);
            synchronized (this) {
                this.mOutFile = createOutFilename;
            }
            createDriveWriter.convertFileToPdf(str, mimeType, createDriveWriter.findOrCreateFolder("Epson iPrint"), createOutFilename, new DriveWriter.ConvertStatusNotifier() {
                public void changeStatus(ConvertStatus convertStatus) {
                    ConvertTask.this.publishProgress(new ConvertStatus[]{convertStatus});
                }
            });
            return 100;
        } catch (Exception unused) {
            return 101;
        }
    }

    @NonNull
    private File createOutFilename(@NonNull Context context, @NonNull String str) {
        String name = new File(str).getName();
        File tempCacheDir = ExternalFileUtils.getInstance(context).getTempCacheDir(ExternalFileUtils.TempCacheDirectory.GOOGLE_DRIVE_CONVERT);
        return new File(tempCacheDir, name + CommonDefine.FileType_PDF);
    }

    protected void onProgressUpdate(ConvertStatus... convertStatusArr) {
        TaskCallback taskCallback = (TaskCallback) this.mTaskCallbackWeakReference.get();
        if (taskCallback != null && convertStatusArr != null && convertStatusArr[0] != null) {
            taskCallback.messageChange(convertStatusArr[0]);
        }
    }

    private void doNotifyTaskEnd(int i) {
        TaskCallback taskCallback = (TaskCallback) this.mTaskCallbackWeakReference.get();
        if (taskCallback != null) {
            taskCallback.notifyTaskEnd(i);
        }
    }

    protected void onPostExecute(Integer num) {
        doNotifyTaskEnd(num != null ? num.intValue() : 101);
    }

    protected void onCancelled() {
        doNotifyTaskEnd(103);
    }
}
