package com.epson.cameracopy.printlayout;

import android.graphics.Rect;
import com.epson.cameracopy.printlayout.PreviewView;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class PreviewPosition implements Serializable {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    public static final double MAX_IMAGE_RATE = 4.0d;
    public static final double MIN_IMAGE_RATE = 0.25d;
    private static final int MOVABLE_PREVIE_PIXEL = 10;
    private static final long serialVersionUID = 1;
    private int m3mPrintAreaActualHeight;
    private int m3mPrintAreaActualWidth;
    private double mActualPreviewRate;
    private int mBorderlessDataHeight;
    private int mBorderlessDataWidth;
    private int mBorderlessLeftMargin;
    private boolean mBorderlessMode;
    private int mBorderlessTopMargin;
    private boolean mFitSizeMode;
    private int mPaperActualHeight = 297;
    private int mPaperActualWidth = 210;
    private int mPaperTo3mActualLeftMargin;
    private int mPaperTo3mActualTopMargin;
    private int[] mPreviewMovableArea = new int[4];
    private double mPreviewPaperRate = 0.98d;
    private transient Rect mPreviewPaperRect = new Rect();
    private int mPrintTargetAddRotation;
    private int mPrtimageActualHeight;
    private int mPrtimageActualWidth;
    private final PreviewRect mPrtimagePreviewPosition = new PreviewRect();
    private transient Rect mScaledPrtimagePreviewRect = new Rect();
    private int mScreenHeight;
    private int mScreenWidth;

    public PreviewPosition() {
    }

    public PreviewPosition(double d) {
        this.mPreviewPaperRate = d;
    }

    public void setPrtimageActualSize(int i, int i2) {
        this.mPrtimageActualWidth = i;
        this.mPrtimageActualHeight = i2;
    }

    public int[] getPrtImageActualSIze() {
        return new int[]{this.mPrtimageActualWidth, this.mPrtimageActualHeight};
    }

    public void setScreenSize(int i, int i2) {
        this.mScreenWidth = i;
        this.mScreenHeight = i2;
    }

    public void changeScreenSize(int i, int i2) {
        if (this.mActualPreviewRate <= 0.0d || i <= 0 || i2 <= 0) {
            return;
        }
        if (this.mScreenWidth != i || this.mScreenHeight != i2) {
            this.mScreenWidth = i;
            this.mScreenHeight = i2;
            double left = ((double) (this.mPrtimagePreviewPosition.getLeft() - this.mPreviewPaperRect.left)) / this.mActualPreviewRate;
            double top = ((double) (this.mPrtimagePreviewPosition.getTop() - this.mPreviewPaperRect.top)) / this.mActualPreviewRate;
            calcPreviewPaperSize();
            double d = this.mActualPreviewRate;
            this.mPrtimagePreviewPosition.setX1Size((int) (((double) this.mPrtimageActualWidth) * d), (int) (((double) this.mPrtimageActualHeight) * d));
            this.mPrtimagePreviewPosition.setLeft(this.mPreviewPaperRect.left + ((int) (left * this.mActualPreviewRate)));
            this.mPrtimagePreviewPosition.setTop(this.mPreviewPaperRect.top + ((int) (top * this.mActualPreviewRate)));
            setMovableArea();
        }
    }

    public void setPaperSize(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10) {
        this.mPaperActualWidth = i;
        this.mPaperActualHeight = i2;
        this.mPaperTo3mActualLeftMargin = i3;
        this.mPaperTo3mActualTopMargin = i4;
        this.m3mPrintAreaActualWidth = i5;
        this.m3mPrintAreaActualHeight = i6;
        this.mBorderlessLeftMargin = i7;
        this.mBorderlessTopMargin = i8;
        this.mBorderlessDataWidth = i9;
        this.mBorderlessDataHeight = i10;
    }

    public int[] getPaperMaginAnd3mPrintableArea() {
        double d = this.mActualPreviewRate;
        return new int[]{(int) (((double) this.mPaperTo3mActualLeftMargin) * d), (int) (((double) this.mPaperTo3mActualTopMargin) * d), (int) (((double) this.m3mPrintAreaActualWidth) * d), (int) (((double) this.m3mPrintAreaActualHeight) * d)};
    }

    public void calcSizeAndPositionOnScreen() {
        calcPreviewPaperSize();
        initPreviewPrtimageRect();
        setMovableArea();
    }

    private void setMovableArea() {
        this.mPreviewMovableArea[0] = this.mPreviewPaperRect.left + 10;
        this.mPreviewMovableArea[1] = this.mPreviewPaperRect.top + 10;
        this.mPreviewMovableArea[2] = this.mPreviewPaperRect.right - 10;
        this.mPreviewMovableArea[3] = this.mPreviewPaperRect.bottom - 10;
        if (!this.mBorderlessMode) {
            int[] paperMaginAnd3mPrintableArea = getPaperMaginAnd3mPrintableArea();
            int[] iArr = this.mPreviewMovableArea;
            iArr[0] = iArr[0] + paperMaginAnd3mPrintableArea[0];
            iArr[1] = iArr[1] + paperMaginAnd3mPrintableArea[1];
            iArr[2] = ((this.mPreviewPaperRect.left + paperMaginAnd3mPrintableArea[0]) + paperMaginAnd3mPrintableArea[2]) - 10;
            this.mPreviewMovableArea[3] = ((this.mPreviewPaperRect.top + paperMaginAnd3mPrintableArea[1]) + paperMaginAnd3mPrintableArea[3]) - 10;
        }
    }

    private void initPreviewPrtimageRect() {
        if (this.mPreviewPaperRect.right > 0 && this.mPreviewPaperRect.width() > 0 && this.mPrtimageActualWidth > 0 && this.mPrtimageActualHeight > 0) {
            this.mFitSizeMode = false;
            this.mPrtimagePreviewPosition.setScale(1.0d);
            this.mPrtimagePreviewPosition.resetRotate();
            double d = this.mActualPreviewRate;
            this.mPrtimagePreviewPosition.setX1Size((int) ((((double) this.mPrtimageActualWidth) * d) + 0.5d), (int) ((((double) this.mPrtimageActualHeight) * d) + 0.5d));
            this.mPrtimagePreviewPosition.setLeft(this.mPreviewPaperRect.left);
            this.mPrtimagePreviewPosition.setTop(this.mPreviewPaperRect.top);
        }
    }

    private void calcPreviewPaperSize() {
        int i;
        int i2;
        int i3;
        int i4 = this.mScreenWidth;
        if (i4 > 0 && (i = this.mScreenHeight) > 0 && (i2 = this.mPaperActualWidth) > 0 && (i3 = this.mPaperActualHeight) > 0) {
            this.mFitSizeMode = false;
            double d = this.mPreviewPaperRate;
            this.mActualPreviewRate = Math.min((((double) i4) * d) / ((double) i2), (((double) i) * d) / ((double) i3));
            this.mPreviewPaperRect.setEmpty();
            Rect rect = this.mPreviewPaperRect;
            double d2 = this.mActualPreviewRate;
            rect.right = (int) (((double) this.mPaperActualWidth) * d2);
            rect.bottom = (int) (((double) this.mPaperActualHeight) * d2);
            this.mPreviewPaperRect.offsetTo((this.mScreenWidth - rect.right) / 2, (this.mScreenHeight - this.mPreviewPaperRect.bottom) / 2);
        }
    }

    public void move(int i, int i2) {
        this.mPrtimagePreviewPosition.move(i, i2);
        adjustPrtimagePosition();
    }

    public Rect getPreviewPaperRect() {
        return this.mPreviewPaperRect;
    }

    public Rect getPreviewPrtimageRect() {
        this.mScaledPrtimagePreviewRect.left = this.mPrtimagePreviewPosition.getLeft();
        this.mScaledPrtimagePreviewRect.right = this.mPrtimagePreviewPosition.getRight();
        this.mScaledPrtimagePreviewRect.top = this.mPrtimagePreviewPosition.getTop();
        this.mScaledPrtimagePreviewRect.bottom = this.mPrtimagePreviewPosition.getBottom();
        return this.mScaledPrtimagePreviewRect;
    }

    public int getPrtimageRotation() {
        return (this.mPrtimagePreviewPosition.getRotation() + this.mPrintTargetAddRotation) & 3;
    }

    public void rotateRight90() {
        this.mPrtimagePreviewPosition.rotateRight90();
        adjustPrtimagePosition();
    }

    public void resetRotation() {
        this.mPrtimagePreviewPosition.resetRotate();
        adjustPrtimagePosition();
    }

    public void setPrintTargetAddRotation(int i) {
        this.mPrintTargetAddRotation = i;
    }

    public double getCurrentPrttargetScale() {
        return this.mPrtimagePreviewPosition.getScale();
    }

    public void setScaleFactor(double d) {
        if (d < 0.25d) {
            d = 0.25d;
        } else if (d > 4.0d) {
            d = 4.0d;
        }
        this.mPrtimagePreviewPosition.setScale(d);
        adjustPrtimagePosition();
    }

    private void adjustPrtimagePosition() {
        int right = this.mPrtimagePreviewPosition.getRight();
        int[] iArr = this.mPreviewMovableArea;
        if (right < iArr[0]) {
            this.mPrtimagePreviewPosition.setRight(iArr[0]);
        }
        int bottom = this.mPrtimagePreviewPosition.getBottom();
        int[] iArr2 = this.mPreviewMovableArea;
        if (bottom < iArr2[1]) {
            this.mPrtimagePreviewPosition.setBottom(iArr2[1]);
        }
        int left = this.mPrtimagePreviewPosition.getLeft();
        int[] iArr3 = this.mPreviewMovableArea;
        if (left > iArr3[2]) {
            this.mPrtimagePreviewPosition.setLeft(iArr3[2]);
        }
        int top = this.mPrtimagePreviewPosition.getTop();
        int[] iArr4 = this.mPreviewMovableArea;
        if (top > iArr4[3]) {
            this.mPrtimagePreviewPosition.setTop(iArr4[3]);
        }
        this.mFitSizeMode = false;
    }

    public void resetPrtimagePosition() {
        initPreviewPrtimageRect();
    }

    public PreviewView.SizeAndPosition getPrintSizeAndPosition() {
        if (this.mActualPreviewRate <= 0.0d) {
            return null;
        }
        return new PreviewView.SizeAndPosition((int) (((double) this.mPrtimagePreviewPosition.getWidth()) / this.mActualPreviewRate), (int) (((double) this.mPrtimagePreviewPosition.getHeight()) / this.mActualPreviewRate), (int) (((double) (this.mPrtimagePreviewPosition.getLeft() - this.mPreviewPaperRect.left)) / this.mActualPreviewRate), (int) (((double) (this.mPrtimagePreviewPosition.getTop() - this.mPreviewPaperRect.top)) / this.mActualPreviewRate), getPrtimageRotation());
    }

    public ImageAndLayout getImageAndLayout(boolean z) {
        double d;
        double d2;
        if (this.mActualPreviewRate <= 0.0d) {
            return null;
        }
        ImageAndLayout imageAndLayout = new ImageAndLayout();
        if (z) {
            imageAndLayout.setLayoutAreaSize(this.mBorderlessDataWidth, this.mBorderlessDataHeight);
            d2 = (((double) (this.mPrtimagePreviewPosition.getLeft() - this.mPreviewPaperRect.left)) / this.mActualPreviewRate) + ((double) this.mBorderlessLeftMargin);
            d = (((double) (this.mPrtimagePreviewPosition.getTop() - this.mPreviewPaperRect.top)) / this.mActualPreviewRate) + ((double) this.mBorderlessTopMargin);
        } else {
            imageAndLayout.setLayoutAreaSize(this.m3mPrintAreaActualWidth, this.m3mPrintAreaActualHeight);
            d2 = (((double) (this.mPrtimagePreviewPosition.getLeft() - this.mPreviewPaperRect.left)) / this.mActualPreviewRate) - ((double) this.mPaperTo3mActualLeftMargin);
            d = (((double) (this.mPrtimagePreviewPosition.getTop() - this.mPreviewPaperRect.top)) / this.mActualPreviewRate) - ((double) this.mPaperTo3mActualTopMargin);
        }
        imageAndLayout.setLayout(((double) this.mPrtimagePreviewPosition.getWidth()) / this.mActualPreviewRate, ((double) this.mPrtimagePreviewPosition.getHeight()) / this.mActualPreviewRate, d2, d, getPrtimageRotation());
        return imageAndLayout;
    }

    public void fitSize() {
        int i;
        int i2 = this.mPrtimageActualWidth;
        if (i2 > 0 && (i = this.mPrtimageActualHeight) > 0) {
            if ((this.mPrtimagePreviewPosition.getRotation() & 1) != 0) {
                int i3 = i;
                i = i2;
                i2 = i3;
            }
            double d = ((double) this.mPaperActualWidth) / ((double) i2);
            double d2 = ((double) this.mPaperActualHeight) / ((double) i);
            if (d <= d2) {
                d2 = d;
            }
            if (d2 > 4.0d) {
                d2 = 4.0d;
            }
            if (d2 < 0.25d) {
                d2 = 0.25d;
            }
            this.mPrtimagePreviewPosition.setScale(d2);
            this.mPrtimagePreviewPosition.setLeft(this.mPreviewPaperRect.left);
            this.mPrtimagePreviewPosition.setTop(this.mPreviewPaperRect.top);
            this.mFitSizeMode = true;
        }
    }

    public boolean isFitSize() {
        return this.mFitSizeMode;
    }

    public void setBorderless(boolean z) {
        this.mBorderlessMode = z;
    }

    private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
        objectOutputStream.defaultWriteObject();
        objectOutputStream.writeInt(this.mPreviewPaperRect.left);
        objectOutputStream.writeInt(this.mPreviewPaperRect.right);
        objectOutputStream.writeInt(this.mPreviewPaperRect.top);
        objectOutputStream.writeInt(this.mPreviewPaperRect.bottom);
        objectOutputStream.writeInt(this.mScaledPrtimagePreviewRect.left);
        objectOutputStream.writeInt(this.mScaledPrtimagePreviewRect.right);
        objectOutputStream.writeInt(this.mScaledPrtimagePreviewRect.top);
        objectOutputStream.writeInt(this.mScaledPrtimagePreviewRect.bottom);
    }

    private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        objectInputStream.defaultReadObject();
        this.mPreviewPaperRect = new Rect();
        this.mPreviewPaperRect.left = objectInputStream.readInt();
        this.mPreviewPaperRect.right = objectInputStream.readInt();
        this.mPreviewPaperRect.top = objectInputStream.readInt();
        this.mPreviewPaperRect.bottom = objectInputStream.readInt();
        this.mScaledPrtimagePreviewRect = new Rect();
        this.mScaledPrtimagePreviewRect.left = objectInputStream.readInt();
        this.mScaledPrtimagePreviewRect.right = objectInputStream.readInt();
        this.mScaledPrtimagePreviewRect.top = objectInputStream.readInt();
        this.mScaledPrtimagePreviewRect.bottom = objectInputStream.readInt();
    }

    static class PreviewRect implements Serializable {
        private static final long serialVersionUID = 1;
        private int mCx;
        private int mCy;
        private int mRotation;
        private double mScale = 1.0d;
        private int mScaledHeight;
        private int mScaledWidth;
        private int mX1Height;
        private int mX1Width;

        PreviewRect() {
        }

        public final void setX1Size(int i, int i2) {
            this.mX1Width = i;
            this.mX1Height = i2;
            setScale(this.mScale);
        }

        public final void setLeft(int i) {
            this.mCx = (this.mScaledWidth / 2) + i;
        }

        public final void setRight(int i) {
            setLeft(i - this.mScaledWidth);
        }

        public final void setTop(int i) {
            this.mCy = (this.mScaledHeight / 2) + i;
        }

        public final void setBottom(int i) {
            setTop(i - this.mScaledHeight);
        }

        public int getLeft() {
            return this.mCx - (this.mScaledWidth / 2);
        }

        public int getRight() {
            return getLeft() + this.mScaledWidth;
        }

        public int getTop() {
            return this.mCy - (this.mScaledHeight / 2);
        }

        public int getBottom() {
            return getTop() + this.mScaledHeight;
        }

        public int getWidth() {
            return this.mScaledWidth;
        }

        public int getHeight() {
            return this.mScaledHeight;
        }

        public void setScale(double d) {
            this.mScale = d;
            this.mScaledWidth = (int) (((double) this.mX1Width) * d);
            this.mScaledHeight = (int) (((double) this.mX1Height) * d);
            if ((this.mRotation & 1) != 0) {
                int i = this.mScaledWidth;
                this.mScaledWidth = this.mScaledHeight;
                this.mScaledHeight = i;
            }
        }

        public double getScale() {
            return this.mScale;
        }

        public void move(int i, int i2) {
            this.mCx += i;
            this.mCy += i2;
        }

        public int getRotation() {
            return this.mRotation;
        }

        public void rotateRight90() {
            this.mRotation++;
            if (this.mRotation >= 4) {
                this.mRotation = 0;
            }
            setScale(this.mScale);
        }

        public void resetRotate() {
            this.mRotation = 0;
            setScale(this.mScale);
        }
    }
}
