package epson.common;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

import epson.print.Util.EPLog;

public class ScalableImageView extends View {
    private static final float MAX_SCALE_FACTOR = 5.0f;
    private static final float MIN_SCALE_FACTOR = 1.0f;
    private static final String TAG = "ScalableImageView";

    /* renamed from: bm */
    private Bitmap f332bm = null;
    private Rect canvasRect = new Rect();
    private GestureDetector gesDetect = null;
    private float mOffsetX = 0.0f;
    private float mOffsetY = 0.0f;
    private float mScaleFactor = 1.0f;
    private final GestureDetector.SimpleOnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
        public boolean onDoubleTap(MotionEvent motionEvent) {
            EPLog.d(TAG, "DOBULE TAP");
            if (mScaleFactor == 1.0f) {
                float x = (((-mOffsetX) + motionEvent.getX()) / mScaleFactor) / ( canvasRect.width());
                float y = (((-mOffsetY) + motionEvent.getY()) / mScaleFactor) / ( canvasRect.height());
                float unused = mScaleFactor = MAX_SCALE_FACTOR;
                float width = ( canvasRect.width()) * mScaleFactor;
                float height = ( canvasRect.height()) * mScaleFactor;
                float unused2 = mOffsetX = motionEvent.getX() - (width * x);
                float unused3 = mOffsetY = motionEvent.getY() - (height * y);
            } else {
                float unused4 = mScaleFactor = 1.0f;
                float unused5 = mOffsetX = 0.0f;
                float unused6 = mOffsetY = 0.0f;
            }
            invalidate();
            return true;
        }

        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
            float f3 = -f;
            float f4 = -f2;
            if (f3 > 0.0f) {
                ScalableImageView scalableImageView = ScalableImageView.this;
                float unused = mOffsetX = Math.min(mOffsetX + f3, 0.0f);
            } else {
                ScalableImageView scalableImageView2 = ScalableImageView.this;
                float unused2 = scalableImageView2.mOffsetX = Math.max(scalableImageView2.mOffsetX + f3, ( canvasRect.width()) - (( canvasRect.width()) * mScaleFactor));
            }
            if (f4 > 0.0f) {
                ScalableImageView scalableImageView3 = ScalableImageView.this;
                float unused3 = scalableImageView3.mOffsetY = Math.min(scalableImageView3.mOffsetY + f4, 0.0f);
            } else {
                ScalableImageView scalableImageView4 = ScalableImageView.this;
                float unused4 = scalableImageView4.mOffsetY = Math.max(scalableImageView4.mOffsetY + f4, ( canvasRect.height()) - (( canvasRect.height()) * mScaleFactor));
            }
            EPLog.d(TAG, "mOffsetX = " + mOffsetX + " mOffsetY = " + mOffsetY);
            invalidate();
            return true;
        }
    };
    private final ScaleGestureDetector.SimpleOnScaleGestureListener onScaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        private float focusX = 0.0f;
        private float focusY = 0.0f;
        float perFocusX = 0.0f;
        float perFocusY = 0.0f;

        public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
            focusX = scaleGestureDetector.getFocusX();
            focusY = scaleGestureDetector.getFocusY();
            perFocusX = (((-mOffsetX) + focusX) / mScaleFactor) / ( canvasRect.width());
            perFocusY = (((-mOffsetY) + focusY) / mScaleFactor) / ( canvasRect.height());
            return super.onScaleBegin(scaleGestureDetector);
        }

        public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
            ScalableImageView scalableImageView = this;
            float unused = mScaleFactor = mScaleFactor * scaleGestureDetector.getScaleFactor();
            ScalableImageView scalableImageView2 = this;
            float unused2 = scalableImageView2.mScaleFactor = Math.max(1.0f, Math.min(scalableImageView2.mScaleFactor, MAX_SCALE_FACTOR));
            if (mScaleFactor == 1.0f) {
                float unused3 = mOffsetX = 0.0f;
                float unused4 = mOffsetY = 0.0f;
            } else {
                float width = ( canvasRect.width()) * mScaleFactor;
                float height = ( canvasRect.height()) * mScaleFactor;
                float unused5 = mOffsetX = focusX - (width * perFocusX);
                float unused6 = mOffsetY = focusY - (height * perFocusY);
                ScalableImageView scalableImageView3 = ScalableImageView.this;
                float unused7 = scalableImageView3.mOffsetX = Math.min(0.0f, scalableImageView3.mOffsetX);
                ScalableImageView scalableImageView4 = ScalableImageView.this;
                float unused8 = scalableImageView4.mOffsetY = Math.min(0.0f, scalableImageView4.mOffsetY);
                ScalableImageView scalableImageView5 = ScalableImageView.this;
                float unused9 = scalableImageView5.mOffsetX = Math.max(scalableImageView5.mOffsetX, ( canvasRect.width()) - (( canvasRect.width()) * mScaleFactor));
                ScalableImageView scalableImageView6 = ScalableImageView.this;
                float unused10 = scalableImageView6.mOffsetY = Math.max(scalableImageView6.mOffsetY, ( canvasRect.height()) - (( canvasRect.height()) * mScaleFactor));
            }
            EPLog.d(TAG, "mScaleFactor = " + mScaleFactor + " mOffsetX = " + mOffsetX + " mOffsetY = " + mOffsetY);
            invalidate();
            return true;
        }
    };
    private ScaleGestureDetector scaleGesDetect = null;

    public ScalableImageView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        scaleGesDetect = new ScaleGestureDetector(context, onScaleGestureListener);
        gesDetect = new GestureDetector(context, onGestureListener);
    }

    public Bitmap getBitmap() {
        return f332bm;
    }

    public void setImageBitmap(Bitmap bitmap) {
        f332bm = bitmap;
    }

    public void SetImageBitmap(Bitmap bitmap, Resources resources) {
        if (f332bm != bitmap) {
            f332bm = bitmap;
        }
    }

    public float getmScaleFactor() {
        return mScaleFactor;
    }

    public void setmScaleFactor(float f) {
        mScaleFactor = f;
    }

    public float getmOffsetX() {
        return mOffsetX;
    }

    public void setmOffsetX(float f) {
        mOffsetX = f;
    }

    public float getmOffsetY() {
        return mOffsetY;
    }

    public void setmOffsetY(float f) {
        mOffsetY = f;
    }

    protected void onConfigurationChanged(Configuration configuration) {
        mScaleFactor = 1.0f;
        mOffsetX = 0.0f;
        mOffsetY = 0.0f;
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (f332bm != null) {
            canvasRect.right = canvas.getWidth();
            canvasRect.bottom = canvas.getHeight();
            float min = Math.min(( canvasRect.width()) / ( f332bm.getWidth()), ( canvasRect.height()) / ( f332bm.getHeight()));
            Rect rect = new Rect(((( canvasRect.width()) / 2.0f) - ((( f332bm.getWidth()) * min) / 2.0f)), ((( canvasRect.height()) / 2.0f) - ((( f332bm.getHeight()) * min) / 2.0f)), ((( canvasRect.width()) / 2.0f) + ((( f332bm.getWidth()) * min) / 2.0f)), ((( canvasRect.height()) / 2.0f) + ((( f332bm.getHeight()) * min) / 2.0f)));
            canvas.save();
            canvas.translate(mOffsetX, mOffsetY);
            float f = mScaleFactor;
            canvas.scale(f, f);
            Bitmap bitmap = f332bm;
            canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), f332bm.getHeight()), rect, (Paint) null);
            canvas.restore();
        }
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (motionEvent == null) {
            return false;
        }
        scaleGesDetect.onTouchEvent(motionEvent);
        if (scaleGesDetect.isInProgress()) {
            return true;
        }
        gesDetect.onTouchEvent(motionEvent);
        return true;
    }

    public void SetRotate(float f) {
        Matrix matrix = new Matrix();
        matrix.postRotate(f);
        Bitmap bitmap = f332bm;
        f332bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), f332bm.getHeight(), matrix, true);
        invalidate();
    }
}
