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) {
        mPreviewPaperRate = d;
    }

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

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

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

    public void changeScreenSize(int i, int i2) {
        if (mActualPreviewRate <= 0.0d || i <= 0 || i2 <= 0) {
            return;
        }
        if (mScreenWidth != i || mScreenHeight != i2) {
            mScreenWidth = i;
            mScreenHeight = i2;
            double left = ((mPrtimagePreviewPosition.getLeft() - mPreviewPaperRect.left)) / mActualPreviewRate;
            double top = ((mPrtimagePreviewPosition.getTop() - mPreviewPaperRect.top)) / mActualPreviewRate;
            calcPreviewPaperSize();
            double d = mActualPreviewRate;
            mPrtimagePreviewPosition.setX1Size(((mPrtimageActualWidth) * d), ((mPrtimageActualHeight) * d));
            mPrtimagePreviewPosition.setLeft(mPreviewPaperRect.left + ((left * mActualPreviewRate)));
            mPrtimagePreviewPosition.setTop(mPreviewPaperRect.top + ((top * 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) {
        mPaperActualWidth = i;
        mPaperActualHeight = i2;
        mPaperTo3mActualLeftMargin = i3;
        mPaperTo3mActualTopMargin = i4;
        m3mPrintAreaActualWidth = i5;
        m3mPrintAreaActualHeight = i6;
        mBorderlessLeftMargin = i7;
        mBorderlessTopMargin = i8;
        mBorderlessDataWidth = i9;
        mBorderlessDataHeight = i10;
    }

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

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

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

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

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

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

    public Rect getPreviewPaperRect() {
        return mPreviewPaperRect;
    }

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

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

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

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

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

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

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

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

    public void resetPrtimagePosition() {
        initPreviewPrtimageRect();
    }

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

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

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

    public boolean isFitSize() {
        return mFitSizeMode;
    }

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

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

    private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        objectInputStream.defaultReadObject();
        mPreviewPaperRect = new Rect();
        mPreviewPaperRect.left = objectInputStream.readInt();
        mPreviewPaperRect.right = objectInputStream.readInt();
        mPreviewPaperRect.top = objectInputStream.readInt();
        mPreviewPaperRect.bottom = objectInputStream.readInt();
        mScaledPrtimagePreviewRect = new Rect();
        mScaledPrtimagePreviewRect.left = objectInputStream.readInt();
        mScaledPrtimagePreviewRect.right = objectInputStream.readInt();
        mScaledPrtimagePreviewRect.top = objectInputStream.readInt();
        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) {
            mX1Width = i;
            mX1Height = i2;
            setScale(mScale);
        }

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

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

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

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

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

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

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

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

        public int getWidth() {
            return mScaledWidth;
        }

        public int getHeight() {
            return mScaledHeight;
        }

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

        public double getScale() {
            return mScale;
        }

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

        public int getRotation() {
            return mRotation;
        }

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

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