package epson.scan.i2lib;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;

import com.epson.iprint.prtlogger.Analytics;
import com.epson.iprint.prtlogger.CommonLog;
import com.epson.iprint.prtlogger.PrintLog;
import com.epson.iprint.shared.SharedParamScan;
import com.epson.lib.escani2.EscanI2Lib;
import com.epson.lib.escani2.ScanI2Params;
import com.epson.lib.escani2.ScanI2Runner;
import com.epson.lib.escani2.ScanSize;
import com.epson.lib.escani2.ScannerI2Info;

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

import epson.common.ExternalFileUtils;
import epson.scan.activity.ScanContinueParam;
import epson.scan.activity.ScannerPropertyWrapper;
import epson.scan.lib.CancelableScannerProbe;
import epson.scan.lib.EscanLibException;
import epson.scan.lib.I1ScanParams;

public class I2ScanTask extends AsyncTask<Void, Void, I2ScanTask.TaskError> {
    private static final String LOG_TAG = "I2ScanTask";
    private Context mApplicationContext;
    private volatile boolean mCancelRequested;
    private CancelableScannerProbe mCancelableScannerProbe;
    private CommonLog mCommonLog;
    private ImageProcessController mImageProcessController;
    private ArrayList<String> mResScanFileList;
    private ScanI2Params mScanI2Params;
    private ScanI2Runner mScanI2Runner = new ScanI2Runner();
    private final ScanResultReceiver mScanResultReceiver;
    private ScannerPropertyWrapper mScannerPropertyWrapper;

    public interface ScanResultReceiver {
        @WorkerThread
        void onProgressUpdate(int i, int i2);

        @UiThread
        void onScanEnd(TaskError taskError);
    }

    protected void onPreExecute() {
    }

    public ArrayList<String> getScanFileList() {
        return mResScanFileList;
    }

    @Nullable
    public File getScanFile(int i, int i2) {
        if (mScanI2Runner == null) {
            return null;
        }
        return mImageProcessController.getAllProcessFile(i, i2);
    }

    public I2ScanTask(@NonNull Context context, @Nullable ScanResultReceiver scanResultReceiver, @Nullable SharedParamScan sharedParamScan, CommonLog commonLog) {
        mApplicationContext = context;
        ScanI2Params loadAndAdjustScanParams = loadAndAdjustScanParams(context);
        margeScanI2Params(loadAndAdjustScanParams, sharedParamScan);
        mScanI2Params = loadAndAdjustScanParams;
        mScannerPropertyWrapper = ScannerPropertyWrapper.load(context);
        ScannerPropertyWrapper scannerPropertyWrapper = mScannerPropertyWrapper;
        if (scannerPropertyWrapper == null) {
            throw new IllegalStateException("ScannerPropertyWrapper.load() error!");
        } else if (scannerPropertyWrapper.getEscIVersion() == 2) {
            mImageProcessController = new ImageProcessController(this, loadAndAdjustScanParams.duplex, loadAndAdjustScanParams.duplexTurnDirection, mScannerPropertyWrapper.getI2ScannerAllInfo().scannerI2Info.adfDuplexType, loadAndAdjustScanParams.colorMode == EscanI2Lib.ColorMode.MONO_1BIT, loadAndAdjustScanParams.density);
            mImageProcessController.init(getSaveDirectory(context), Calendar.getInstance());
            mScanResultReceiver = scanResultReceiver;
            mResScanFileList = new ArrayList<>();
            mCommonLog = commonLog;
            if (mCommonLog == null) {
                mCommonLog = new CommonLog();
            }
        } else {
            throw new IllegalStateException("ScannerPropertyWrapper.load() error! ESC/I version != 2");
        }
    }

    private void margeScanI2Params(@NonNull ScanI2Params scanI2Params, @Nullable SharedParamScan sharedParamScan) {
        if (sharedParamScan != null) {
            scanI2Params.resolutionMain = sharedParamScan.getRes_main();
            scanI2Params.resolutionSub = sharedParamScan.getRes_sub();
            scanI2Params.scanSize = new ScanSize(sharedParamScan.getPixel_main(), sharedParamScan.getPixel_sub(), 0, 0);
            EscanI2Lib.ColorMode colorMode = EscanI2Lib.ColorMode.COLOR_24BIT;
            switch (sharedParamScan.getScan_type()) {
                case 2:
                    colorMode = EscanI2Lib.ColorMode.GRAYSCALE_8BIT;
                    break;
                case 3:
                    colorMode = EscanI2Lib.ColorMode.MONO_1BIT;
                    break;
            }
            scanI2Params.colorMode = colorMode;
        }
    }

    private File getSaveDirectory(Context context) {
        File file = new File(ExternalFileUtils.getInstance(context).getScannedImageDir());
        if (file.exists() || file.mkdirs()) {
            return file;
        }
        throw new RuntimeException("mkdirs error '" + file + "'");
    }

    private ScanI2Params loadAndAdjustScanParams(Context context) {
        ScanI2Params loadScanI2Params = I2ScanParamManager.loadScanI2Params(context);
        loadScanI2Params.autoCrop = EscanI2Lib.AutoCrop.FALSE;
        loadScanI2Params.autoSkew = EscanI2Lib.AutoSkew.FALSE;
        loadScanI2Params.lookupTableNo = 0;
        loadScanI2Params.overScan = false;
        loadScanI2Params.doubleFeedLevel = EscanI2Lib.DoubleFeedLevel.LEVEL_NONE;
        if (loadScanI2Params.inputUnit == EscanI2Lib.InputUnit.ADF) {
            loadScanI2Params.maxScanSheets = ScanContinueParam.getAvailableScanPageCount();
            loadScanI2Params.maxWriteSheets = ScanContinueParam.getAvailableScanPageCount();
        } else {
            loadScanI2Params.maxScanSheets = 1;
            loadScanI2Params.maxWriteSheets = 1;
        }
        loadScanI2Params.qualityHW = 90;
        loadScanI2Params.qualitySW = 90;
        return loadScanI2Params;
    }

    public int[] getScanPages() {
        return (int[]) mScanI2Runner.getScanPages().clone();
    }


    public epson.scan.i2lib.I2ScanTask.TaskError doInBackground(java.lang.Void... r9) {

        throw new UnsupportedOperationException("Method not decompiled: epson.scan.i2lib.I2ScanTask.doInBackground(java.lang.Void[]):epson.scan.i2lib.I2ScanTask$TaskError");
    }

    private void sendLog(int i) {
        CommonLog commonLog = mCommonLog;
        if (commonLog != null) {
            commonLog.connectionType = i;
            commonLog.numberOfSheet = getScanFileList().size();
            if (mCommonLog.action == 0) {
                mCommonLog.action = PrintLog.ACTION_SCAN;
            }
            ScannerPropertyWrapper scannerPropertyWrapper = mScannerPropertyWrapper;
            if (scannerPropertyWrapper != null) {
                mCommonLog.printerName = scannerPropertyWrapper.getModelName();
            }
            if (mCommonLog.numberOfSheet > 0) {
                Analytics.sendScanI1Log(mApplicationContext, convertScanParams(mScanI2Params), mCommonLog, ScanContinueParam.isContinueScanning());
            }
        }
    }

    private I1ScanParams convertScanParams(@NonNull ScanI2Params scanI2Params) {
        I1ScanParams i1ScanParams = new I1ScanParams();
        if (C24451.$SwitchMap$com$epson$lib$escani2$EscanI2Lib$InputUnit[scanI2Params.inputUnit.ordinal()] != 1) {
            i1ScanParams.inputDevice = 0;
        } else {
            i1ScanParams.inputDevice = 1;
        }
        switch (scanI2Params.colorMode) {
            case MONO_1BIT:
                i1ScanParams.colorType = 3;
                break;
            case GRAYSCALE_8BIT:
                i1ScanParams.colorType = 2;
                break;
            default:
                i1ScanParams.colorType = 1;
                break;
        }
        i1ScanParams.resolution = scanI2Params.resolutionMain;
        i1ScanParams.twoSide = scanI2Params.duplex ? 1 : 0;
        if (C24451.$SwitchMap$com$epson$lib$escani2$EscanI2Lib$Gamma[scanI2Params.userGamma.ordinal()] != 1) {
            i1ScanParams.gamma = 1;
        } else {
            i1ScanParams.gamma = 0;
        }
        i1ScanParams.brightness = 255 - scanI2Params.density;
        if (scanI2Params.colorMode != EscanI2Lib.ColorMode.MONO_1BIT) {
            i1ScanParams.brightness = 127;
        }
        return i1ScanParams;
    }

    /* renamed from: epson.scan.i2lib.I2ScanTask$1 */
    static /* synthetic */ class C24451 {
        static final /* synthetic */ int[] $SwitchMap$com$epson$lib$escani2$EscanI2Lib$Gamma = new int[EscanI2Lib.Gamma.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$epson$lib$escani2$EscanI2Lib$InputUnit = new int[EscanI2Lib.InputUnit.values().length];
    }

    private int checkScanParams(@NonNull File file, @NonNull ScanI2Params scanI2Params, @NonNull ScannerI2Info scannerI2Info) {
        int[] validScanSize = scanI2Params.scanSize.getValidScanSize(scannerI2Info, scanI2Params);
        if (validScanSize == null) {
            return 1;
        }
        if (file.getFreeSpace() < ((long) ((scanI2Params.inputUnit == EscanI2Lib.InputUnit.ADF ? ScanContinueParam.getAvailableScanPageCount() : 1) + 1)) * ((long) (validScanSize[0] * validScanSize[1] * 3))) {
            return 2;
        }
        return 0;
    }

    private String probeScanner(Context context, ScannerPropertyWrapper scannerPropertyWrapper) throws EscanLibException {
        synchronized (this) {
            mCancelableScannerProbe = new CancelableScannerProbe();
        }
        String probeScanner = mCancelableScannerProbe.probeScanner(scannerPropertyWrapper, context);
        synchronized (this) {
            mCancelableScannerProbe = null;
        }
        return probeScanner;
    }

    private void convertFile() {
        mResScanFileList = mImageProcessController.getScanFiles(mScanI2Runner.getScanPages());
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public void scanProgressCallback(int i, int i2) {
        mImageProcessController.putScanResult(i, i2);
    }

    protected void onPostExecute(TaskError taskError) {
        ScanResultReceiver scanResultReceiver = mScanResultReceiver;
        if (scanResultReceiver != null) {
            scanResultReceiver.onScanEnd(taskError);
        }
    }

    public void cancelScan() {
        mCancelRequested = true;
        synchronized (this) {
            if (mCancelableScannerProbe != null) {
                mCancelableScannerProbe.cancel();
            }
        }
        mScanI2Runner.cancelScan();
        mImageProcessController.cancelThread();
    }

    private void checkCancel() throws LocalCancel {
        if (mCancelRequested) {
            throw new LocalCancel();
        }
    }

    private void onImageProcessEnd(int i, int i2) {
        ScanResultReceiver scanResultReceiver = mScanResultReceiver;
        if (scanResultReceiver != null) {
            scanResultReceiver.onProgressUpdate(i, i2);
        }
    }

    private class TaskObserver implements EscanI2Lib.Observer {
        private TaskObserver() {
        }

        /* synthetic */ TaskObserver(I2ScanTask i2ScanTask, C24451 r2) {
            this();
        }

        public void update(int i, int i2) {
            if (i2 > 0) {
                I2ScanTask.this.scanProgressCallback(i, i2 - 1);
            }
        }
    }

    public static class TaskError {
        public static final int ERROR_SD_CARD_NOT_MOUNTED = 2;
        public static final int ESC_I2_ERROR = 1;
        public static final int OTHER_ERROR = 2;
        public static final int OTHER_ERROR_INVALID_SCAN_AREA = 3;
        public int error1;
        public int error2;

        public TaskError(int i, int i2) {
            error1 = i;
            error2 = i2;
        }

        public boolean isCancel() {
            return error1 == 1 && error2 == 1;
        }

        public static boolean isInvalidScanArea(@Nullable TaskError taskError) {
            return taskError != null && taskError.error1 == 2 && taskError.error2 == 3;
        }
    }

    static class LocalCancel extends Throwable {
        LocalCancel() {
        }
    }
}
