package epson.scan.i2lib;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
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 epson.scan.activity.ScannerPropertyWrapper;
import epson.scan.lib.ScanCommonParams;
import epson.scan.lib.ScanSizeHelper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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() {
        this.mInputUnitSelector = new Selector<>();
        this.mInputUnitSelector.setCandidateArray(ALL_INPUT_UNITS);
        this.mFlatbedScanSizeSelector = new Selector<>();
        this.mFlatbedScanSizeSelector.setCandidateArray(new ScanSize[]{new ScanSize(ScanSize.PaperSize.MAX, true)});
        this.mScanSizeSelector = this.mFlatbedScanSizeSelector;
        this.mAltScanSize = null;
        this.mColorModeSelector = new Selector<>();
        this.mColorModeSelector.setCandidateArray(new EscanI2Lib.ColorMode[]{EscanI2Lib.ColorMode.COLOR_24BIT});
        this.mResolutionSelector = new Selector<>();
        setDefaultResolutionCandidate();
        this.mDuplexSelector = new Selector<>();
        this.mDuplexSelector.setCandidateArray(new Boolean[]{false});
        this.mGammaSelector = new Selector<>();
        this.mGammaSelector.setCandidateArray(new EscanI2Lib.Gamma[]{EscanI2Lib.Gamma.GAMMA_180});
        this.mManualAdfAlignmentI1Value = 0;
    }

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

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

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

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

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

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

    public int getManualAdfAlignmentValue() {
        return this.mManualAdfAlignmentI1Value;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    public int getDuplexTurnDirection() {
        return this.mDuplexTurnDirection;
    }

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

    public int getDensity() {
        return this.mDensity;
    }

    public boolean getDensityChangeable() {
        return this.mDensityChangeable;
    }

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

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

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

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

    public void notifyScannerChanged(Context context, I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability) {
        internalAdjust(i2LibScannerInfoAndCapability, ScanCommonParams.load(context));
        this.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();
        this.mScannerAdfAlignment = i2LibScannerInfoAndCapability.getAlignment(EscanI2Lib.InputUnit.ADF);
        this.mColorModeSelector = new Selector<>();
        this.mColorModeSelector.setCandidateArray(new EscanI2Lib.ColorMode[]{EscanI2Lib.ColorMode.COLOR_24BIT, EscanI2Lib.ColorMode.GRAYSCALE_8BIT, EscanI2Lib.ColorMode.MONO_1BIT});
        this.mColorModeSelector.setSelectedValue(defaultScanI2Params.colorMode);
        this.mGammaSelector = new Selector<>();
        this.mGammaSelector.setCandidateArray(new EscanI2Lib.Gamma[]{EscanI2Lib.Gamma.GAMMA_100, EscanI2Lib.Gamma.GAMMA_180});
        this.mGammaSelector.setSelectedValue(defaultScanI2Params.userGamma);
        adjustResolution(i2LibScannerInfoAndCapability);
        setResolutionInitValue();
        adjustInputUnitSelector(i2LibScannerInfoAndCapability);
        this.mInputUnitSelector.setSelectedValue(scanCommonParams.getI2ScanSourceUnit());
        if (this.mInputUnitSelector.getPosition() < 0) {
            this.mInputUnitSelector.setPosition(0);
        }
        adjustDuplexSelector(i2LibScannerInfoAndCapability);
        adjustDuplexAndInputUnit(scanCommonParams.getI2Duplex());
        makeAllScanSizeSelector(i2LibScannerInfoAndCapability);
        this.mAltScanSize = null;
        selectSizeSelectorFromInputUnitAndDuplex(getInputUnit());
        this.mScanSizeSelector.setSelectedValue(defaultScanI2Params.scanSize);
        checkAndAdjustScanSize();
        this.mDensity = defaultScanI2Params.density;
        this.mDensityChangeable = defaultScanI2Params.densityChangeable;
    }

    public void makeSelectionList(@NonNull Context context, @NonNull I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability, @NonNull ScanI2Params scanI2Params) {
        adjust(context, i2LibScannerInfoAndCapability);
        this.mColorModeSelector.setSelectedValue(scanI2Params.colorMode);
        this.mGammaSelector.setSelectedValue(scanI2Params.userGamma);
        this.mResolutionSelector.setSelectedValue(Integer.valueOf(scanI2Params.resolutionMain));
        if (-1 == this.mResolutionSelector.getPosition()) {
            this.mResolutionSelector.setSelectedValue(150);
        }
        this.mInputUnitSelector.setSelectedValue(scanI2Params.inputUnit);
        this.mDuplexSelector.setSelectedValue(Boolean.valueOf(scanI2Params.duplex));
        setDuplexPosition(this.mDuplexSelector.getPosition());
        this.mDuplexTurnDirection = scanI2Params.duplexTurnDirection;
        this.mDensity = scanI2Params.density;
        this.mDensityChangeable = scanI2Params.densityChangeable;
        ScanSize scanSize = scanI2Params.scanSize;
        if (scanSize.isPixelSize()) {
            this.mScanSizeSelector.setPosition(-1);
            this.mAltScanSize = scanI2Params.scanSize;
        } else {
            this.mScanSizeSelector.setSelectedValue(scanSize);
            checkAndAdjustScanSize();
            this.mAltScanSize = null;
        }
        this.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) {
            this.mInputUnitSelector.setCandidateArray(arrayList.toArray(new EscanI2Lib.InputUnit[arrayList.size()]));
            return;
        }
        throw new RuntimeException();
    }

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

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

    /* access modifiers changed from: package-private */
    public 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));
            }
        }
        this.mResolutionSelector.setCandidateArray((Integer[]) arrayList.toArray(new Integer[arrayList.size()]));
    }

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

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

    private void makeAllScanSizeSelector(I2LibScannerInfoAndCapability i2LibScannerInfoAndCapability) {
        if (i2LibScannerInfoAndCapability == null) {
            this.mFlatbedScanSizeSelector = new Selector<>();
            this.mFlatbedScanSizeSelector.setCandidateArray(new ScanSize[]{new ScanSize(ScanSize.PaperSize.MAX, true)});
            this.mScanSizeSelector = this.mFlatbedScanSizeSelector;
            return;
        }
        int sensorSizeBaseResolution = i2LibScannerInfoAndCapability.scannerI2Info.getSensorSizeBaseResolution();
        this.mFlatbedScanSizeSelector = makeScanSizeSelector(EscanI2Lib.InputUnit.FLATBED, false, i2LibScannerInfoAndCapability.getSensorSize(EscanI2Lib.InputUnit.FLATBED), sensorSizeBaseResolution);
        this.mAdfDuplexScanSizeSelector = makeScanSizeSelector(EscanI2Lib.InputUnit.ADF, true, i2LibScannerInfoAndCapability.getSensorSize(EscanI2Lib.InputUnit.ADF), sensorSizeBaseResolution);
        this.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 = this.mDuplexTurnDirection;
        scanI2Params.density = this.mDensity;
        scanI2Params.densityChangeable = this.mDensityChangeable;
        scanI2Params.manualAdfAlignment = this.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 this.mSelectedPosition;
        }

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

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

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

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

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