package com.kuo.mychartlib.handler;

import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.widget.OverScroller;
import android.widget.Scroller;

import com.kuo.mychartlib.presenter.ChartCompute;

/*
 * Created by Kuo on 2016/4/1.
 */
public class ComputeScrollHandler{

    private float offsetX, offsetY;

    public static final int SCROLL_MODE = 0;
    public static final int FLING_MODE = 1;

    public int SCROLLER_MODE = -1;

    private Scroller mScroller;
    private OverScroller mOverScroller;
    private ViewConfiguration mViewConfiguration;
    private VelocityTracker mVelocityTracker;

    private int mScaledMinimumFlingVelocity;
    private int mScaledMaximumFlingVelocity;

    private float dX, dY;

    private ChartCompute chartCompute;

    public ComputeScrollHandler(Context context) {

        mScroller = new Scroller(context);

        mViewConfiguration = ViewConfiguration.get(context);

        mScaledMinimumFlingVelocity = mViewConfiguration.getScaledMinimumFlingVelocity();
        mScaledMaximumFlingVelocity = mViewConfiguration.getScaledMaximumFlingVelocity();

    }

    protected void setDownPosition(float dX, float dY) {
        this.dX = dX;
        this.dY = dY;
    }

    protected void setPreviousOffest(float x, float y, ChartCompute chartCompute) {
        offsetX = chartCompute.getCurViewport().left - x;
        offsetY = chartCompute.getCurViewport().top - y;
    }

    protected void startScroll(int x, int y, float distanceX, float distanceY, ChartCompute chartCompute) {
        SCROLLER_MODE = SCROLL_MODE;

        //float left = chartCompute.getCurViewport().left + x - dX;
        //float top = chartCompute.getCurViewport().top + y - dY;
        //float right  = left + chartCompute.getCurViewport().width();
        //float bottom = top + chartCompute.getCurViewport().height();

        float viewportOffsetX = -distanceX * chartCompute.getCurViewport().width() / chartCompute.getMinViewport().width();
        float viewportOffsetY = -distanceY * chartCompute.getCurViewport().height() / chartCompute.getMinViewport().height();


        float left = chartCompute.getCurViewport().left + viewportOffsetX;
        float top = chartCompute.getCurViewport().top + viewportOffsetY;
        float right  = left + chartCompute.getCurViewport().width();
        float bottom = top + chartCompute.getCurViewport().height();

        chartCompute.setCurViewport(left, top, right, bottom);
        //chartCompute.getCurViewport().left = chartCompute.getCurViewport().left + viewportOffsetX;
        //chartCompute.getCurViewport().top = chartCompute.getCurViewport().top + viewportOffsetY;
        //chartCompute.getCurViewport().right = chartCompute.getCurViewport().left + curW;
        //chartCompute.getCurViewport().bottom = chartCompute.getCurViewport().top + curH;

        //mScroller.startScroll(x, y, (int) offsetX, (int) offsetY, 1);
    }

    protected void startFling(int velocityX, int velocityY, ChartCompute chartCompute) {

        SCROLLER_MODE = FLING_MODE;

        int minX = ((int) chartCompute.getMinViewport().width() + (int) chartCompute.getMinViewport().left)
                - (int) chartCompute.getCurViewport().width();

        int maxX = (int) chartCompute.getMinViewport().left;

        int minY = ((int) chartCompute.getMinViewport().height() + (int) chartCompute.getMinViewport().top)
                - (int) chartCompute.getCurViewport().height();

        int maxY = (int) chartCompute.getMinViewport().top;

        mScroller.fling((int) chartCompute.getCurViewport().left, (int) chartCompute.getCurViewport().top,
                velocityX, velocityY,
                minX, maxX, minY, maxY);
    }

    protected void obtainVelocityTracker(MotionEvent event) {
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);
    }

    protected void computeCurrentVelocity(int units) {
        mVelocityTracker.computeCurrentVelocity(units, mScaledMaximumFlingVelocity);
    }

    protected void releaseVelocityTracker() {
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
    }

    protected void stopAnimation() {
        mScroller.abortAnimation();
    }

    protected void stopForce() {
        mScroller.forceFinished(true);
    }

    protected Scroller getScroller() {
        return mScroller;
    }

    protected int getCurScrollerMode() {
        return SCROLLER_MODE;
    }

}

@@ -1737,274 +1737,63 @@
   //
-float left = chartCompute.getCurViewport().left + x - dX;%0A        //float top = chartCompute.getCurViewport().top + y - dY;%0A        //float right  = left + chartCompute.getCurViewport().width();%0A        //float bottom = top + chartCompute.getCurViewport().height();%0A
+%E5%B0%87%E6%88%91%E5%80%91%E7%95%B6%E5%89%8DView%E7%9A%84%E9%AB%98%E5%BA%A6%E8%88%87%E9%A1%AF%E7%A4%BA%E7%9A%84%E9%AB%98%E5%BA%A6%E5%81%9A%E9%99%A4%E6%B3%95%E9%81%8B%E7%AE%97%EF%BC%8C%E5%9C%A8%E4%B9%98%E4%B8%8A%E7%95%B6%E5%89%8D%E7%9A%84X%7C%7CY%E4%BD%8D%E7%A7%BB%E7%9A%84%E4%BA%AE%EF%BC%8C%E4%BE%86%E9%81%BF%E5%85%8D%E5%9B%A0%E7%82%BA%E9%BB%9E%E6%93%8A%E8%80%8C%E9%80%A0%E6%88%90%E4%BD%8D%E7%A7%BB%E9%87%8F%E4%BA%82%E6%94%B9%E8%AE%8A
 %0A   
@@ -2040,17 +2040,16 @@
 ght();%0A%0A
-%0A
         
@@ -2319,32 +2319,32 @@
 rt().height();%0A%0A
+
         chartCom
@@ -2394,463 +2394,8 @@
 m);%0A
-        //chartCompute.getCurViewport().left = chartCompute.getCurViewport().left + viewportOffsetX;%0A        //chartCompute.getCurViewport().top = chartCompute.getCurViewport().top + viewportOffsetY;%0A        //chartCompute.getCurViewport().right = chartCompute.getCurViewport().left + curW;%0A        //chartCompute.getCurViewport().bottom = chartCompute.getCurViewport().top + curH;%0A%0A        //mScroller.startScroll(x, y, (int) offsetX, (int) offsetY, 1);%0A
     

Update ScrollHandler