package com.epson.cameracopy.printlayout;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.p000v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

import epson.common.ImageUtil;
import epson.common.Info_paper;

import org.opencv.videoio.Videoio;

public class PreviewView extends View {
    private static final int BORDER_AREA_COLOR = -1;
    private static final ColorMatrixColorFilter[] COLOR_FILTER = {new ColorMatrixColorFilter(new ColorMatrix(colorMatrix)), new ColorMatrixColorFilter(new ColorMatrix(monoMatrix)), new ColorMatrixColorFilter(new ColorMatrix(sepiaMatrix))};
    private static final float MANUSCRIPT_FRAME_BORDER_SIZE_DP = 1.5f;
    private static final int MANUSCRIPT_FRAME_COLOR = -1057030144;
    private static final int PAPER_AREA_COLOR = -1;
    private static final float[] colorMatrix = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
    private static final float[] monoMatrix = {0.298912f, 0.586611f, 0.114478f, 0.0f, 0.0f, 0.298912f, 0.586611f, 0.114478f, 0.0f, 0.0f, 0.298912f, 0.586611f, 0.114478f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
    private static final float[] sepiaMatrix = {0.11747242f, 0.45110387f, 0.021636343f, 0.0f, 0.0f, 0.10432029f, 0.40241516f, 0.019232305f, 0.0f, 0.0f, 0.050217215f, 0.31325027f, 0.014996618f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
    private Bitmap mBitmap;
    private final Paint mBitmapPaint;
    private boolean mBorderlessMode;
    private float mDpToPixelScale;
    private final GestureDetectorCompat mGestureDetector;
    private String mImageFileName;
    private boolean mIsMoving;
    private boolean mIsPrintPositionResetRequired;
    private float[] mLinePoints;
    private final Matrix mMatrix;
    private final Paint mPaint;
    private int mPapaerSizeId;
    private PreviewPosition mPreviewPosition;
    private PrttargetScaleChangeListener mPrttargetScaleChangeListener;
    private final ScaleGestureDetector mScaleGestureDetector;

    public interface PrttargetScaleChangeListener {
        void onPrttargetScaleChange(double d);
    }

    public static class SizeAndPosition {
        public int height;
        public int offsetX;
        public int offsetY;
        public int rotation;
        public int width;

        public SizeAndPosition(int i, int i2, int i3, int i4, int i5) {
            width = i;
            height = i2;
            offsetX = i3;
            offsetY = i4;
            rotation = i5;
        }

        public SizeAndPosition() {
        }
    }

    public PreviewView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        mBitmapPaint = new Paint();
        mMatrix = new Matrix();
        mPapaerSizeId = -1;
        mGestureDetector = new GestureDetectorCompat(context, new MyGestureListener(this));
        mGestureDetector.setIsLongpressEnabled(false);
        mScaleGestureDetector = new ScaleGestureDetector(context, new MyScaledGestureListener(this));
        mPaint = new Paint();
        mLinePoints = new float[16];
        mPreviewPosition = new PreviewPosition();
        mDpToPixelScale = context.getResources().getDisplayMetrics().density;
    }

    public PreviewView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public PreviewView(Context context) {
        this(context, (AttributeSet) null, 0);
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        boolean z = mGestureDetector.onTouchEvent(motionEvent) || mScaleGestureDetector.onTouchEvent(motionEvent);
        if (motionEvent.getActionMasked() == 1) {
            setMoving(false);
            invalidate();
        }
        if (z || super.onTouchEvent(motionEvent)) {
            return true;
        }
        return false;
    }

    protected void onDraw(Canvas canvas) {
        Rect previewPaperRect = mPreviewPosition.getPreviewPaperRect();
        if (previewPaperRect != null) {
            mPaint.setColor(-1);
            canvas.drawRect(previewPaperRect, mPaint);
        }
        drawPrintTargetPreview(canvas);
        drawOutsidePaperArea(canvas, previewPaperRect);
        if (!mBorderlessMode && previewPaperRect != null) {
            drawNonPrintableArea(canvas, previewPaperRect, mPreviewPosition.getPaperMaginAnd3mPrintableArea());
        }
    }

    private void drawPrintTargetPreview(Canvas canvas) {
        double d;
        double d2;
        int i;
        int i2;
        Rect previewPrtimageRect = mPreviewPosition.getPreviewPrtimageRect();
        if (previewPrtimageRect != null && mBitmap != null) {
            int prtimageRotation = mPreviewPosition.getPrtimageRotation();
            mMatrix.reset();
            mMatrix.setRotate(((float) prtimageRotation) * 90.0f);
            if ((prtimageRotation & 1) != 0) {
                d2 = (previewPrtimageRect.width()) / (mBitmap.getHeight());
                d = (previewPrtimageRect.height()) / (mBitmap.getWidth());
            } else {
                d2 = (previewPrtimageRect.width()) / (mBitmap.getWidth());
                d = (previewPrtimageRect.height()) / (mBitmap.getHeight());
            }
            mMatrix.postScale((float) d2, (float) d);
            switch (prtimageRotation) {
                case 1:
                    i2 = previewPrtimageRect.width();
                    i = 0;
                    break;
                case 2:
                    i2 = previewPrtimageRect.width();
                    i = previewPrtimageRect.height();
                    break;
                case 3:
                    i = previewPrtimageRect.height();
                    i2 = 0;
                    break;
                default:
                    i2 = 0;
                    i = 0;
                    break;
            }
            mMatrix.postTranslate((float) (previewPrtimageRect.left + i2), (float) (previewPrtimageRect.top + i));
            canvas.drawBitmap(mBitmap, mMatrix, mBitmapPaint);
            mPaint.setColor(MANUSCRIPT_FRAME_COLOR);
            float f = mDpToPixelScale * MANUSCRIPT_FRAME_BORDER_SIZE_DP;
            mPaint.setStrokeWidth(f);
            float f2 = f / 2.0f;
            float f3 = ((float) previewPrtimageRect.bottom) - f2;
            float f4 = ((float) previewPrtimageRect.right) - f2;
            mLinePoints[0] = (float) previewPrtimageRect.left;
            mLinePoints[1] = (float) previewPrtimageRect.top;
            mLinePoints[2] = (float) previewPrtimageRect.left;
            float[] fArr = mLinePoints;
            fArr[3] = f3;
            fArr[4] = (float) previewPrtimageRect.left;
            float[] fArr2 = mLinePoints;
            fArr2[5] = f3;
            fArr2[6] = f4;
            fArr2[7] = f3;
            fArr2[8] = f4;
            fArr2[9] = f3;
            fArr2[10] = f4;
            fArr2[11] = (float) previewPrtimageRect.top;
            float[] fArr3 = mLinePoints;
            fArr3[12] = f4;
            fArr3[13] = (float) previewPrtimageRect.top;
            mLinePoints[14] = (float) previewPrtimageRect.left;
            mLinePoints[15] = (float) previewPrtimageRect.top;
            canvas.drawLines(mLinePoints, mPaint);
        }
    }

    private void drawNonPrintableArea(Canvas canvas, Rect rect, int[] iArr) {
        mPaint.setColor(-1);
        int i = iArr[0];
        int i2 = iArr[1];
        int i3 = iArr[2];
        int i4 = iArr[3];
        Canvas canvas2 = canvas;
        canvas2.drawRect((float) rect.left, (float) rect.top, (float) (rect.left + i), (float) rect.bottom, mPaint);
        canvas2.drawRect((float) (rect.left + i), (float) rect.top, (float) (rect.left + i + i3), (float) (rect.top + i2), mPaint);
        canvas2.drawRect((float) (rect.left + i), (float) (rect.top + i2 + i4), (float) (rect.left + i + i3), (float) rect.bottom, mPaint);
        canvas.drawRect((float) (rect.left + i + i3), (float) rect.top, (float) rect.right, (float) rect.bottom, mPaint);
    }

    private void drawOutsidePaperArea(Canvas canvas, Rect rect) {
        mPaint.setColor(getResources().getColor(R.color.background_preview));
        if (mIsMoving) {
            mPaint.setAlpha(221);
        }
        float width = (float) getWidth();
        float height = (float) getHeight();
        Canvas canvas2 = canvas;
        canvas2.drawRect(0.0f, 0.0f, (float) rect.left, height, mPaint);
        canvas2.drawRect((float) rect.left, 0.0f, (float) rect.right, (float) rect.top, mPaint);
        canvas2.drawRect((float) rect.left, (float) rect.bottom, (float) rect.right, height, mPaint);
        canvas.drawRect((float) rect.right, 0.0f, width, height, mPaint);
    }

    protected void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
        if (i > 0 && i2 > 0) {
            if (i3 <= 0 || i4 <= 0) {
                mPreviewPosition.setScreenSize(i, i2);
                update();
            } else if (i != i3 || i2 != i4) {
                mPreviewPosition.changeScreenSize(i, i2);
            }
        }
    }

    public void setMoving(boolean z) {
        mIsMoving = z;
    }

    static class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        private final PreviewView mPreviewView;

        public MyGestureListener(PreviewView previewView) {
            mPreviewView = previewView;
        }

        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
            mPreviewView.setMoving(true);
            mPreviewView.mPreviewPosition.move(-(f), -(f2));
            mPreviewView.invalidate();
            return true;
        }

        public boolean onDown(MotionEvent motionEvent) {
            mPreviewView.setMoving(true);
            mPreviewView.invalidate();
            return true;
        }

        public boolean onDoubleTap(MotionEvent motionEvent) {
            if (mPreviewView.mPreviewPosition.isFitSize()) {
                mPreviewView.mPreviewPosition.resetPrtimagePosition();
                mPreviewView.updateScale();
                mPreviewView.invalidate();
                return true;
            }
            mPreviewView.mPreviewPosition.fitSize();
            mPreviewView.updateScale();
            mPreviewView.invalidate();
            return true;
        }
    }

    static class MyScaledGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        private double mBaseFactor;
        private final PreviewView mPreviewView;

        public MyScaledGestureListener(PreviewView previewView) {
            mPreviewView = previewView;
        }

        public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
            mBaseFactor = mPreviewView.mPreviewPosition.getCurrentPrttargetScale();
            return super.onScaleBegin(scaleGestureDetector);
        }

        public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
            mPreviewView.mPreviewPosition.setScaleFactor((scaleGestureDetector.getScaleFactor()) * mBaseFactor);
            mPreviewView.updateScale();
            mPreviewView.invalidate();
            return super.onScale(scaleGestureDetector);
        }
    }

    private void updateScale() {
        PrttargetScaleChangeListener prttargetScaleChangeListener = mPrttargetScaleChangeListener;
        if (prttargetScaleChangeListener != null) {
            prttargetScaleChangeListener.onPrttargetScaleChange(mPreviewPosition.getCurrentPrttargetScale());
        }
    }

    public void setPaperSize(int i) {
        if (i != mPapaerSizeId) {
            setPaperSizeInternal(i);
        }
    }

    private void setPaperSizeInternal(int i) {
        mPapaerSizeId = i;
        Info_paper infoPaper = Info_paper.getInfoPaper(getContext(), i);
        if (infoPaper != null) {
            mPreviewPosition.setPaperSize(infoPaper.getPaper_width(), infoPaper.getPaper_height(), infoPaper.getLeftMargin_border(), infoPaper.getTopMargin_border(), infoPaper.getPaper_width_boder(), infoPaper.getPaper_height_boder(), -infoPaper.getLeftMargin(), -infoPaper.getTopMargin(), infoPaper.getPaper_width_boderless(), infoPaper.getPaper_height_boderless());
            mIsPrintPositionResetRequired = true;
        }
    }

    public void setColor(int i) {
        ColorMatrixColorFilter colorMatrixColorFilter;
        switch (i) {
            case 0:
                colorMatrixColorFilter = COLOR_FILTER[0];
                break;
            case 1:
                colorMatrixColorFilter = COLOR_FILTER[1];
                break;
            default:
                colorMatrixColorFilter = null;
                break;
        }
        if (colorMatrixColorFilter != null) {
            mBitmapPaint.setColorFilter(colorMatrixColorFilter);
        }
    }

    public void rotateImageR90() {
        mPreviewPosition.rotateRight90();
        invalidate();
    }

    public void setImage(String str) {
        mImageFileName = str;
        mIsPrintPositionResetRequired = true;
    }

    public void setPrintTargetSize(int i, int i2) {
        mPreviewPosition.setPrtimageActualSize(i, i2);
        mIsPrintPositionResetRequired = true;
    }

    public void update() {
        if (mImageFileName != null && getHeight() > 0 && getWidth() > 0) {
            mBitmap = createPreviewBitmap(mImageFileName);
            if (mBitmap != null) {
                int[] prtImageActualSIze = mPreviewPosition.getPrtImageActualSIze();
                int i = 1;
                if ((prtImageActualSIze[0] - prtImageActualSIze[1]) * (mBitmap.getWidth() - mBitmap.getHeight()) >= 0) {
                    i = 0;
                }
                mPreviewPosition.setPrintTargetAddRotation(i);
                if (mIsPrintPositionResetRequired) {
                    setPaperSizeInternal(mPapaerSizeId);
                    mPreviewPosition.setBorderless(mBorderlessMode);
                    mPreviewPosition.resetRotation();
                    mPreviewPosition.calcSizeAndPositionOnScreen();
                    mIsPrintPositionResetRequired = false;
                    updateScale();
                }
                invalidate();
            }
        }
    }

    private Bitmap createPreviewBitmap(String str) {
        return ImageUtil.loadBitmap(str, Videoio.CAP_UNICAP, Videoio.CAP_PVAPI, true, true);
    }

    public void releaseBitmap() {
        mBitmap.recycle();
        mBitmap = null;
    }

    public void restoreBitmap() {
        String str;
        if (mBitmap == null && (str = mImageFileName) != null) {
            mBitmap = createPreviewBitmap(str);
        }
    }

    public void setBorderless(boolean z) {
        if (mBorderlessMode != z) {
            mBorderlessMode = z;
            mIsPrintPositionResetRequired = true;
        }
    }

    public void printPostionResetRequest() {
        mIsPrintPositionResetRequired = true;
    }

    public void setPrttargetScaleChangeListener(PrttargetScaleChangeListener prttargetScaleChangeListener) {
        mPrttargetScaleChangeListener = prttargetScaleChangeListener;
    }

    public ImageAndLayout getImageAndLayout() {
        return mPreviewPosition.getImageAndLayout(mBorderlessMode);
    }

    public void saveInstanceState(Bundle bundle, String str) {
        bundle.putSerializable(str, mPreviewPosition);
    }

    public void restoreInstanceState(Bundle bundle, String str) {
        mPreviewPosition = (PreviewPosition) bundle.getSerializable(str);
        mIsPrintPositionResetRequired = false;
        updateScale();
    }
}
