package com.epson.lib.escani2;




import com.epson.lib.escani2.EscanI2Lib;

import epson.scan.i2lib.I2ScanParamArbiter;
import epson.scan.lib.ScanSizeHelper;

import java.io.Serializable;

public class ScanSize implements Serializable {
    private int mOffsetX;
    private int mOffsetY;
    private int mPixelHeight;
    private int mPixelWidth;
    private boolean mPortrait;
    private PaperSize mSizeType;

    public enum PaperSize {
        A4,
        LETTER,
        LEGAL,
        B5,
        B6,
        A5,
        A6,
        A8,
        MEISHI,
        PCARD,
        AUTO,
        A3,
        MAX,
        PIXEL
    }

    private static int getI2AlignmentValue(int i) {
        return i != 1 ? 2 : 1;
    }

    static int[] get300DpiPixelFromPaperSize(PaperSize paperSize) {
        switch (paperSize) {
            case A3:
                return new int[]{3507, 4959};
            case A4:
                return new int[]{2480, 3507};
            case LETTER:
                return new int[]{2550, 3300};
            case LEGAL:
                return new int[]{2550, 4200};
            case B5:
                return new int[]{2149, 3035};
            case B6:
                return new int[]{1512, 2150};
            case A5:
                return new int[]{1748, 2480};
            case A6:
                return new int[]{1240, 1748};
            case A8:
                return new int[]{615, 874};
            case MEISHI:
                return new int[]{1051, 650};
            case PCARD:
                return new int[]{1011, 638};
            case AUTO:
                return new int[]{2550, 4200};
            default:
                return new int[]{-1, -1};
        }
    }

    public ScanSize(PaperSize paperSize, boolean z) {
        mSizeType = paperSize;
        mPortrait = z;
    }

    public ScanSize(int i, int i2, int i3, int i4) {
        mSizeType = PaperSize.PIXEL;
        mPixelWidth = i;
        mPixelHeight = i2;
        mOffsetX = i3;
        mOffsetY = i4;
        mPortrait = true;
    }

    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof ScanSize)) {
            return false;
        }
        ScanSize scanSize = (ScanSize) obj;
        if (!mSizeType.equals(scanSize.mSizeType)) {
            return false;
        }
        switch (mSizeType) {
            case PIXEL:
                if (mPixelWidth == scanSize.mPixelWidth && mPixelHeight == scanSize.mPixelHeight && mOffsetX == scanSize.mOffsetX && mOffsetY == scanSize.mOffsetY) {
                    return true;
                }
                return false;
            case MAX:
                return true;
            default:
                return mPortrait;
        }
    }

    public int hashCode() {
        if (mSizeType == PaperSize.PIXEL) {
            return (((((((mSizeType.hashCode() * 31) + mPixelWidth) * 31) + mPixelHeight) * 31) + mOffsetX) * 31) + mOffsetY;
        }
        return mSizeType.hashCode();
    }

    public PaperSize getSize() {
        return mSizeType;
    }

    public boolean isPixelSize() {
        return mSizeType == PaperSize.PIXEL;
    }

    public int[] getValidScanSize(ScannerI2Info scannerI2Info, ScanI2Params scanI2Params) {
        int[] validScanSizeInternal = getValidScanSizeInternal(scannerI2Info, scanI2Params);
        if (validScanSizeInternal == null) {
            return null;
        }
        return adjustSizeFromScannerInfo(validScanSizeInternal, scannerI2Info, scanI2Params);
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public int[] adjustSizeFromScannerInfo(@NonNull int[] iArr, @NonNull ScannerI2Info scannerI2Info, @NonNull ScanI2Params scanI2Params) {
        int[] sensorPixelSize = scannerI2Info.getSensorPixelSize(scanI2Params.inputUnit, scanI2Params.resolutionMain, scanI2Params.resolutionSub);
        if (iArr[0] > sensorPixelSize[0]) {
            iArr[0] = sensorPixelSize[0];
        }
        int i = (iArr[0] + iArr[2]) - sensorPixelSize[0];
        if (i > 0) {
            iArr[2] = iArr[2] - i;
        }
        if (iArr[1] > sensorPixelSize[1]) {
            iArr[1] = sensorPixelSize[1];
        }
        int i2 = (iArr[1] + iArr[3]) - sensorPixelSize[1];
        if (i2 > 0) {
            iArr[3] = iArr[3] - i2;
        }
        return iArr;
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public int[] getValidScanSizeInternal(ScannerI2Info scannerI2Info, ScanI2Params scanI2Params) {
        if (scannerI2Info.isAdfAlignmentInvalid()) {
            scannerI2Info.adfAlignment = getI2AlignmentValue(scanI2Params.manualAdfAlignment);
        }
        if (!isPixelSize()) {
            return getSize(scannerI2Info, scanI2Params);
        }
        if (scanI2Params.inputUnit != EscanI2Lib.InputUnit.ADF) {
            return getLeftAlignPixelSizeValidScanSize(scannerI2Info, scanI2Params);
        }
        if (scannerI2Info.adfAlignment == 1) {
            return getCenterAlignPixelSizeValidScanSize(scannerI2Info, scanI2Params);
        }
        return getLeftAlignPixelSizeValidScanSize(scannerI2Info, scanI2Params);
    }

    private int[] getCenterAlignPixelSizeValidScanSize(ScannerI2Info scannerI2Info, ScanI2Params scanI2Params) {
        int[] size = getSize(scannerI2Info, scanI2Params);
        size[2] = 0;
        size[3] = 0;
        int[] sensorPixelSize = scannerI2Info.getSensorPixelSize(scanI2Params.inputUnit, scanI2Params.resolutionMain, scanI2Params.resolutionSub);
        if (size[0] > sensorPixelSize[0]) {
            size[0] = sensorPixelSize[0];
        } else {
            size[2] = (sensorPixelSize[0] - size[0]) / 2;
        }
        if (size[1] > sensorPixelSize[1]) {
            size[1] = sensorPixelSize[1];
        }
        return size;
    }

    private int[] getLeftAlignPixelSizeValidScanSize(ScannerI2Info scannerI2Info, ScanI2Params scanI2Params) {
        int[] size = getSize(scannerI2Info, scanI2Params);
        int[] sensorPixelSize = scannerI2Info.getSensorPixelSize(scanI2Params.inputUnit, scanI2Params.resolutionMain, scanI2Params.resolutionSub);
        if (size[0] + size[2] <= 0 || size[1] + size[3] <= 0 || size[2] >= sensorPixelSize[0] || size[3] >= sensorPixelSize[1]) {
            return null;
        }
        if (size[2] < 0) {
            size[0] = size[2] + size[0];
            size[2] = 0;
        }
        size[0] = size[0] + size[2] >= sensorPixelSize[0] ? sensorPixelSize[0] - size[2] : size[0];
        if (size[3] < 0) {
            size[1] = size[3] + size[1];
            size[3] = 0;
        }
        size[1] = size[1] + size[3] >= sensorPixelSize[1] ? sensorPixelSize[1] - size[3] : size[1];
        return size;
    }

    public int[] getSize(ScannerI2Info scannerI2Info, ScanI2Params scanI2Params) {
        return getSize(scannerI2Info, scanI2Params.inputUnit, scanI2Params.resolutionMain, scanI2Params.resolutionSub);
    }

    private int[] getSize(ScannerI2Info scannerI2Info, EscanI2Lib.InputUnit inputUnit, int i, int i2) {
        int[] pixel = getPixel(scannerI2Info, inputUnit, i, i2);
        int[] offset = getOffset(scannerI2Info, inputUnit, i, i2);
        return new int[]{pixel[0], pixel[1], offset[0], offset[1]};
    }

    private int[] getPixel(ScannerI2Info scannerI2Info, EscanI2Lib.InputUnit inputUnit, int i, int i2) {
        if (mSizeType == PaperSize.MAX) {
            return scannerI2Info.getSensorPixelSize(inputUnit, i, i2);
        }
        if (mSizeType == PaperSize.PIXEL) {
            return new int[]{mPixelWidth, mPixelHeight};
        }
        int[] iArr = get300DpiPixelFromPaperSize(mSizeType);
        if (!mPortrait) {
            int i3 = iArr[0];
            iArr[0] = iArr[1];
            iArr[1] = i3;
        }
        if (i != 300) {
            iArr[0] = (((iArr[0]) * (i)) / (300));
        }
        if (i2 != 300) {
            iArr[1] = (((iArr[1]) * (i2)) / (300));
        }
        return iArr;
    }

    private int[] getOffset(ScannerI2Info scannerI2Info, EscanI2Lib.InputUnit inputUnit, int i, int i2) {
        if (mSizeType == PaperSize.PIXEL) {
            return new int[]{mOffsetX, mOffsetY};
        } else if (mSizeType == PaperSize.MAX) {
            return new int[]{0, 0};
        } else {
            int[] iArr = {0, 0};
            iArr[0] = calcOffset(scannerI2Info.getAlignment(inputUnit), getPixel(scannerI2Info, inputUnit, i, i2)[0], Math.round(((scannerI2Info.getSensorSize(inputUnit)[0]) * (i)) / (scannerI2Info.getSensorSizeBaseResolution())));
            return iArr;
        }
    }

    private int calcOffset(int i, int i2, int i3) {
        switch (i) {
            case 1:
                return (i3 - i2) / 2;
            case 2:
                return i3 - i2;
            default:
                return 0;
        }
    }

    public static ScanSize getLocaleDefaultSize() {
        return I2ScanParamArbiter.getI2ScanSize(ScanSizeHelper.getDefaultScanSize());
    }
}
