package com.epson.iprint.storage.gdrivev3;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;



import epson.common.ExternalFileUtils;
import epson.common.Utils;
import epson.print.CommonDefine;
import java.io.File;
import java.io.IOException;

public class DownloadTask extends AsyncTask<OnlineFile, Void, OnlineFile> implements Canceller {
    private static final String DOWNLOAD_TMP_FILE_NAME_BODY = "google_v3";
    @SuppressLint({"StaticFieldLeak"})
    private final Context mApplicationContext;
    private volatile boolean mDownloadCancelled = false;
    private final DownloadCompleteListener mDownloadCompleteListener;
    private volatile File mDownloadFile;
    private final GoogleDownloader mMyDrive;
    private Exception mThreadException;

    public interface DownloadCompleteListener {
        void onDownloadComplete(boolean z, @Nullable String str);
    }

    DownloadTask(@NonNull Context context, @NonNull GoogleDownloader googleDownloader, @NonNull DownloadCompleteListener downloadCompleteListener) {
        this.mApplicationContext = context;
        this.mMyDrive = googleDownloader;
        this.mDownloadCompleteListener = downloadCompleteListener;
        this.mDownloadFile = null;
    }

    protected OnlineFile doInBackground(OnlineFile... onlineFileArr) {
        GoogleDriveFile googleDriveFile;
        if (onlineFileArr == null || (googleDriveFile = onlineFileArr[0]) == null) {
            return null;
        }
        try {
            if (!(googleDriveFile instanceof GoogleDriveFile) || !googleDriveFile.isGoogleDocuments()) {
                File outFile = getOutFile(this.mApplicationContext, getExtensionFromMimeType((OnlineFile) googleDriveFile));
                this.mDownloadFile = outFile;
                if (outFile == null) {
                    return null;
                }
                return this.mMyDrive.downloadFile(googleDriveFile, outFile, this);
            }
            File outFile2 = getOutFile(this.mApplicationContext, CommonDefine.FileType_PDF);
            this.mDownloadFile = outFile2;
            if (outFile2 == null) {
                return null;
            }
            return this.mMyDrive.downloadPdf(googleDriveFile, outFile2);
        } catch (Exception e) {
            this.mThreadException = e;
            return null;
        }
    }

    @VisibleForTesting
    @NonNull
    static String getExtensionFromMimeType(OnlineFile onlineFile) {
        if (onlineFile == null) {
            return "";
        }
        String extensionFromMimeType = getExtensionFromMimeType(onlineFile.getMimeType());
        return extensionFromMimeType.isEmpty() ? getExtensionFromMimeType(Utils.getMimeType(onlineFile.getName())) : extensionFromMimeType;
    }

    @NonNull
    private static String getExtensionFromMimeType(String str) {
        String mimeExt = Utils.getMimeExt(str);
        return mimeExt != null ? mimeExt : "";
    }

    protected void onPostExecute(OnlineFile onlineFile) {
        if (this.mDownloadCancelled) {
            this.mDownloadCompleteListener.onDownloadComplete(false, (String) null);
        }
        if (onlineFile != null) {
            String name = onlineFile.getName();
            if (name != null) {
                this.mDownloadCompleteListener.onDownloadComplete(false, name);
            } else {
                this.mDownloadCompleteListener.onDownloadComplete(true, (String) null);
            }
        } else {
            Exception exc = this.mThreadException;
            deleteDownloadFile();
            this.mDownloadCompleteListener.onDownloadComplete(true, (String) null);
        }
    }

    protected void onCancelled() {
        deleteDownloadFile();
        this.mDownloadCompleteListener.onDownloadComplete(false, (String) null);
    }

    public boolean canceled() {
        return this.mDownloadCancelled;
    }

    public void cancelTask() {
        this.mDownloadCancelled = true;
        cancel(true);
    }

    @Nullable
    private File getOutFile(@NonNull Context context, @NonNull String str) {
        try {
            File prepareOutDirectory = prepareOutDirectory(context);
            return new File(prepareOutDirectory, DOWNLOAD_TMP_FILE_NAME_BODY + str);
        } catch (IOException | SecurityException unused) {
            return null;
        }
    }

    @NonNull
    private File prepareOutDirectory(@NonNull Context context) throws IOException {
        File file = new File(ExternalFileUtils.getInstance(context).getDownloadDir());
        if (file.exists() || file.mkdirs()) {
            File[] listFiles = file.listFiles();
            if (listFiles != null) {
                for (File delete : listFiles) {
                    delete.delete();
                }
            }
            return file;
        }
        throw new IOException();
    }

    public void deleteDownloadFile() {
        if (this.mDownloadFile != null && this.mDownloadFile.exists()) {
            this.mDownloadFile.delete();
            this.mDownloadFile = null;
        }
    }
}
