package com.epson.memcardacc;

import android.os.AsyncTask;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import epson.common.ExternalFileUtils;
import epson.common.ImageUtil;
import epson.print.IprintApplication;

class FileConvertTask extends AsyncTask<Void, Void, ArrayList<String>> {
    private static final int FILE_SIZE_LIMIT = 10485760;
    private final ConvertEndListener mConvertEndListener;
    private ErrorType mErrorType = ErrorType.NO_ERROR;
    private final ArrayList<String> mOrgFileList;

    interface ConvertEndListener {
        void onConvertEnd(@NonNull ErrorType errorType, @Nullable ArrayList<String> arrayList);
    }

    enum ErrorType {
        NO_ERROR,
        FILE_SIZE_ERROR,
        CONVERT_ERROR
    }

    FileConvertTask(@NonNull ArrayList<String> arrayList, @NonNull ConvertEndListener convertEndListener) {
        mOrgFileList = arrayList;
        mConvertEndListener = convertEndListener;
    }

    static boolean checkSize(ArrayList<String> arrayList) {
        long j;
        try {
            j = MemcardUtil.getFileLength(arrayList.get(0));
        } catch (Exception unused) {
            j = 0;
        }
        if (j > 10485760) {
            return false;
        }
        return true;
    }

    @NonNull
    static File initTempDirectory() {
        return ExternalFileUtils.getInstance(IprintApplication.getInstance()).initTempCacheDirectory(ExternalFileUtils.TempCacheDirectory.MEMORY_CARD_ACCESS);
    }

    protected void onPostExecute(ArrayList<String> arrayList) {
        ConvertEndListener convertEndListener = mConvertEndListener;
        if (convertEndListener != null) {
            convertEndListener.onConvertEnd(mErrorType, arrayList);
        }
    }

    protected ArrayList<String> doInBackground(Void... voidArr) {
        ArrayList<String> convertImageFile = convertImageFile(mOrgFileList);
        if (convertImageFile == null) {
            return null;
        }
        if (checkSize(convertImageFile)) {
            return convertImageFile;
        }
        mErrorType = ErrorType.FILE_SIZE_ERROR;
        return null;
    }

    @Nullable
    private ArrayList<String> convertImageFile(@NonNull ArrayList<String> arrayList) {
        ArrayList<String> arrayList2 = new ArrayList<>();
        File initTempDirectory = initTempDirectory();
        Iterator<String> it = arrayList.iterator();
        while (it.hasNext()) {
            String next = it.next();
            String notDuplicateFilename = ExternalFileUtils.getNotDuplicateFilename(next, initTempDirectory.getPath(), 1);
            if (notDuplicateFilename == null) {
                mErrorType = ErrorType.CONVERT_ERROR;
                return null;
            } else if (!ImageUtil.convertHEIFtoJPEG(next, notDuplicateFilename)) {
                mErrorType = ErrorType.CONVERT_ERROR;
                return null;
            } else {
                arrayList2.add(notDuplicateFilename);
            }
        }
        return arrayList2;
    }
}
