package com.usai.redant.raimage.PhotoList.GridLayoutRecycler;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;

/**
 * Created by macmini1 on 2017/6/19.
 */

public class RAPreviewRecyclerView extends RecyclerView {


    private int totalPage = 0;
    private int curPage = 0;

    /**
     * 0: 停止滚动
     * 1: 开始滚动
     * 2: 手指移开
     *
     * */
    private int scrollState = 0;
    private int shortestDistance;
    private float slideDistance;
    private float scrollX = 0; // X轴当前的位置

    public PageIndicator indicator;

    public RAPreviewRecyclerView(Context context) {
        this(context, null);
    }

    public RAPreviewRecyclerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RAPreviewRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        super.onMeasure(widthSpec, heightSpec);
        shortestDistance = getMeasuredWidth() / 3;
        totalPage = getAdapter().getItemCount();
    }

    /**
     * 取消惯性滑动
     * 阻尼：1000为将惯性滚动速度缩小1000倍，近似drag操作。
     * */
    @Override
    public boolean fling(int velocityX, int velocityY) {
        return super.fling(velocityX / 1000, velocityY / 1000);
    }

    /**滚动*/
    @Override
    public void onScrolled(int dx, int dy) {
//        Log.d("Scroll","scroll X: " + dx);
        scrollX += dx;
        if (scrollState == 1) {
            slideDistance += dx;
        }
        super.onScrolled(dx, dy);
    }

    @Override
    public void onScrollStateChanged(int state) {

        switch (state) {
            case 2: {
                scrollState = 2;
            }
                break;
            case 1:
                scrollState = 1;
                break;
            case 0:
                if (slideDistance == 0) {
                    break;
                }
                scrollState = 0;

//                if (slideDistance < 0) { // 上页
//                    curPage = (int) Math.ceil(scrollX / getWidth());
////                    Log.d("Scroll","<0 scroll X: " + scrollX + " Cur :" + curPage);
//                    if (curPage * getWidth() - scrollX < shortestDistance) {
//                        curPage += 1;
//                    }
//                } else { // 下页
//                    curPage = (int) Math.ceil(scrollX / getWidth()) + 1;
////                    Log.d("Scroll",">0 scroll X: " + scrollX + " Cur :" + curPage);
//                    if (curPage <= totalPage) {
//                        if (scrollX - (curPage - 2) * getWidth() < shortestDistance) {
//                            // 如果这一页滑出距离不足，则定位到前一页
//                            curPage -= 1;
//                        }
//                    } else {
//                        curPage = totalPage;
//                    }
//                }

                int offsetX = computeHorizontalScrollOffset();
                int width = getWidth();
                curPage = (int)Math.ceil(offsetX / width);

                if (slideDistance < 0) {
                    if ((curPage * width) - offsetX > shortestDistance) {
                        curPage--;
                    }
                } else  {
                    if ((curPage * width) - offsetX < shortestDistance) {
                        curPage++;
                        if (curPage >= totalPage) {
                            curPage = totalPage - 1;
                        }
                    }
                }


                // 执行自动滚动
                scrollToPosition(curPage);
                // 修改指示器选中项
                if (indicator != null) {
                    indicator.changePageIndex(curPage,totalPage);
                }
                slideDistance = 0;
                break;
        }
        super.onScrollStateChanged(state);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {

        int action = e.getAction();
        if (action == ACTION_DOWN) {

        }

        if (action == ACTION_UP) {

        }

        if (action == ACTION_MOVE) {

        }

        return super.onTouchEvent(e);
    }

    @Override
    public void computeScroll() {

        super.computeScroll();
    }

    public void scrollToPage(int page) {
        curPage = page;
        scrollToPosition(curPage);
    }

    public interface PageIndicator {
        void changePageIndex(int index,int total);
    }
}
