package epson.scan.activity;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.support.p000v4.view.GestureDetectorCompat;
import android.support.p000v4.view.ViewCompat;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import epson.common.ImageUtil;
import epson.print.C2135R;
import java.util.ArrayList;
import java.util.Iterator;

public class I2ScanAreaView extends View {
    private static final int CONTOUR_AREA_BACK_COLOR = -1;
    private final int SCAN_AREA_CORNER_LENGTH_DP = 30;
    private final int SCAN_AREA_CORNER_LONG_TAP_AREA_WIDTH_DP = 60;
    private final int SCAN_AREA_CORNER_WIDTH_DP = 4;
    private final int SCAN_AREA_MOVE_MODE_AREA_MOVE = 3;
    private final int SCAN_AREA_MOVE_MODE_LEFT_UPPER = 1;
    private final int SCAN_AREA_MOVE_MODE_NONE = 0;
    private final int SCAN_AREA_MOVE_MODE_RIGHT_LOWER = 2;
    private ArrayList<String> mBreakStrings;
    private float[] mDashInterval;
    private Rect mDisplayContourArea;
    private float mDisplayDensity;
    private Rect mDisplayMovableArea;
    private Rect mDisplayPreviewImageRect;
    private double mDisplayRate;
    private float mDisplayTextLeft;
    private ArrayList<Float> mDisplayTextYPosition;
    private FlickCallback mFlickCallback;
    private GestureDetectorCompat mGestureDetectorCompat;
    private final GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {
        public boolean onDown(MotionEvent motionEvent) {
            return true;
        }

        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
            return true;
        }

        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
            I2ScanAreaView.this.checkFlick(f);
            return true;
        }

        public void onLongPress(MotionEvent motionEvent) {
            boolean unused = I2ScanAreaView.this.checkMoveMode(motionEvent.getX(), motionEvent.getY());
        }
    };
    private boolean mMovableAreaMode;
    private int mOrgContourHeight;
    private int mOrgContourWidth;
    private Rect mOrgMovableArea;
    private Rect mOrgPreviewImageRect;
    private Paint mPaint;
    private float mPhase;
    private float mPrevX;
    private float mPrevY;
    private Bitmap mPreviewImageBitmap;
    private String mPreviewImageFile;
    private int mPromptTextColor = -16776961;
    private boolean mPromptTextDisplay;
    private float mPromptTextSize = 10.0f;
    private Bitmap mScanAreaCenterBitmap;
    private int mScanAreaCornerLengthPixel;
    private int mScanAreaCornerLogTapAreaWidthPixel;
    private int mScanAreaCornerWidthPixel;
    private boolean mScanAreaModified;
    private int mScanAreaMoveMode;
    private final Path mScanAreaPath = new Path();
    private String mScanPromptString;
    private TextPaint mTextPaint;

    interface FlickCallback {
        void flick(int i);
    }

    public boolean isScanAreaModified() {
        return this.mMovableAreaMode && this.mScanAreaModified;
    }

    public Rect getMovableScanArea() {
        return this.mOrgMovableArea;
    }

    public int[] getOrgContour() {
        return new int[]{this.mOrgContourWidth, this.mOrgContourHeight};
    }

    public I2ScanAreaView(Context context) {
        super(context);
        init((AttributeSet) null, 0);
    }

    public I2ScanAreaView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init(attributeSet, 0);
    }

    public I2ScanAreaView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        init(attributeSet, i);
    }

    private void init(AttributeSet attributeSet, int i) {
        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attributeSet, C2135R.styleable.I2ScanAreaView, i, 0);
        this.mScanPromptString = obtainStyledAttributes.getString(0);
        this.mPromptTextColor = obtainStyledAttributes.getColor(1, this.mPromptTextColor);
        this.mPromptTextSize = obtainStyledAttributes.getDimension(2, this.mPromptTextSize);
        obtainStyledAttributes.recycle();
        this.mTextPaint = new TextPaint();
        this.mTextPaint.setFlags(1);
        this.mTextPaint.setTextAlign(Paint.Align.LEFT);
        this.mPaint = new Paint();
        this.mPromptTextDisplay = true;
        this.mScanAreaMoveMode = 0;
        float f = getResources().getDisplayMetrics().density;
        this.mScanAreaCornerLengthPixel = Math.round(30.0f * f);
        this.mScanAreaCornerWidthPixel = Math.round(4.0f * f);
        this.mScanAreaCornerLogTapAreaWidthPixel = Math.round(60.0f * f);
        this.mScanAreaCenterBitmap = BitmapFactory.decodeResource(getResources(), C2135R.C2136drawable.area_center);
        this.mDashInterval = new float[]{10.0f * f, 2.0f * f};
        this.mDisplayDensity = f;
        this.mScanAreaModified = false;
        this.mGestureDetectorCompat = new GestureDetectorCompat(getContext(), this.mGestureListener);
        invalidateTextPaintAndMeasurements();
    }

    public void setFlickCallback(FlickCallback flickCallback) {
        this.mFlickCallback = flickCallback;
    }

    private void invalidateTextPaintAndMeasurements() {
        this.mTextPaint.setTextSize(this.mPromptTextSize);
        this.mTextPaint.setColor(this.mPromptTextColor);
    }

    /* access modifiers changed from: protected */
    public void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
        calcDrawSize(i, i2);
    }

    private void calcDrawSize(int i, int i2) {
        if (i != 0 && i2 != 0 && this.mOrgContourWidth != 0 && this.mOrgContourHeight != 0) {
            int paddingLeft = (i - getPaddingLeft()) - getPaddingRight();
            int paddingTop = (i2 - getPaddingTop()) - getPaddingBottom();
            if (paddingLeft <= 0 || paddingTop <= 0) {
                this.mDisplayContourArea = null;
                return;
            }
            double max = Math.max(((double) this.mOrgContourWidth) / ((double) paddingLeft), ((double) this.mOrgContourHeight) / ((double) paddingTop));
            this.mDisplayRate = max;
            int i3 = (int) (((double) this.mOrgContourWidth) / max);
            int i4 = (int) (((double) this.mOrgContourHeight) / max);
            this.mDisplayContourArea = new Rect(0, 0, i3, i4);
            this.mDisplayContourArea.offsetTo(getPaddingLeft() + ((paddingLeft - i3) / 2), getPaddingTop() + ((paddingTop - i4) / 2));
            if (this.mMovableAreaMode) {
                Rect rect = this.mOrgMovableArea;
                if (rect != null) {
                    this.mDisplayMovableArea = new Rect((int) Math.round(((double) rect.left) / max), (int) Math.round(((double) this.mOrgMovableArea.top) / max), (int) Math.round(((double) this.mOrgMovableArea.right) / max), (int) Math.round(((double) this.mOrgMovableArea.bottom) / max));
                    this.mDisplayMovableArea.offset(this.mDisplayContourArea.left, this.mDisplayContourArea.top);
                }
            } else {
                this.mDisplayMovableArea = null;
            }
            Rect rect2 = this.mOrgPreviewImageRect;
            if (rect2 != null) {
                this.mDisplayPreviewImageRect = new Rect((int) Math.round(((double) rect2.left) / max), (int) Math.round(((double) this.mOrgPreviewImageRect.top) / max), (int) Math.round(((double) this.mOrgPreviewImageRect.right) / max), (int) Math.round(((double) this.mOrgPreviewImageRect.bottom) / max));
                if (!this.mMovableAreaMode) {
                    this.mDisplayPreviewImageRect.offsetTo((this.mDisplayContourArea.width() - this.mDisplayPreviewImageRect.width()) / 2, 0);
                }
                this.mDisplayPreviewImageRect.offset(this.mDisplayContourArea.left, this.mDisplayContourArea.top);
            }
            readPreviewBitmap();
            String str = this.mScanPromptString;
            if (str != null && !str.isEmpty()) {
                calcText(this.mDisplayContourArea);
            }
        }
    }

    private void calcText(Rect rect) {
        float width = ((float) rect.width()) * 0.9f;
        int length = this.mScanPromptString.length();
        Rect rect2 = new Rect();
        this.mBreakStrings = new ArrayList<>();
        ArrayList arrayList = new ArrayList();
        int i = 0;
        float f = 0.0f;
        while (i < length) {
            int breakText = this.mTextPaint.breakText(this.mScanPromptString.substring(i, length), true, width, (float[]) null);
            int i2 = i + breakText;
            String substring = this.mScanPromptString.substring(i, i2);
            this.mTextPaint.getTextBounds(substring, 0, breakText, rect2);
            f += (float) rect2.height();
            arrayList.add(Float.valueOf((float) rect2.height()));
            this.mBreakStrings.add(substring);
            i = i2;
        }
        this.mDisplayTextLeft = ((float) rect.left) + (width * 0.050000012f);
        float height = ((float) rect.top) + ((((float) rect.height()) - f) / 2.0f);
        this.mDisplayTextYPosition = new ArrayList<>();
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            this.mDisplayTextYPosition.add(Float.valueOf(height));
            height += ((Float) it.next()).floatValue();
        }
    }

    /* access modifiers changed from: protected */
    public void onDraw(Canvas canvas) {
        ArrayList<Float> arrayList;
        super.onDraw(canvas);
        if (this.mDisplayContourArea != null) {
            this.mPaint.setColor(-1);
            this.mPaint.setStyle(Paint.Style.FILL);
            canvas.drawRect(this.mDisplayContourArea, this.mPaint);
            Bitmap bitmap = this.mPreviewImageBitmap;
            if (bitmap != null) {
                canvas.drawBitmap(bitmap, (Rect) null, this.mDisplayPreviewImageRect, this.mPaint);
            }
            if (this.mDisplayMovableArea != null) {
                drawScanArea(canvas);
            }
            if (this.mPromptTextDisplay && this.mScanPromptString != null && this.mPreviewImageBitmap == null && this.mBreakStrings != null && (arrayList = this.mDisplayTextYPosition) != null) {
                Iterator<Float> it = arrayList.iterator();
                Iterator<String> it2 = this.mBreakStrings.iterator();
                while (it2.hasNext()) {
                    String next = it2.next();
                    if (it.hasNext()) {
                        canvas.drawText(next, this.mDisplayTextLeft, it.next().floatValue(), this.mTextPaint);
                    }
                }
            }
        }
    }

    private void drawScanArea(Canvas canvas) {
        if (this.mDisplayMovableArea != null) {
            this.mPaint.setColor(ViewCompat.MEASURED_STATE_MASK);
            if (this.mScanAreaMoveMode == 3) {
                canvas.drawBitmap(this.mScanAreaCenterBitmap, (float) (this.mDisplayMovableArea.centerX() - (this.mScanAreaCenterBitmap.getWidth() / 2)), (float) (this.mDisplayMovableArea.centerY() - (this.mScanAreaCenterBitmap.getHeight() / 2)), this.mPaint);
            }
            this.mScanAreaPath.reset();
            this.mScanAreaPath.moveTo((float) this.mDisplayMovableArea.left, (float) this.mDisplayMovableArea.top);
            this.mScanAreaPath.lineTo((float) this.mDisplayMovableArea.left, (float) this.mDisplayMovableArea.bottom);
            this.mScanAreaPath.lineTo((float) this.mDisplayMovableArea.right, (float) this.mDisplayMovableArea.bottom);
            this.mScanAreaPath.lineTo((float) this.mDisplayMovableArea.right, (float) this.mDisplayMovableArea.top);
            this.mScanAreaPath.close();
            if (this.mScanAreaMoveMode > 0) {
                this.mPaint.setPathEffect(new DashPathEffect(this.mDashInterval, this.mPhase));
                this.mPhase += this.mDisplayDensity;
                if (this.mPhase > 10000.0f) {
                    this.mPhase = 0.0f;
                }
                postInvalidate();
            } else {
                this.mPaint.setPathEffect((PathEffect) null);
            }
            this.mPaint.setStyle(Paint.Style.STROKE);
            this.mPaint.setStrokeWidth(1.0f);
            canvas.drawPath(this.mScanAreaPath, this.mPaint);
            int i = this.mScanAreaCornerWidthPixel;
            int i2 = this.mScanAreaCornerLengthPixel;
            if (this.mScanAreaMoveMode == 1) {
                i *= 2;
                i2 *= 2;
            }
            this.mPaint.setStrokeWidth((float) i);
            this.mPaint.setPathEffect((PathEffect) null);
            this.mPaint.setStrokeCap(Paint.Cap.SQUARE);
            int i3 = i / 2;
            int i4 = this.mDisplayMovableArea.left - i3;
            int i5 = this.mDisplayMovableArea.top - i3;
            float f = (float) i4;
            float f2 = (float) i5;
            Canvas canvas2 = canvas;
            float f3 = f;
            float f4 = f2;
            canvas2.drawLine(f3, f4, (float) (i4 + i2), f2, this.mPaint);
            canvas2.drawLine(f3, f4, f, (float) (i5 + i2), this.mPaint);
            int i6 = this.mScanAreaCornerWidthPixel;
            int i7 = this.mScanAreaCornerLengthPixel;
            if (this.mScanAreaMoveMode == 2) {
                i6 *= 2;
                i7 *= 2;
            }
            this.mPaint.setStrokeWidth((float) i6);
            int i8 = i6 / 2;
            int i9 = this.mDisplayMovableArea.right + i8;
            int i10 = this.mDisplayMovableArea.bottom + i8;
            float f5 = (float) i9;
            float f6 = (float) i10;
            Canvas canvas3 = canvas;
            float f7 = f5;
            float f8 = f6;
            canvas3.drawLine(f7, f8, f5, (float) (i10 - i7), this.mPaint);
            canvas3.drawLine(f7, f8, (float) (i9 - i7), f6, this.mPaint);
        }
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (this.mScanAreaMoveMode > 0) {
            if (checkTouchEvent(motionEvent) || super.onTouchEvent(motionEvent)) {
                return true;
            }
            return false;
        } else if (this.mGestureDetectorCompat.onTouchEvent(motionEvent) || super.onTouchEvent(motionEvent)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean checkTouchEvent(MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case 0:
                return true;
            case 1:
                if (this.mScanAreaMoveMode <= 0) {
                    return false;
                }
                this.mScanAreaMoveMode = 0;
                updateOrgScanArea();
                postInvalidate();
                return true;
            case 2:
                return moveScanArea(motionEvent.getX(), motionEvent.getY());
            default:
                return false;
        }
    }

    private void updateOrgScanArea() {
        Rect rect = this.mDisplayMovableArea;
        if (rect != null) {
            Rect rect2 = this.mOrgMovableArea;
            rect2.left = 0;
            rect2.top = 0;
            rect2.right = (int) Math.round(((double) (rect.width() - 1)) * this.mDisplayRate);
            this.mOrgMovableArea.bottom = (int) Math.round(((double) (this.mDisplayMovableArea.height() - 1)) * this.mDisplayRate);
            this.mOrgMovableArea.offsetTo((int) Math.round(((double) (this.mDisplayMovableArea.left - this.mDisplayContourArea.left)) * this.mDisplayRate), (int) Math.round(((double) (this.mDisplayMovableArea.top - this.mDisplayContourArea.top)) * this.mDisplayRate));
        }
    }

    private boolean moveScanArea(float f, float f2) {
        if (this.mDisplayMovableArea == null) {
            return false;
        }
        int round = Math.round(f - this.mPrevX);
        int round2 = Math.round(f2 - this.mPrevY);
        switch (this.mScanAreaMoveMode) {
            case 1:
                this.mDisplayMovableArea.left += round;
                this.mDisplayMovableArea.top += round2;
                break;
            case 2:
                this.mDisplayMovableArea.right += round;
                this.mDisplayMovableArea.bottom += round2;
                break;
            case 3:
                this.mDisplayMovableArea.left += round;
                this.mDisplayMovableArea.top += round2;
                this.mDisplayMovableArea.right += round;
                this.mDisplayMovableArea.bottom += round2;
                break;
            default:
                return false;
        }
        if (this.mDisplayMovableArea.left > this.mDisplayMovableArea.right) {
            int i = this.mDisplayMovableArea.left;
            Rect rect = this.mDisplayMovableArea;
            rect.left = rect.right;
            this.mDisplayMovableArea.right = i;
        }
        if (this.mDisplayMovableArea.top > this.mDisplayMovableArea.bottom) {
            int i2 = this.mDisplayMovableArea.top;
            Rect rect2 = this.mDisplayMovableArea;
            rect2.top = rect2.bottom;
            this.mDisplayMovableArea.bottom = i2;
        }
        if (this.mDisplayMovableArea.left < 0) {
            this.mDisplayMovableArea.left = 0;
        }
        if (this.mDisplayMovableArea.top < 0) {
            this.mDisplayMovableArea.top = 0;
        }
        if (this.mDisplayMovableArea.right >= getWidth()) {
            this.mDisplayMovableArea.right = getWidth() - 1;
        }
        if (this.mDisplayMovableArea.bottom >= getHeight()) {
            this.mDisplayMovableArea.bottom = getHeight() - 1;
        }
        this.mPrevX = f;
        this.mPrevY = f2;
        postInvalidate();
        return false;
    }

    public String getPromptString() {
        return this.mScanPromptString;
    }

    public void setPromptString(String str) {
        this.mScanPromptString = str;
        invalidateTextPaintAndMeasurements();
    }

    public int getPromptTextColor() {
        return this.mPromptTextColor;
    }

    public void setPromptTextColor(int i) {
        this.mPromptTextColor = i;
        invalidateTextPaintAndMeasurements();
    }

    public float getPromptTextSize() {
        return this.mPromptTextSize;
    }

    public void setPromptTextSize(float f) {
        this.mPromptTextSize = f;
        invalidateTextPaintAndMeasurements();
    }

    public void setPromptStringVisibility(int i) {
        this.mPromptTextDisplay = i == 0;
        invalidate();
    }

    public void setArea(int i, int i2) {
        this.mScanAreaModified = false;
        this.mOrgContourWidth = i;
        this.mOrgContourHeight = i2;
        this.mMovableAreaMode = false;
        calcDrawSize(getWidth(), getHeight());
    }

    public void setMovableAreaModeSize(int i, int i2, Rect rect) {
        this.mScanAreaModified = false;
        this.mOrgContourWidth = i;
        this.mOrgContourHeight = i2;
        this.mMovableAreaMode = true;
        this.mOrgMovableArea = new Rect(rect);
        calcDrawSize(getWidth(), getHeight());
    }

    public void setPreviewImage(String str, int[] iArr) {
        this.mPreviewImageFile = str;
        if (str == null || iArr == null || iArr.length < 4) {
            this.mOrgPreviewImageRect = null;
            this.mPreviewImageBitmap = null;
        } else {
            this.mOrgPreviewImageRect = new Rect(iArr[2], iArr[3], iArr[2] + iArr[0], iArr[3] + iArr[1]);
        }
        calcDrawSize(getWidth(), getHeight());
    }

    public void setPreviewImage(String str) {
        this.mPreviewImageFile = str;
        if (this.mMovableAreaMode) {
            this.mOrgPreviewImageRect = new Rect(this.mOrgMovableArea);
        } else {
            this.mOrgPreviewImageRect = new Rect(0, 0, this.mOrgContourWidth, this.mOrgContourHeight);
        }
        calcDrawSize(getWidth(), getHeight());
    }

    private void readPreviewBitmap() {
        String str = this.mPreviewImageFile;
        if (str == null) {
            this.mPreviewImageBitmap = null;
        } else {
            this.mPreviewImageBitmap = ImageUtil.loadBitmap(str, this.mDisplayPreviewImageRect.width(), this.mDisplayPreviewImageRect.height(), true, false);
        }
    }

    /* access modifiers changed from: private */
    public void checkFlick(float f) {
        if (this.mFlickCallback != null) {
            float min = ((float) Math.min(getWidth(), getHeight())) * 0.1f;
            if (f < (-min)) {
                this.mFlickCallback.flick(1);
            } else if (f > min) {
                this.mFlickCallback.flick(-1);
            }
        }
    }

    /* access modifiers changed from: private */
    public boolean checkMoveMode(float f, float f2) {
        Rect rect = this.mDisplayMovableArea;
        if (rect == null) {
            return false;
        }
        this.mPrevX = f;
        this.mPrevY = f2;
        if (Math.hypot((double) (((float) rect.left) - f), (double) (((float) this.mDisplayMovableArea.top) - f2)) < ((double) this.mScanAreaCornerLogTapAreaWidthPixel)) {
            this.mScanAreaMoveMode = 1;
        } else if (Math.hypot((double) (((float) this.mDisplayMovableArea.right) - f), (double) (((float) this.mDisplayMovableArea.bottom) - f2)) < ((double) this.mScanAreaCornerLogTapAreaWidthPixel)) {
            this.mScanAreaMoveMode = 2;
        } else if (f < ((float) this.mDisplayMovableArea.left) || f > ((float) this.mDisplayMovableArea.right) || f2 < ((float) this.mDisplayMovableArea.top) || f2 > ((float) this.mDisplayMovableArea.bottom)) {
            return false;
        } else {
            this.mScanAreaMoveMode = 3;
        }
        this.mScanAreaModified = true;
        postInvalidate();
        return true;
    }
}
