package com.wayne.www.waynelib.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridView;
import android.widget.ListView;

public class NestedListview extends GridView {

    private GridView mParent;

    private float mDownY;

    public NestedListview(Context context) {
        super(context);
    }

    public NestedListview(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NestedListview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setParent(GridView view){
        mParent = view;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                setParentScrollAble(false);
                mDownY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                //
                if (isListViewReachTop() && ev.getY() - mDownY > 0) {
                    setParentScrollAble(true);
                } else if (isListViewReachBottom() && ev.getY() - mDownY < 0) {
                    setParentScrollAble(true);
                }
                break;
            case MotionEvent.ACTION_UP:
                super.smoothScrollToPosition(0, 0);
                super.smoothScrollBy(0, 0);
                if (ev.getY() - mDownY > 0) {
                    this.setSelection(this.getFirstVisiblePosition());
                } else if (ev.getY() - mDownY < 0) {
                    this.setSelection(this.getLastVisiblePosition());
                }

            case MotionEvent.ACTION_CANCEL:
                super.smoothScrollToPosition(0, 0);
                super.smoothScrollBy(0, 0);
                if (ev.getY() - mDownY > 0) {
                    this.setSelection(this.getFirstVisiblePosition());
                } else if (ev.getY() - mDownY < 0) {
                    this.setSelection(this.getLastVisiblePosition());
                }

                setParentScrollAble(true);
                break;
            case MotionEvent.ACTION_HOVER_EXIT:
                if (ev.getY() - mDownY > 0) {
                    this.setSelection(this.getFirstVisiblePosition());
                } else if (ev.getY() - mDownY < 0) {
                    this.setSelection(this.getLastVisiblePosition());
                }
                break;
            default:
                break;
        }
        return super.onTouchEvent(ev);
    }


    /**
     * @param flag
     */
    private void setParentScrollAble(boolean flag) {
        mParent.requestDisallowInterceptTouchEvent(!flag);
    }


    public boolean isListViewReachTop() {
        boolean result=false;
        if(getFirstVisiblePosition()==0){
            View topChildView = getChildAt(0);
            if (topChildView != null) {
                result=topChildView.getTop()==0;
            }
        }
        return result ;
    }

    public boolean isListViewReachBottom() {
        boolean result=false;
        if (getLastVisiblePosition() == (getCount() - 1)) {
            View bottomChildView = getChildAt(getLastVisiblePosition() - getFirstVisiblePosition());
            if (bottomChildView != null) {
                result= (getHeight() >= bottomChildView.getBottom());
            }
        }
        return  result;
    }

//    @Override
//    public void fling(int velocity) {
//        super.fling(velocity / 10000);
//    }
}