package epson.scan.lib;

import android.content.Context;
import android.graphics.Point;


import com.epson.mobilephone.common.PrintingLib.define.Constants;
import com.epson.mobilephone.common.wifidirect.MacAddrUtils;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;

import java.io.IOException;
import java.util.Arrays;

import epson.common.Utils;
import epson.print.MyPrinter;
import epson.print.R;
import epson.print.Util.EPLog;
import epson.scan.activity.ScannerPropertyWrapper;
import epson.scan.i2lib.I2ScanParamArbiter;
import epson.scan.i2lib.I2ScanParamManager;

public class ScanSettingHelper {
    private static final String TAG = "ScanSettingHelper";

    public static void saveSupportedOptionToRef(Context context, ScannerInfo scannerInfo) {
        ScanInfoStorage.saveSelectedScannerSupportedOptions(context, scannerInfo.getSupportedOptions());
        saveSelectedScannerSupportedResolutionList(context, scannerInfo.getSupportedResolutionList());
    }

    public static void saveAllUserSettingsToRef(Context context, ScannerInfo scannerInfo) {
        ScanInfoStorage.saveScannerConnectivityInfo(context, scannerInfo.getModelName(), scannerInfo.getIp(), scannerInfo.getScannerId(), scannerInfo.getLocation());
        ScanInfoStorage.saveSettings(context, scannerInfo);
        saveScanDefaultMaxSize(context, scannerInfo);
    }

    private static void saveSelectedScannerSupportedResolutionList(Context context, int[] iArr) {
        for (int i = 0; i < iArr.length; i++) {
            int i2 = iArr[i];
            if (i2 == 75 || i2 == 150 || i2 == 300) {
                Utils.savePref(context, Constants.SCAN_REFS_SUPPORTED_OPTIONS, Constants.SCAN_REFS_OPTIONS_RESOLUTION_ + i, iArr[i]);
            }
        }
    }

    public static void saveScanDefaultMaxSize(Context context, ScannerInfo scannerInfo) {
        int i;
        int i2;
        int supportedBasicResolution = scannerInfo.getSupportedBasicResolution();
        int resolutionValue = scannerInfo.getResolutionValue();
        int i3 = Constants.SCAN_MAX_HEIGHT;
        int i4 = Constants.SCAN_MAX_WIDTH;
        if (supportedBasicResolution > 0) {
            if (scannerInfo.getSourceValue() == 1) {
                Point maxScanSize = ScanSizeHelper.getMaxScanSize(scannerInfo.getSourceValue(), scannerInfo.getTwoSidedScanningValue(), scannerInfo.getSupportedAdfWidth(), scannerInfo.getSupportedAdfHeight(), supportedBasicResolution);
                i = maxScanSize.x;
                i2 = maxScanSize.y;
            } else {
                i = scannerInfo.getSupportedBasicWidth();
                i2 = scannerInfo.getSupportedBasicHeight();
            }
            if (resolutionValue > supportedBasicResolution) {
                int i5 = resolutionValue / supportedBasicResolution;
                i *= i5;
                i2 *= i5;
            } else if (resolutionValue < supportedBasicResolution) {
                int i6 = supportedBasicResolution / resolutionValue;
                i /= i6;
                i2 /= i6;
            }
            if (i != 0) {
                i4 = i;
            }
            if (i2 != 0) {
                i3 = i2;
            }
        }
        scannerInfo.setMaxWidth(i4);
        scannerInfo.setMaxHeight(i3);
        Utils.savePref(context, Constants.SCAN_REFS_SUPPORTED_OPTIONS, Constants.SCAN_REFS_MAX_WIDTH, scannerInfo.getMaxWidth());
        Utils.savePref(context, Constants.SCAN_REFS_SUPPORTED_OPTIONS, Constants.SCAN_REFS_MAX_HEIGHT, scannerInfo.getMaxHeight());
    }

    public static void saveErroredBlankSettings(Context context) {
        Utils.savePref(context, Constants.SCAN_REFS_SUPPORTED_OPTIONS, Constants.SCAN_REFS_OPTIONS_VALIDATION, false);
        ScanInfoStorage.saveScannerConnectivityInfo(context, context.getString(R.string.str_lbl_title_scan), "", "", 0);
        Utils.savePref(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_SOURCE, 0);
        Utils.savePref(context, Constants.SCAN_REFS_SUPPORTED_OPTIONS, Constants.SCAN_REFS_OPTIONS_ADF_HEIGHT, 0);
        Utils.savePref(context, Constants.SCAN_REFS_SUPPORTED_OPTIONS, Constants.SCAN_REFS_OPTIONS_ADF_WIDTH, 0);
        Utils.savePref(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SCANNER_CHOSEN_SIZE_ADF, Constants.PaperName.EPS_MSID_UNKNOWN.getCode());
        Utils.savePref(context, epson.common.Constants.SCAN_REFS_USED_SCANNER_PATH, epson.common.Constants.SCAN_REFS_SCANNER_CHOSEN_SIZE_DOC, Constants.PaperName.EPS_MSID_UNKNOWN.getCode());
        Utils.savePref(context, epson.common.Constants.SCAN_REFS_USED_SCANNER_PATH, epson.common.Constants.SCAN_REFS_SCANNER_CHOSEN_SIZE, Constants.PaperName.EPS_MSID_UNKNOWN.getCode());
        Utils.savePref(context, epson.common.Constants.SCAN_REFS_USED_SCANNER_PATH, epson.common.Constants.SCAN_REFS_SETTINGS_COLOR, 1);
        Utils.savePref(context, epson.common.Constants.SCAN_REFS_USED_SCANNER_PATH, epson.common.Constants.SCAN_REFS_SETTINGS_RESOLUTION, 150);
        Utils.savePref(context, epson.common.Constants.SCAN_REFS_SUPPORTED_OPTIONS, epson.common.Constants.SCAN_REFS_MAX_WIDTH, epson.common.Constants.SCAN_MAX_WIDTH);
        Utils.savePref(context, epson.common.Constants.SCAN_REFS_SUPPORTED_OPTIONS, epson.common.Constants.SCAN_REFS_MAX_HEIGHT, epson.common.Constants.SCAN_MAX_HEIGHT);
    }

    public static int getI1SupportedOptions(escanLib escanlib, ScannerInfo scannerInfo) {
        if (escanlib.escanWrapperSetScanner(0) != 0) {
            return -1300;
        }
        int GetSupportedOption = escanlib.GetSupportedOption();
        scannerInfo.setSupportedOptions(escanlib.getEscanSupportOption());
        scannerInfo.setSupportedResolutionList(escanlib.getEscanSupportResolution());
        updateNewSelectedScannerSupportedOptions(scannerInfo);
        return GetSupportedOption;
    }

    public static void updateNewSelectedScannerSupportedOptions(ScannerInfo scannerInfo) {
        scannerInfo.setVersion(scannerInfo.getSupportedOptions()[0]);
        scannerInfo.setSupportedBasicResolution(scannerInfo.getSupportedOptions()[1]);
        scannerInfo.setSupportedBasicWidth(scannerInfo.getSupportedOptions()[2]);
        scannerInfo.setSupportedBasicHeight(scannerInfo.getSupportedOptions()[3]);
        scannerInfo.setSupportedAdfWidth(scannerInfo.getSupportedOptions()[4]);
        scannerInfo.setSupportedAdfHeight(scannerInfo.getSupportedOptions()[5]);
        scannerInfo.setSupportedAdfDuplex(scannerInfo.getSupportedOptions()[6]);
    }

    public static void setDefaultSettings2(@NonNull Context context, @NonNull ScanInfoStorage scanInfoStorage, @NonNull ScannerInfo scannerInfo) {
        scannerInfo.setSourceValue(0);
        scannerInfo.setTwoSidedScanningValue(0);
        scannerInfo.setAdfDuplexRotaitonYes(false);
        if (scannerInfo.getSupportedAdfHeight() > 0 && scannerInfo.getSupportedAdfWidth() > 0) {
            scannerInfo.setSourceValue(1);
            if (scanInfoStorage.getScanSourceUnit(context, 0) != 1) {
                int scanSourceUnit = scanInfoStorage.getScanSourceUnit(context, -1);
                if (scanSourceUnit != -1) {
                    scannerInfo.setSourceValue(scanSourceUnit);
                }
            } else if (scannerInfo.getSupportedAdfDuplex() == 1) {
                scannerInfo.setTwoSidedScanningValue(scanInfoStorage.getTwoSideValue(context));
            }
        }
        int[] iArr = (int[]) scannerInfo.getSupportedResolutionList().clone();
        Arrays.sort(iArr);
        if (Arrays.binarySearch(iArr, 150) > 0) {
            scannerInfo.setResolutionValue(150);
        } else if (Arrays.binarySearch(iArr, 75) > 0) {
            scannerInfo.setResolutionValue(75);
        } else if (Arrays.binarySearch(iArr, 300) > 0) {
            scannerInfo.setResolutionValue(300);
        } else {
            scannerInfo.setResolutionValue(0);
        }
        scannerInfo.setColorValue(1);
        scannerInfo.setDensityStatus(false);
        scannerInfo.setDensityValue(128);
        scannerInfo.setGammaValue(1);
    }

    public static void setDefaultSettings3(@NonNull ScanCommonParams scanCommonParams, @NonNull ScannerInfo scannerInfo) {
        scannerInfo.setSourceValue(0);
        scannerInfo.setTwoSidedScanningValue(0);
        scannerInfo.setAdfDuplexRotaitonYes(false);
        if (scannerInfo.getSupportedAdfHeight() > 0 && scannerInfo.getSupportedAdfWidth() > 0) {
            scannerInfo.setSourceValue(1);
            if (scanCommonParams.getScanSourceUnit() != 1) {
                scannerInfo.setSourceValue(scanCommonParams.getScanSourceUnit());
            } else if (scannerInfo.getSupportedAdfDuplex() == 1) {
                scannerInfo.setTwoSidedScanningValue(scanCommonParams.getTwoSideValue());
            }
        }
        int[] iArr = (int[]) scannerInfo.getSupportedResolutionList().clone();
        Arrays.sort(iArr);
        if (Arrays.binarySearch(iArr, 150) > 0) {
            scannerInfo.setResolutionValue(150);
        } else if (Arrays.binarySearch(iArr, 75) > 0) {
            scannerInfo.setResolutionValue(75);
        } else if (Arrays.binarySearch(iArr, 300) > 0) {
            scannerInfo.setResolutionValue(300);
        } else {
            scannerInfo.setResolutionValue(0);
        }
        scannerInfo.setColorValue(1);
        scannerInfo.setDensityStatus(false);
        scannerInfo.setDensityValue(128);
        scannerInfo.setGammaValue(1);
    }

    private static void changeScanner(Context context, ScannerInfo scannerInfo) {
        String prefString = Utils.getPrefString(context, epson.common.Constants.SCAN_REFS_USED_SCANNER_PATH, epson.common.Constants.SCAN_REFS_SCANNER_ID);
        int prefInt = Utils.getPrefInt(context, epson.common.Constants.SCAN_REFS_USED_SCANNER_PATH, epson.common.Constants.SCAN_REFS_SCANNER_LOCATION);
        if (prefString == null || !prefString.equals(scannerInfo.getScannerId()) || prefInt != scannerInfo.getLocation()) {
            EPLog.d(TAG, "changeScanner scannerId=" + scannerInfo.getScannerId());
            saveAllUserSettingsToRef(context, scannerInfo);
            saveSupportedOptionToRef(context, scannerInfo);
            String curConnectInfo = WiFiDirectManager.getCurConnectInfo(context, MacAddrUtils.getMacAddressFromScannerId(scannerInfo.getScannerId()));
            if (scannerInfo.getLocation() != 1 || curConnectInfo == null) {
                WiFiDirectManager.resetConnectInfo(context, WiFiDirectManager.DEVICE_TYPE_SCANNER);
            } else {
                WiFiDirectManager.setConnectInfo(context, curConnectInfo, WiFiDirectManager.DEVICE_TYPE_SCANNER, scannerInfo.getModelName());
            }
        }
    }

    public static ScannerPropertyWrapper recodeScannerInfo(escanLib escanlib, Context context, MyPrinter myPrinter) throws EscanLibException, IOException {
        ScannerPropertyWrapper scannerProperty = CancelablePropertyTaker.getScannerProperty(escanlib, myPrinter);
        if (scannerProperty != null) {
            adjustAndSaveScanParams(context, scannerProperty);
            return scannerProperty;
        }
        throw new EscanLibException(-1001);
    }

    @VisibleForTesting
    static void adjustAndSaveScanParams(@NonNull Context context, @NonNull ScannerPropertyWrapper scannerPropertyWrapper) throws IOException {
        if (scannerPropertyWrapper.getEscIVersion() == 2) {
            I2ScanParamManager.saveParams(context, I2ScanParamArbiter.getAdjustedParams(context, scannerPropertyWrapper));
            scannerPropertyWrapper.updateSetSimpleApSsid(context);
            scannerPropertyWrapper.saveData(context, true);
            return;
        }
        ScannerInfo i1ScannerInfo = scannerPropertyWrapper.getI1ScannerInfo();
        setDefaultSettings3(ScanCommonParams.load(context), i1ScannerInfo);
        scannerPropertyWrapper.saveData(context, true);
        changeScanner(context, i1ScannerInfo);
    }
}
