package epson.scan.lib;


import com.epson.lib.escani2.I2LibScannerInfoAndCapability;
import com.epson.lib.escani2.ScanI2PropertyTaker;

import epson.print.MyPrinter;
import epson.scan.activity.ScannerPropertyWrapper;

public class CancelablePropertyTaker {
    private volatile boolean mCancelRequested;
    private escanLib mEscanLib;

    public CancelablePropertyTaker(@NonNull escanLib escanlib) {
        mEscanLib = escanlib;
    }

    @Nullable
    public ScannerPropertyWrapper getScannerProperty(MyPrinter myPrinter) throws EscanLibException {
        ScannerPropertyWrapper scannerPropertyWrapper;
        ScannerInfo instance = ScannerInfo.getInstance();
        instance.setLocation(myPrinter.getLocation());
        instance.setModelName(myPrinter.getName());
        instance.setScannerId(myPrinter.getScannerId());
        instance.setIp(myPrinter.getIp());
        String scannerId = instance.getScannerId();
        if (scannerId == null || scannerId.isEmpty()) {
            scannerId = null;
        }
        mEscanLib.resetEscanLib();
        if (mCancelRequested) {
            return null;
        }
        int probeScannerByIp = mEscanLib.probeScannerByIp(scannerId, instance.getIp());
        if (probeScannerByIp != 0) {
            throw new EscanLibException(probeScannerByIp);
        } else if (mCancelRequested) {
            return null;
        } else {
            if (instance.getScannerId() == null || instance.getScannerId().length() <= 0) {
                instance.setScannerId(mEscanLib.getListFoundScanner().get(0).getScannerId());
            }
            int escIVersion = mEscanLib.getEscIVersion(0);
            if (mCancelRequested) {
                return null;
            }
            if (escIVersion != 2) {
                int i1SupportedOptions = ScanSettingHelper.getI1SupportedOptions(mEscanLib, instance);
                if (i1SupportedOptions == 0) {
                    scannerPropertyWrapper = new ScannerPropertyWrapper(instance);
                } else {
                    throw new EscanLibException(i1SupportedOptions);
                }
            } else {
                ScanI2PropertyTaker scanI2PropertyTaker = new ScanI2PropertyTaker();
                I2LibScannerInfoAndCapability scannerInfoAndCapability = scanI2PropertyTaker.getScannerInfoAndCapability(myPrinter.getIp());
                if (scannerInfoAndCapability != null) {
                    scannerPropertyWrapper = new ScannerPropertyWrapper(instance, scannerInfoAndCapability);
                } else {
                    throw new EscanLibException(2, scanI2PropertyTaker.getEscanI2LibError());
                }
            }
            if (mCancelRequested) {
                return null;
            }
            return scannerPropertyWrapper;
        }
    }

    public void cancel() {
        mCancelRequested = true;
        mEscanLib.escanWrapperCancelFindScanner();
    }

    @Nullable
    public static ScannerPropertyWrapper getScannerProperty(escanLib escanlib, MyPrinter myPrinter) throws EscanLibException {
        return new CancelablePropertyTaker(escanlib).getScannerProperty(myPrinter);
    }
}
