package epson.scan.i2lib;

import android.content.Context;

import com.epson.lib.escani2.EscanI2Lib;
import com.epson.lib.escani2.I2LibScannerInfoAndCapability;
import com.epson.lib.escani2.ScanI2Params;
import com.epson.lib.escani2.ScanSize;
import com.epson.mobilephone.common.PrintingLib.define.Constants;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import epson.scan.activity.ScannerPropertyWrapper;
import epson.scan.lib.ScanCommonParams;
import epson.scan.lib.ScanSizeHelper;

public class I2ScanParamArbiter {
    private static final EscanI2Lib.InputUnit[] ALL_INPUT_UNITS = {EscanI2Lib.InputUnit.FLATBED, EscanI2Lib.InputUnit.ADF};
    private Selector<ScanSize> mAdfDuplexScanSizeSelector;
    private Selector<ScanSize> mAdfNonDuplexScanSizeSelector;
    private ScanSize mAltScanSize;
    private Selector<EscanI2Lib.ColorMode> mColorModeSelector;
    private int mDensity;
    private boolean mDensityChangeable;
    private Selector<Boolean> mDuplexSelector;
    private int mDuplexTurnDirection;
    private Selector<ScanSize> mFlatbedScanSizeSelector;
    private Selector<EscanI2Lib.Gamma> mGammaSelector;
    private Selector<EscanI2Lib.InputUnit> mInputUnitSelector;
    private int mManualAdfAlignmentI1Value;
    private Selector<Integer> mResolutionSelector;
    private Selector<ScanSize> mScanSizeSelector;
    private int mScannerAdfAlignment;

    public I2ScanParamArbiter() {
        init();
    }

    private void init() {
        mInputUnitSelector = new Selector<>();
        mInputUnitSelector.setCandidateArray(ALL_INPUT_UNITS);
        mFlatbedScanSizeSelector = new Selector<>();
        mFlatbedScanSizeSelector.setCandidateArray(new ScanSize[]{new ScanSize(ScanSize.PaperSize.MAX, true)});
        mScanSizeSelector = mFlatbedScanSizeSelector;
        mAltScanSize = null;
        mColorModeSelector = new Selector<>();
        mColorModeSelector.setCandidateArray(new EscanI2Lib.ColorMode[]{EscanI2Lib.ColorMode.COLOR_24BIT});
        mResolutionSelector = new Selector<>();
        setDefaultResolutionCandidate();
        mDuplexSelector = new Selector<>();
        mDuplexSelector.setCandidateArray(new Boolean[]{false});
        mGammaSelector = new Selector<>();
        mGammaSelector.setCandidateArray(new EscanI2Lib.Gamma[]{EscanI2Lib.Gamma.GAMMA_180});
        mManualAdfAlignmentI1Value = 0;
    }

    public EscanI2Lib.InputUnit[] getInputUnitCandidates() {
        return (EscanI2Lib.InputUnit[]) mInputUnitSelector.getCandidateArray();
    }

    public EscanI2Lib.InputUnit getInputUnit() {
        return mInputUnitSelector.getValue();
    }

    public int getInputUnitPosition() {
        return mInputUnitSelector.getPosition();
    }

    public void setInputUnitPosition(int i) {
        mInputUnitSelector.setPosition(i);
        if (getInputUnit() == EscanI2Lib.InputUnit.FLATBED && mDuplexSelector.getValue().booleanValue()) {
            mDuplexSelector.setSelectedValue(false);
        }
        selectSizeSelectorFromInputUnitAndDuplex(getInputUnit());
    }

    private void setInputUnit(EscanI2Lib.InputUnit inputUnit) {
        mInputUnitSelector.setSelectedValue(inputUnit);
        selectSizeSelectorFromInputUnitAndDuplex(inputUnit);
    }

    public boolean isAdfAlignmentMenuValid() {
        return mScannerAdfAlignment == 255;
    }

    public int getManualAdfAlignmentValue() {
        return mManualAdfAlignmentI1Value;
    }

    public void setManualAdfAlignmentValue(int i) {
        mManualAdfAlignmentI1Value = i;
    }

    private void selectSizeSelectorFromInputUnitAndDuplex(EscanI2Lib.InputUnit inputUnit) {
        ScanSize scanSize = getScanSize();
        if (inputUnit == EscanI2Lib.InputUnit.ADF) {
            mScanSizeSelector = getDuplex().booleanValue() ? mAdfDuplexScanSizeSelector : mAdfNonDuplexScanSizeSelector;
        } else {
            mScanSizeSelector = mFlatbedScanSizeSelector;
        }
        mAltScanSize = null;
        mScanSizeSelector.setSelectedValue(scanSize);
        checkAndAdjustScanSize();
    }

    private void checkAndAdjustScanSize() {
        if (getScanSizePosition() < 0) {
            mScanSizeSelector.setSelectedValue(ScanSize.getLocaleDefaultSize());
            if (mScanSizeSelector.getPosition() < 0) {
                mScanSizeSelector.setSelectedValue(new ScanSize(ScanSize.PaperSize.MAX, true));
                if (mScanSizeSelector.getPosition() < 0) {
                    mScanSizeSelector.setPosition(0);
                }
            }
        }
    }

    public ScanSize[] getScanSizeCandidates() {
        return (ScanSize[]) mScanSizeSelector.getCandidateArray();
    }

    public ScanSize getScanSize() {
        return mScanSizeSelector.getPosition() < 0 ? mAltScanSize : mScanSizeSelector.getValue();
    }

    public int getScanSizePosition() {
        return mScanSizeSelector.getPosition();
    }

    public void setScanSizePosition(int i) {
        mScanSizeSelector.setPosition(i);
        mAltScanSize = null;
    }

    public EscanI2Lib.ColorMode[] getColorModeCandidates() {
        return (EscanI2Lib.ColorMode[]) mColorModeSelector.getCandidateArray();
    }

    public EscanI2Lib.ColorMode getColorMode() {
        return mColorModeSelector.getValue();
    }

    public int getColorModePosition() {
        return mColorModeSelector.getPosition();
    }

    public void setColorModePosition(int i) {
        mColorModeSelector.setPosition(i);
    }

    public Integer[] getResolutionCandidates() {
        return (Integer[]) mResolutionSelector.getCandidateArray();
    }

    public Integer getResolution() {
        return mResolutionSelector.getValue();
    }

    public int getResolutionPosition() {
        return mResolutionSelector.getPosition();
    }

    public void setResolutionPosition(int i) {
        int position = mResolutionSelector.getPosition();
        mResolutionSelector.setPosition(i);
        if (position != i) {
            mAltScanSize = null;
            checkAndAdjustScanSize();
        }
    }

    public Boolean[] getDuplexCandidates() {
        return (Boolean[]) mDuplexSelector.getCandidateArray();
    }

    public Boolean getDuplex() {
        return mDuplexSelector.getValue();
    }

    public int getDuplexPosition() {
        return mDuplexSelector.getPosition();
    }

    public boolean isDuplexSupported() {
        for (Boolean booleanValue : (Boolean[]) mDuplexSelector.getCandidateArray()) {
            if (booleanValue.booleanValue()) {
                return true;
            }
        }
        return false;
    }

    public void setDuplexPosition(int i) {
        mDuplexSelector.setPosition(i);
        changeInputUnitFromDuplexIfNeeded();
    }

    public void setDuplexValue(boolean z) {
        mDuplexSelector.setSelectedValue(Boolean.valueOf(z));
        changeInputUnitFromDuplexIfNeeded();
    }

    private void changeInputUnitFromDuplexIfNeeded() {
        if (!mDuplexSelector.getValue().booleanValue() || mInputUnitSelector.getValue() != EscanI2Lib.InputUnit.FLATBED) {
            selectSizeSelectorFromInputUnitAndDuplex(getInputUnit());
        } else {
            setInputUnit(EscanI2Lib.InputUnit.ADF);
        }
    }

    public void setDuplexTurnDirection(int i) {
        mDuplexTurnDirection = i;
    }

    public int getDuplexTurnDirection() {
        return mDuplexTurnDirection;
    }

    public void setDensity(int i, boolean z) {
        mDensity = i;
        mDensityChangeable = z;
    }

    public int getDensity() {
        return mDensity;
    }

    public boolean getDensityChangeable() {
        return mDensityChangeable;
    }

    public EscanI2Lib.Gamma[] getGammaCandidates() {
        return (EscanI2Lib.Gamma[]) mGammaSelector.getCandidateArray();
    }

    public EscanI2Lib.Gamma getGamma() {
        return mGammaSelector.getValue();
    }

    public int getGammaPosition() {
        return mGammaSelector.getPosition();
    }

    public void setGammaPosition(int i) {
        mGammaSelector.setPosition(i);
    }

    public void notifyScannerChanged(Context context, I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability) {
        internalAdjust(i2LibScannerInfoAndCapability, ScanCommonParams.load(context));
        mManualAdfAlignmentI1Value = 0;
    }

    public void adjust(Context context, I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability) {
        internalAdjust(i2LibScannerInfoAndCapability, ScanCommonParams.load(context));
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public void internalAdjust(I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability, @NonNull ScanCommonParams scanCommonParams) {
        ScanI2Params defaultScanI2Params = I2ScanParamManager.getDefaultScanI2Params();
        mScannerAdfAlignment = i2LibScannerInfoAndCapability.getAlignment(EscanI2Lib.InputUnit.ADF);
        mColorModeSelector = new Selector<>();
        mColorModeSelector.setCandidateArray(new EscanI2Lib.ColorMode[]{EscanI2Lib.ColorMode.COLOR_24BIT, EscanI2Lib.ColorMode.GRAYSCALE_8BIT, EscanI2Lib.ColorMode.MONO_1BIT});
        mColorModeSelector.setSelectedValue(defaultScanI2Params.colorMode);
        mGammaSelector = new Selector<>();
        mGammaSelector.setCandidateArray(new EscanI2Lib.Gamma[]{EscanI2Lib.Gamma.GAMMA_100, EscanI2Lib.Gamma.GAMMA_180});
        mGammaSelector.setSelectedValue(defaultScanI2Params.userGamma);
        adjustResolution(i2LibScannerInfoAndCapability);
        setResolutionInitValue();
        adjustInputUnitSelector(i2LibScannerInfoAndCapability);
        mInputUnitSelector.setSelectedValue(scanCommonParams.getI2ScanSourceUnit());
        if (mInputUnitSelector.getPosition() < 0) {
            mInputUnitSelector.setPosition(0);
        }
        adjustDuplexSelector(i2LibScannerInfoAndCapability);
        adjustDuplexAndInputUnit(scanCommonParams.getI2Duplex());
        makeAllScanSizeSelector(i2LibScannerInfoAndCapability);
        mAltScanSize = null;
        selectSizeSelectorFromInputUnitAndDuplex(getInputUnit());
        mScanSizeSelector.setSelectedValue(defaultScanI2Params.scanSize);
        checkAndAdjustScanSize();
        mDensity = defaultScanI2Params.density;
        mDensityChangeable = defaultScanI2Params.densityChangeable;
    }

    public void makeSelectionList(@NonNull Context context, @NonNull I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability, @NonNull ScanI2Params scanI2Params) {
        adjust(context, i2LibScannerInfoAndCapability);
        mColorModeSelector.setSelectedValue(scanI2Params.colorMode);
        mGammaSelector.setSelectedValue(scanI2Params.userGamma);
        mResolutionSelector.setSelectedValue(Integer.valueOf(scanI2Params.resolutionMain));
        if (-1 == mResolutionSelector.getPosition()) {
            mResolutionSelector.setSelectedValue(150);
        }
        mInputUnitSelector.setSelectedValue(scanI2Params.inputUnit);
        mDuplexSelector.setSelectedValue(Boolean.valueOf(scanI2Params.duplex));
        setDuplexPosition(mDuplexSelector.getPosition());
        mDuplexTurnDirection = scanI2Params.duplexTurnDirection;
        mDensity = scanI2Params.density;
        mDensityChangeable = scanI2Params.densityChangeable;
        ScanSize scanSize = scanI2Params.scanSize;
        if (scanSize.isPixelSize()) {
            mScanSizeSelector.setPosition(-1);
            mAltScanSize = scanI2Params.scanSize;
        } else {
            mScanSizeSelector.setSelectedValue(scanSize);
            checkAndAdjustScanSize();
            mAltScanSize = null;
        }
        mManualAdfAlignmentI1Value = scanI2Params.manualAdfAlignment;
    }

    private void adjustInputUnitSelector(I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability) {
        ArrayList arrayList = new ArrayList();
        if (i2LibScannerInfoAndCapability == null) {
            arrayList.add(EscanI2Lib.InputUnit.FLATBED);
        } else {
            if (i2LibScannerInfoAndCapability.isFlatbedAvailable()) {
                arrayList.add(EscanI2Lib.InputUnit.FLATBED);
            }
            if (i2LibScannerInfoAndCapability.isAdfAvailable()) {
                arrayList.add(EscanI2Lib.InputUnit.ADF);
            }
        }
        if (arrayList.size() > 0) {
            mInputUnitSelector.setCandidateArray(arrayList.toArray(new EscanI2Lib.InputUnit[arrayList.size()]));
            return;
        }
        throw new RuntimeException();
    }

    private void adjustDuplexSelector(I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability) {
        if (i2LibScannerInfoAndCapability == null || !i2LibScannerInfoAndCapability.canDoubleSideScan()) {
            mDuplexSelector.setCandidateArray(new Boolean[]{false});
            return;
        }
        mDuplexSelector.setCandidateArray(new Boolean[]{true, false});
    }

    private void adjustDuplexAndInputUnit(boolean z) {
        if (((Boolean[]) mDuplexSelector.getCandidateArray()).length >= 2) {
            mDuplexSelector.setSelectedValue(Boolean.valueOf(z));
        } else {
            mDuplexSelector.setSelectedValue(false);
        }
        if (mDuplexSelector.getPosition() < 0) {
            mDuplexSelector.setPosition(0);
        }
    }

    private void adjustResolution(I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability) {
        if (i2LibScannerInfoAndCapability == null) {
            setDefaultResolutionCandidate();
            return;
        }
        int[] iArr = (int[]) i2LibScannerInfoAndCapability.scannerI2Capability.mainScanResolutions.clone();
        int[] iArr2 = i2LibScannerInfoAndCapability.scannerI2Capability.subScanResolutions;
        if (iArr == null || iArr2 == null) {
            setDefaultResolutionCandidate();
            return;
        }
        List<Integer> makeList = makeList(new int[]{75, 150, 300});
        List<Integer> makeList2 = makeList(iArr2);
        ArrayList arrayList = new ArrayList();
        Arrays.sort(iArr);
        for (int i : iArr) {
            if (makeList2.contains(Integer.valueOf(i)) && makeList.contains(Integer.valueOf(i))) {
                arrayList.add(Integer.valueOf(i));
            }
        }
        mResolutionSelector.setCandidateArray((Integer[]) arrayList.toArray(new Integer[arrayList.size()]));
    }

    private void setDefaultResolutionCandidate() {
        mResolutionSelector.setCandidateArray(new Integer[]{150});
    }

    private void setResolutionInitValue() {
        mResolutionSelector.setSelectedValue(150);
        if (-1 == mResolutionSelector.getPosition()) {
            mResolutionSelector.setPosition(0);
        }
    }

    private void makeAllScanSizeSelector(I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability) {
        if (i2LibScannerInfoAndCapability == null) {
            mFlatbedScanSizeSelector = new Selector<>();
            mFlatbedScanSizeSelector.setCandidateArray(new ScanSize[]{new ScanSize(ScanSize.PaperSize.MAX, true)});
            mScanSizeSelector = mFlatbedScanSizeSelector;
            return;
        }
        int sensorSizeBaseResolution = i2LibScannerInfoAndCapability.scannerI2Info.getSensorSizeBaseResolution();
        mFlatbedScanSizeSelector = makeScanSizeSelector(EscanI2Lib.InputUnit.FLATBED, false, i2LibScannerInfoAndCapability.getSensorSize(EscanI2Lib.InputUnit.FLATBED), sensorSizeBaseResolution);
        mAdfDuplexScanSizeSelector = makeScanSizeSelector(EscanI2Lib.InputUnit.ADF, true, i2LibScannerInfoAndCapability.getSensorSize(EscanI2Lib.InputUnit.ADF), sensorSizeBaseResolution);
        mAdfNonDuplexScanSizeSelector = makeScanSizeSelector(EscanI2Lib.InputUnit.ADF, false, i2LibScannerInfoAndCapability.getSensorSize(EscanI2Lib.InputUnit.ADF), sensorSizeBaseResolution);
    }

    private Selector<ScanSize> makeScanSizeSelector(EscanI2Lib.InputUnit inputUnit, boolean z, @Nullable int[] iArr, int i) {
        if (iArr == null) {
            Selector<ScanSize> selector = new Selector<>();
            selector.setCandidateArray(new ScanSize[]{new ScanSize(ScanSize.PaperSize.MAX, true)});
            return selector;
        }
        int[] supportedScanSizeList = ScanSizeHelper.getSupportedScanSizeList(inputUnit == EscanI2Lib.InputUnit.ADF ? 1 : 0, z ? 1 : 0, iArr[0], iArr[1], i);
        ArrayList arrayList = new ArrayList();
        for (int i2ScanSize : supportedScanSizeList) {
            ScanSize i2ScanSize2 = getI2ScanSize(i2ScanSize);
            if (i2ScanSize2 != null) {
                arrayList.add(i2ScanSize2);
            }
        }
        if (inputUnit != EscanI2Lib.InputUnit.ADF) {
            arrayList.add(new ScanSize(ScanSize.PaperSize.MAX, true));
        } else if (arrayList.isEmpty()) {
            arrayList.add(new ScanSize(ScanSize.PaperSize.MAX, true));
        }
        Selector<ScanSize> selector2 = new Selector<>();
        selector2.setCandidateArray((ScanSize[]) arrayList.toArray(new ScanSize[arrayList.size()]));
        return selector2;
    }

    public ScanI2Params getScanParam() {
        ScanI2Params scanI2Params = new ScanI2Params();
        int intValue = getResolution().intValue();
        scanI2Params.resolutionMain = intValue;
        scanI2Params.resolutionSub = intValue;
        scanI2Params.inputUnit = getInputUnit();
        scanI2Params.scanSize = getScanSize();
        scanI2Params.colorMode = getColorMode();
        scanI2Params.userGamma = getGamma();
        scanI2Params.duplex = getDuplex().booleanValue();
        scanI2Params.duplexTurnDirection = mDuplexTurnDirection;
        scanI2Params.density = mDensity;
        scanI2Params.densityChangeable = mDensityChangeable;
        scanI2Params.manualAdfAlignment = mManualAdfAlignmentI1Value;
        return scanI2Params;
    }

    public static ScanSize getI2ScanSize(int i) {
        if (i == Constants.PaperName.EPS_MSID_B5.getCode()) {
            return new ScanSize(ScanSize.PaperSize.B5, true);
        }
        if (i == Constants.PaperName.EPS_MSID_A4.getCode()) {
            return new ScanSize(ScanSize.PaperSize.A4, true);
        }
        if (i == Constants.PaperName.EPS_MSID_LETTER.getCode()) {
            return new ScanSize(ScanSize.PaperSize.LETTER, true);
        }
        if (i == Constants.PaperName.EPS_MSID_LEGAL.getCode()) {
            return new ScanSize(ScanSize.PaperSize.LEGAL, true);
        }
        if (i == Constants.PaperName.EPS_MSID_A3.getCode()) {
            return new ScanSize(ScanSize.PaperSize.A3, true);
        }
        return null;
    }

    private static List<Integer> makeList(int[] iArr) {
        ArrayList arrayList = new ArrayList(iArr.length);
        for (int valueOf : iArr) {
            arrayList.add(Integer.valueOf(valueOf));
        }
        return arrayList;
    }

    public void tmpReset() {
        init();
    }

    public static ScanI2Params getAdjustedParams(@NonNull Context context, @NonNull ScannerPropertyWrapper scannerPropertyWrapper) {
        I2LibScannerInfoAndCapability i2ScannerAllInfo = scannerPropertyWrapper.getI2ScannerAllInfo();
        I2ScanParamArbiter i2ScanParamArbiter = new I2ScanParamArbiter();
        i2ScanParamArbiter.adjust(context, i2ScannerAllInfo);
        return i2ScanParamArbiter.getScanParam();
    }

    private static class Selector<E> {
        public static final int UNKNOWN_POSITION = -1;
        private E[] mCandidates;
        private int mSelectedPosition = -1;

        public int getPosition() {
            return mSelectedPosition;
        }

        public void setPosition(int i) {
            mSelectedPosition = i;
        }

        public void setSelectedValue(E e) {
            setPosition(Arrays.asList(mCandidates).indexOf(e));
        }

        public E getValue() {
            int position = getPosition();
            if (position < 0) {
                position = 0;
            }
            return mCandidates[position];
        }

        public void setCandidateArray(E[] eArr) {
            mCandidates = eArr;
            if (eArr.length > 0) {
                setPosition(0);
            } else {
                setPosition(-1);
            }
        }

        public E[] getCandidateArray() {
            return mCandidates;
        }
    }
}
