package com.fxy.baselibrary.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.fxy.baselibrary.language.other.CommSharedUtil;
import com.fxy.baselibrary.util.ScreenUtil;

/**
 * 桂林
 *随意拖动的view -会跟着页面动
 */

@SuppressLint("AppCompatCustomView")
public class DragView extends ImageView {
    private Context mContext;
    private ScrollView scroll_view;
    private int width;
    private int height;
    private int screenWidth;
    private int screenHeight;
    //ScrollView 的总高度
    private int scrollHeight;

    private float downX;
    private float downY;
    private Context context;

    private CommSharedUtil commSharedUtil;

    //是否拖动
    private boolean isDrag=false;

    public boolean isDrag() {
        return isDrag;
    }
    public DragView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width=getMeasuredWidth();
        height=getMeasuredHeight();
        screenWidth= ScreenUtil.getScreenWidth(context);
        screenHeight= ScreenUtil.getScreenHeight(context)-getStatusBarHeight();

        //页面有ScrollView 重新计数高度
        if (scroll_view!=null){
            scrollHeight= scroll_view.getChildAt(0).getMeasuredHeight();
            if (scrollHeight>screenWidth){
                screenHeight = scrollHeight-getStatusBarHeight();
            }
        }

    }
    public int getStatusBarHeight(){
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        return getResources().getDimensionPixelSize(resourceId);
    }

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

    //如果您想在运行时更新它，您应该在视图中实现它onDraw。
    //旋转也可以在onDraw的Canvas中完成
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setLocation(context);
    }


    public void setLocation(Context context){
        commSharedUtil = CommSharedUtil.getInstance(context);
        int left = commSharedUtil.getInt("dragViewLocationLeft",screenWidth-width);
        int top  = commSharedUtil.getInt("dragViewLocationTop",0);
        //超时高度
        if (screenHeight>0 && top>(screenHeight-height)){
            //过于靠近底部了
            top = screenHeight-height-height;
        }
        this.layout(left, top, left + width, top + height);
    }

    //保存当前位置
    public void setCurrentLocation(int left,int top){
        if (commSharedUtil==null){
            commSharedUtil = CommSharedUtil.getInstance(context);
        }
        commSharedUtil.putInt("dragViewLocationLeft",left);
        commSharedUtil.putInt("dragViewLocationTop",top);
    }


    @SuppressLint("ResourceType")
    public void init(View iv_drag, ScrollView scrollView, Context context){
        mContext = context;
        scroll_view = scrollView;
        Log.e("screenHeight：", String.valueOf(screenHeight));
        Log.e("打印数据", String.valueOf(scroll_view.getHeight()));
        iv_drag.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_MOVE && scroll_view!=null) {
                    scroll_view.requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }
        });
    }





    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (this.isEnabled()) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    isDrag=false;
                    downX = event.getX();
                    downY = event.getY();
                    Log.e("down---->", "getX=" + getX() );
                    break;
                case MotionEvent.ACTION_MOVE:
                    //Log.e("kid","ACTION_MOVE");
                    final float xDistance = event.getX() - downX;
                    final float yDistance = event.getY() - downY;
                    int l,r,t,b;
                    //当水平或者垂直滑动距离大于10,才算拖动事件
                    if (Math.abs(xDistance) >10 ||Math.abs(yDistance)>10) {
                        //Log.e("kid","Drag"); 229-261-319-351
                        isDrag=true;
                        l = (int) (getLeft() + xDistance);
                        r = l+width;
                        t = (int) (getTop() + yDistance);
                        b = t+height;
                        //不划出边界判断,此处应按照项目实际情况,因为本项目需求移动的位置是手机全屏,
                        // 所以才能这么写,如果是固定区域,要得到父控件的宽高位置后再做处理
                        if(l<0){
                            l=0;
                            r=l+width;
                        }else if(r>screenWidth){
                            r=screenWidth;
                            l=r-width;
                        }
                        if(t<0){
                            t=0;
                            b=t+height;
                        }else if(b>screenHeight){
                            b=screenHeight;
                            t=b-height;
                        }
                        //Log.e("实时获取高度", ""+scroll_view.getChildAt(0).getMeasuredHeight());
                        Log.e("kid",l+"-"+t+"-"+r+"-"+b);
                        Log.e("XY",event.getX()+"-"+event.getY());
                        this.layout(l, t, r, b);
                        setCurrentLocation(l,b);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    setPressed(false);
                    break;
                case MotionEvent.ACTION_CANCEL:
                    setPressed(false);
                    break;
            }
            return true;
        }
        return false;
    }

}
