package com.sph.pdf;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.radaee.reader.PDFLayoutView;
import com.radaee.view.PDFLayout;

/**
 * Created by minfu on 5/6/15.
 */
public class SPHPDFLayoutView extends PDFLayoutView {

    public static boolean swipeable = false;
    public Context context = null;
    public long touchDownTime = -1;
    public long isMovingTime = -1;

    public GestureDetector gestureDetector = null;

    public SPHPDFLayoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        gestureDetector = new GestureDetector(this.context, new GestureListener());
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (m_layout != null) {
            if (m_layout.vGetZoom() > 1.0) {
                swipeable = false;
            } else {
                swipeable = true;
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        /*switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                touchDownTime = event.getEventTime();
                break;
            case MotionEvent.ACTION_MOVE:
                isMovingTime = event.getEventTime();
                break;
            case MotionEvent.ACTION_UP:
                if(event.getEventTime() - touchDownTime < ViewConfiguration.getTapTimeout() && (event.getEventTime() - isMovingTime < 20 || isMovingTime == -1)){
                    PDFActivity activity = (PDFActivity) context;
                    activity.tapDetected();
                    touchDownTime = -1;
                }
                isMovingTime = -1;
                break;
        }
        return super.onTouchEvent(event);*/
        gestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    public class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if (m_layout.vGetZoom() <= 1.0) {
                int x = (int) e.getX();
                int y = (int) e.getY();
                PDFLayout.PDFPos pos = m_layout.vGetPos(x, y);
                m_layout.vZoomSet(x, y, pos, 2.0f);
            }else if (m_layout.vGetZoom() > 1.0) {
                int x = (int) e.getX();
                int y = (int) e.getY();
                PDFLayout.PDFPos pos = m_layout.vGetPos(x, y);
                m_layout.vZoomSet(x, y, pos, 1.0f);
            }

            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            PDFActivity activity = (PDFActivity) context;
            activity.tapDetected();
            return super.onSingleTapConfirmed(e);
        }
    }
}
