package com.gingersoft.gsa.cloud.main.mvp.ui.view;

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

import com.gingersoft.gsa.cloud.main.R;
import com.jess.arms.utils.ArmsUtils;
import com.nineoldandroids.view.ViewHelper;

import static com.qmuiteam.qmui.util.QMUIDisplayHelper.dp2px;

/**
 * des ：SlidingMenu 側邊欄側滑菜單
 */
public class SlidingMenu extends HorizontalScrollView {
    private int mScreenWidth;
    private int mMenuRightPadding;
    private int mMenuWidth;
    private int mHalfMenuWidth;
    private boolean isOpen;
    private boolean once;
    private ViewGroup mMenu;
    private ViewGroup mContent;
    private View touchView;

    private OnOpenChangedListenter onOpenChangedListenter;

    public interface OnOpenChangedListenter {
        void onChange(boolean isOpen);
    }

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

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

    public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mScreenWidth = ArmsUtils.getScreenWidth(context);
        mMenuRightPadding = dp2px(context, 140);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /**
         * 顯示設置一個寬度
         */
        if (!once) {
            LinearLayout wrapper = (LinearLayout) getChildAt(0);
            mMenu = (ViewGroup) wrapper.getChildAt(0);
            mContent = (ViewGroup) wrapper.getChildAt(1);
            touchView = mContent.findViewById(R.id.line_chart_view);
            mMenuWidth = mScreenWidth - mMenuRightPadding;
            mHalfMenuWidth = mMenuWidth / 2;
            mMenu.getLayoutParams().width = mMenuWidth;
            mContent.getLayoutParams().width = mScreenWidth;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
//      将菜单隐藏
            this.scrollTo(mMenuWidth, 0);
            once = true;
        }
    }

    private float downX, downY;
    private long downTimeMillis;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            downX = ev.getX();
            downY = ev.getY();
            downTimeMillis = System.currentTimeMillis();
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            if (isOpen && downX > mContent.getLeft() && ev.getX() > mContent.getLeft()
                    && System.currentTimeMillis() < downTimeMillis + 500) {
                closeMenu();
                return true;
            }
            return false;
        } else if (touchView != null) {
            float x = ev.getX();
            float y = ev.getY();
            if (x < touchView.getRight() && x > touchView.getLeft()
                    && y > touchView.getTop() && y < touchView.getBottom()) {
                return false;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            // Up时，进行判断，如果显示区域大于菜单宽度一半则完全显示，否则隐藏
            case MotionEvent.ACTION_UP: {
                int scrollX = getScrollX();
                if (scrollX > mHalfMenuWidth) {
                    this.smoothScrollTo(mMenuWidth, 0);
                    setOpen(false);
                } else {
                    this.smoothScrollTo(0, 0);
                    setOpen(true);
                }
                return true;
            }
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 打開菜單
     */
    public void openMenu() {
        if (isOpen)
            return;
        this.smoothScrollTo(0, 0);
        setOpen(true);
        invalidate();
    }

    /**
     * 關閉菜單
     */
    public void closeMenu() {
        if (isOpen) {
            this.smoothScrollTo(mMenuWidth, 0);
            setOpen(false);
        }
    }

    /**
     * 切换菜单状态
     */
    public void toggle() {
        if (isOpen) {
            closeMenu();
        } else {
            openMenu();
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        float scale = l * 1.0f / mMenuWidth;
        float leftScale = 1 - 0.3f * scale;
        float rightScale = 0.8f + scale * 0.2f;
        ViewHelper.setScaleX(mMenu, leftScale);
        ViewHelper.setScaleY(mMenu, leftScale);
        ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));
        ViewHelper.setTranslationX(mMenu, mMenuWidth * scale * 0.7f);
        ViewHelper.setPivotX(mContent, 0);
        ViewHelper.setPivotY(mContent, mContent.getHeight() / 2);
        ViewHelper.setScaleX(mContent, rightScale);
        ViewHelper.setScaleY(mContent, rightScale);
    }

    public OnOpenChangedListenter getOnOpenChangedListenter() {
        return onOpenChangedListenter;
    }

    public void setOnOpenChangedListenter(OnOpenChangedListenter onOpenChangedListenter) {
        this.onOpenChangedListenter = onOpenChangedListenter;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setOpen(boolean open) {
        isOpen = open;
        if (onOpenChangedListenter != null) {
            onOpenChangedListenter.onChange(isOpen);
        }
    }
}