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;
    /* access modifiers changed from: private */
    public 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) {
            this.width = i;
            this.height = i2;
            this.offsetX = i3;
            this.offsetY = i4;
            this.rotation = i5;
        }

        public SizeAndPosition() {
        }
    }

    public PreviewView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.mBitmapPaint = new Paint();
        this.mMatrix = new Matrix();
        this.mPapaerSizeId = -1;
        this.mGestureDetector = new GestureDetectorCompat(context, new MyGestureListener(this));
        this.mGestureDetector.setIsLongpressEnabled(false);
        this.mScaleGestureDetector = new ScaleGestureDetector(context, new MyScaledGestureListener(this));
        this.mPaint = new Paint();
        this.mLinePoints = new float[16];
        this.mPreviewPosition = new PreviewPosition();
        this.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 = this.mGestureDetector.onTouchEvent(motionEvent) || this.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 = this.mPreviewPosition.getPreviewPaperRect();
        if (previewPaperRect != null) {
            this.mPaint.setColor(-1);
            canvas.drawRect(previewPaperRect, this.mPaint);
        }
        drawPrintTargetPreview(canvas);
        drawOutsidePaperArea(canvas, previewPaperRect);
        if (!this.mBorderlessMode && previewPaperRect != null) {
            drawNonPrintableArea(canvas, previewPaperRect, this.mPreviewPosition.getPaperMaginAnd3mPrintableArea());
        }
    }

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

    private void drawNonPrintableArea(Canvas canvas, Rect rect, int[] iArr) {
        this.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, this.mPaint);
        canvas2.drawRect((float) (rect.left + i), (float) rect.top, (float) (rect.left + i + i3), (float) (rect.top + i2), this.mPaint);
        canvas2.drawRect((float) (rect.left + i), (float) (rect.top + i2 + i4), (float) (rect.left + i + i3), (float) rect.bottom, this.mPaint);
        canvas.drawRect((float) (rect.left + i + i3), (float) rect.top, (float) rect.right, (float) rect.bottom, this.mPaint);
    }

    private void drawOutsidePaperArea(Canvas canvas, Rect rect) {
        this.mPaint.setColor(getResources().getColor(R.color.background_preview));
        if (this.mIsMoving) {
            this.mPaint.setAlpha(221);
        }
        float width = (float) getWidth();
        float height = (float) getHeight();
        Canvas canvas2 = canvas;
        canvas2.drawRect(0.0f, 0.0f, (float) rect.left, height, this.mPaint);
        canvas2.drawRect((float) rect.left, 0.0f, (float) rect.right, (float) rect.top, this.mPaint);
        canvas2.drawRect((float) rect.left, (float) rect.bottom, (float) rect.right, height, this.mPaint);
        canvas.drawRect((float) rect.right, 0.0f, width, height, this.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) {
                this.mPreviewPosition.setScreenSize(i, i2);
                update();
            } else if (i != i3 || i2 != i4) {
                this.mPreviewPosition.changeScreenSize(i, i2);
            }
        }
    }

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

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

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

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

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

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

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

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

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

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

    /* access modifiers changed from: private */
    public void updateScale() {
        PrttargetScaleChangeListener prttargetScaleChangeListener = this.mPrttargetScaleChangeListener;
        if (prttargetScaleChangeListener != null) {
            prttargetScaleChangeListener.onPrttargetScaleChange(this.mPreviewPosition.getCurrentPrttargetScale());
        }
    }

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

    private void setPaperSizeInternal(int i) {
        this.mPapaerSizeId = i;
        Info_paper infoPaper = Info_paper.getInfoPaper(getContext(), i);
        if (infoPaper != null) {
            this.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());
            this.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) {
            this.mBitmapPaint.setColorFilter(colorMatrixColorFilter);
        }
    }

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

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

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

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

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

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

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

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

    public void printPostionResetRequest() {
        this.mIsPrintPositionResetRequired = true;
    }

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

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

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

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