package com.tofly.ghrq_route.common.ui.view;

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

import com.tubb.smrv.SwipeHorizontalMenuLayout;

/**
 * Created by Amu on 2017/2/15.
 */

public class MySwipeHorizontalMenuLayout extends SwipeHorizontalMenuLayout {
    public MySwipeHorizontalMenuLayout(Context context) {
        super(context);
    }

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

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

    public void smoothOpenMenu(int duration) {
        if (mCurrentSwiper != null){
            super.smoothOpenMenu(duration);
        }
    }

    public void smoothCloseMenu(int duration) {
        if (mCurrentSwiper != null){
            super.smoothCloseMenu(duration);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean isIntercepted = false;
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mDownX = mLastX = (int) ev.getX();
                mDownY = (int) ev.getY();
                isIntercepted = false;
                break;
            case MotionEvent.ACTION_MOVE:
                int disX = (int) (ev.getX() - mDownX);
                int disY = (int) (ev.getY() - mDownY);
                isIntercepted = Math.abs(disX) > mScaledTouchSlop && Math.abs(disX) > Math.abs(disY);
                break;
            case MotionEvent.ACTION_UP:
                isIntercepted = false;
                // menu view opened and click on content view,
                // we just close the menu view and intercept the up event
                if (isMenuOpened() && mCurrentSwiper != null
                        && mCurrentSwiper.isClickOnContentView(this, ev.getX())){
                        smoothCloseMenu();
                        isIntercepted = true;
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                isIntercepted = false;
                if (!mScroller.isFinished())
                    mScroller.forceFinished(false);
                break;
        }
        return isIntercepted;
    }
}
