package com.kfzs.android.view.widget.ScalingImage;

import android.graphics.RectF;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * <pre>
 *     sinlov
 *
 *     /\__/\
 *    /`    '\
 *  ≈≈≈ 0  0 ≈≈≈ Hello world!
 *    \  --  /
 *   /        \
 *  /          \
 * |            |
 *  \  ||  ||  /
 *   \_oo__oo_/≡≡≡≡≡≡≡≡o
 *
 * </pre>
 * Created by sinlov on 16/5/18.
 */
public class DefaultOnDoubleTapListener implements GestureDetector.OnDoubleTapListener {
    private ScalingImageViewAttacher scalingImageViewAttacher;

    /**
     * Default constructor
     *
     * @param scalingImageViewAttacher scalingImageViewAttacher to bind to
     */
    public DefaultOnDoubleTapListener(ScalingImageViewAttacher scalingImageViewAttacher) {
        setScalingImageViewAttacher(scalingImageViewAttacher);
    }

    /**
     * Allows to change ScalingImageViewAttacher within range of single instance
     *
     * @param newScalingImageViewAttacher ScalingImageViewAttacher to bind to
     */
    public void setScalingImageViewAttacher(ScalingImageViewAttacher newScalingImageViewAttacher) {
        this.scalingImageViewAttacher = newScalingImageViewAttacher;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        if (this.scalingImageViewAttacher == null)
            return false;

        ImageView imageView = scalingImageViewAttacher.getImageView();

        if (null != scalingImageViewAttacher.getOnPhotoTapListener()) {
            final RectF displayRect = scalingImageViewAttacher.getDisplayRect();

            if (null != displayRect) {
                final float x = e.getX(), y = e.getY();

                // Check to see if the user tapped on the photo
                if (displayRect.contains(x, y)) {

                    float xResult = (x - displayRect.left)
                            / displayRect.width();
                    float yResult = (y - displayRect.top)
                            / displayRect.height();

                    scalingImageViewAttacher.getOnPhotoTapListener().onPhotoTap(imageView, xResult, yResult);
                    return true;
                } else {
                    scalingImageViewAttacher.getOnPhotoTapListener().onOutsidePhotoTap();
                }
            }
        }
        if (null != scalingImageViewAttacher.getOnViewTapListener()) {
            scalingImageViewAttacher.getOnViewTapListener().onViewTap(imageView, e.getX(), e.getY());
        }

        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent ev) {
        if (scalingImageViewAttacher == null)
            return false;

        try {
            float scale = scalingImageViewAttacher.getScale();
            float x = ev.getX();
            float y = ev.getY();

            if (scale < scalingImageViewAttacher.getMediumScale()) {
                scalingImageViewAttacher.setScale(scalingImageViewAttacher.getMediumScale(), x, y, true);
            } else if (scale >= scalingImageViewAttacher.getMediumScale() && scale < scalingImageViewAttacher.getMaximumScale()) {
                scalingImageViewAttacher.setScale(scalingImageViewAttacher.getMaximumScale(), x, y, true);
            } else {
                scalingImageViewAttacher.setScale(scalingImageViewAttacher.getMinimumScale(), x, y, true);
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            // Can sometimes happen when getX() and getY() is called
        }

        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        // Wait for the confirmed onDoubleTap() instead
        return false;
    }
}
