package com.novelot.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.novelot.lunar.R;
import com.novelot.lunar.view.WeatherView;

/**
 * Created by 刘云龙 on 2016/10/27.
 */

public class StickyNavLayout extends LinearLayout {

    private static final float FRICTION = 1.0f;
    private static final String TAG = "novelot";
    private View mHeaderView;
    private FrameLayout mContentView;
    private float lastY;
    private float initY;
    private int mHeaderViewHeight;
    private int mScrollY;

    public StickyNavLayout(Context context) {
        super(context);
        init();
    }

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

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

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public StickyNavLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setOrientation(LinearLayout.VERTICAL);
        //
        mHeaderView = new WeatherView(getContext());
        mContentView = new FrameLayout(getContext());

        mHeaderView.setBackgroundResource(R.mipmap.bg_weather);

        mHeaderView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        mHeaderViewHeight = mHeaderView.getMeasuredHeight();

        LayoutParams paramsHeader = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mHeaderViewHeight);
        LayoutParams paramsContent = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        addView(mHeaderView, 0, paramsHeader);
        addView(mContentView, 1, paramsContent);

        PullMenuView innerView = new PullMenuView(getContext(), new String[]{"浏览器", "电话", "设置",
                "浏览器", "电话", "设置", "浏览器", "电话", "设置", "浏览器", "电话", "设置",
                "浏览器", "电话", "设置", "浏览器", "电话", "设置", "浏览器", "电话", "设置",
                "浏览器", "电话", "设置", "浏览器", "电话", "设置"});
        mContentView.addView(innerView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//        mContentView.addView(View.inflate(getContext(), R.layout.layout_sticky_nav, null));
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean isUpTouch = true;
//        float beginY = 0;
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_MOVE:
//                float scroll = beginY - event.getY();
//                if (scroll > 0) {
//                    //向上划
//                    isUpTouch = true;
//                } else {
//                    isUpTouch = false;
//                }
//                break;
//            case MotionEvent.ACTION_DOWN:
//                beginY = event.getY();
//                break;
//        }
//
//        //
//        if (isUpTouch) {
//            if (mScrollY < mHeaderViewHeight) {
//                Log.e(TAG, "onInterceptTouchEvent: mScrollY < mHeaderViewHeight mScrollY=" + mScrollY + ",mHeaderViewHeight=" + mHeaderViewHeight);
//                return true;
//            } else {
//                Log.e(TAG, "onInterceptTouchEvent:super mScrollY=" + mScrollY + ",mHeaderViewHeight=" + mHeaderViewHeight);
//                return super.onInterceptTouchEvent(event);
//            }
//        } else {
//            if (mScrollY > 0) {
//                return true;
//            } else {
//                return super.onInterceptTouchEvent(event);
//            }
//
//        }
        if (mScrollY < mHeaderViewHeight) {
            Log.e(TAG, "onInterceptTouchEvent: mScrollY < mHeaderViewHeight mScrollY=" + mScrollY + ",mHeaderViewHeight=" + mHeaderViewHeight);
            return true;
        } else {
            Log.e(TAG, "onInterceptTouchEvent:super mScrollY=" + mScrollY + ",mHeaderViewHeight=" + mHeaderViewHeight);
            return super.onInterceptTouchEvent(event);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                lastY = event.getY();
                int scroll = Math.round((initY - lastY + mScrollY) / FRICTION);
                if (scroll > 0) {
                    //向上划
                    if (scroll < mHeaderViewHeight) {
                        scrollTo(0, scroll);
                    }
                } else {
//                    if (scroll > -mHeaderViewHeight) {
//                        scrollTo(0, scroll);
//                    }
                }
                return true;
            case MotionEvent.ACTION_DOWN:
                initY = event.getY();
                return true;
            case MotionEvent.ACTION_UP:
//                scrollTo(0, 0);
                mScrollY = getScrollY() ;
                return true;
            default:
                return super.onTouchEvent(event);
        }
    }
}
