package cn.yan.library;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Scroller;

/**
 * Created by yan on 17-1-8.
 *
 * like SlidingPaneLayout, but this used to mini lib, such as no support v4.
 *
 */

public class SlideLayout extends LinearLayout {
    public static final int STATE_CLOSE = 0;
    public static final int STATE_SLIDING = 1;
    public static final int STATE_OPEN = 2;

    private static final int ORI_RIGHT = 0;
    private static final int ORI_LEFT = 1;
    private static final int ORI_TOP = 2;
    private static final int ORI_BOTTOM = 3;

    private View mContentView;
    private View mSlideView;

    private Scroller mScroller;

    private int mLastX = 0;
    private int mLastY = 0;

    private int mSlideOrientation = ORI_RIGHT;

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

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

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

    private void init(Context context, AttributeSet attrs) {
        setOrientation(HORIZONTAL);
        mScroller = new Scroller(context);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() != 2) {
            throw new IllegalArgumentException("SlideLayout only need contains two child (content and slide).");
        }

        mContentView = getChildAt(0);
        mSlideView = getChildAt(1);
    }

    public int getSlideState() {
        int retValue = STATE_CLOSE;
        if (isScrolling) {
            retValue = STATE_SLIDING;
        } else {
            retValue = (getScrollX() == 0) ? STATE_CLOSE : STATE_OPEN;
        }
        return retValue;
    }

    public void smoothCloseSlide() {
        smoothScrollTo(0, 0);
    }

    public void smoothOpenSlide() {
        smoothScrollTo(mSlideView.getMeasuredWidth(), 0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return isScrolling || super.onInterceptTouchEvent(event);
    }

    boolean isScrolling = false;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int scrollX = getScrollX();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastX = (int) event.getX();
                mLastY = (int) event.getY();
                isScrolling = false;
                //Maybe child not set OnClickListener, so ACTION_DOWN need to return true and use super.
                super.dispatchTouchEvent(event);
                return true;
            case MotionEvent.ACTION_MOVE:
                int offsetX = x - mLastX;
                int offsetY = y - mLastY;
                if (/*Math.abs(offsetX) < 10 &&*/ Math.abs(offsetX) - Math.abs(offsetY) < 1) {
                    break;
                }
                getParent().requestDisallowInterceptTouchEvent(true);
                isScrolling = true;
                int newScrollX = scrollX - offsetX;
                if (offsetX != 0) {
                    if (newScrollX < 0) {
                        newScrollX = 0;
                    } else if (newScrollX > mSlideView.getMeasuredWidth()) {
                        newScrollX = mSlideView.getMeasuredWidth();
                    }
                    scrollTo(newScrollX, 0);
                }
                break;
            case MotionEvent.ACTION_UP:
                isScrolling = false;
                getParent().requestDisallowInterceptTouchEvent(false);
                int finalScrollX = 0;
                if (scrollX > mSlideView.getMeasuredWidth() / 2) {
                    finalScrollX = mSlideView.getMeasuredWidth();
                }
                smoothScrollTo(finalScrollX, 0);
                break;
        }

        mLastX = x;
        mLastY = y;
        return super.dispatchTouchEvent(event);
    }

    private void smoothScrollTo(int destX, int destY) {
        int scrollX = getScrollX();
        int delta = destX - scrollX;
        mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 3);
        invalidate();
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }
}

@@ -522,183 +522,8 @@
 2;%0A%0A
-    private static final int ORI_RIGHT = 0;%0A    private static final int ORI_LEFT = 1;%0A    private static final int ORI_TOP = 2;%0A    private static final int ORI_BOTTOM = 3;%0A%0A
     
@@ -549,16 +549,16 @@
 ntView;%0A
+
     priv
@@ -695,31 +695,26 @@
 lide
-Orientation = ORI_RIGHT
+SensitiveWidth = 0
 ;%0A%0A 
@@ -3722,21 +3722,30 @@
     
-if (scrollX %3E
+mSlideSensitiveWidth =
  mSl
@@ -3774,16 +3774,68 @@
 th() / 2
+;%0A                if (scrollX %3E mSlideSensitiveWidth
 ) %7B%0A    
@@ -4201,16 +4201,17 @@
 nt delta
+X
  = destX
@@ -4222,16 +4222,90 @@
 crollX;%0A
+        int scrollY = getScrollY();%0A        int deltaY = destY - scrollY;%0A
         
@@ -4339,41 +4339,112 @@
 lX, 
-0
+scrollY
 , delta
+X
 , 
-0, Math.abs(delta
+deltaY,%0A                (int) (Math.abs(Math.sqrt(deltaX*deltaX + deltaY*deltaY)
 ) * 3)
+)
 ;%0A  
@@ -4449,17 +4449,21 @@
         
-i
+postI
 nvalidat

fix some bug and future version need add four direction support and sensitive support.