package com.fxy.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.fxy.baselibrary.language.other.CommSharedUtil;

/**
 * 作者 桂林
 * 浮动按钮
 */
@SuppressLint("AppCompatCustomView")
public class FloatingImageView extends ImageView {
    private int parentHeight;
    private int parentWidth;
    private int width ;
    private int height;
    private int lastX;
    private int lastY;

    private Context context;
    private CommSharedUtil commSharedUtil;




    private boolean isDrag;
    public boolean isDrag() {
        return isDrag;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }

    public FloatingImageView(Context context) {
        super(context);
        this.context=context;
    }

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

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

    /**
     * 设置初始位置
     * @param context
     */
    public void setLocation(Context context){
        commSharedUtil = CommSharedUtil.getInstance(context);
        int left = commSharedUtil.getInt("dragViewLocationLeft",parentWidth);
        int top  = commSharedUtil.getInt("dragViewLocationTop",0);
        //超时宽度或者小于
        if (parentWidth>0 && (left>parentWidth || left<0)){
            left = parentWidth;
            commSharedUtil.putInt("dragViewLocationLeft",parentWidth);
        }
        if (parentHeight>0 && (top<0 || top>parentHeight)){
            top = 0;
            commSharedUtil.putInt("dragViewLocationLeft",0);
        }

        setX(left);
        setY(top);
    }

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        int rawX = (int) event.getRawX();
        int rawY = (int) event.getRawY();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                setPressed(true);
                isDrag = false;
                getParent().requestDisallowInterceptTouchEvent(true);
                lastX = rawX;
                lastY = rawY;
                ViewGroup parent;
                if (getParent() != null) {
                    parent = (ViewGroup) getParent();
                    parentHeight = parent.getHeight();
                    parentWidth = parent.getWidth();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (parentHeight <= 0 || parentWidth == 0) {
                    isDrag = false;
                    break;
                } else {
                    isDrag = true;
                }
                int dx = rawX - lastX;
                int dy = rawY - lastY;
                //这里修复一些华为手机无法触发点击事件
                int distance = (int) Math.sqrt(dx * dx + dy * dy);
                if (distance == 0) {
                    isDrag = false;
                    break;
                }
                float x = getX() + dx;
                float y = getY() + dy;
                //检测是否到达边缘 左上右下
                x = x < 0 ? 0 : x > parentWidth - getWidth() ? parentWidth - getWidth() : x;
                y = getY() < 0 ? 0 : getY() + getHeight() > parentHeight ? parentHeight - getHeight() : y;
                setX(x);
                setY(y);
                lastX = rawX;
                lastY = rawY;
                //Log.i("aa", "isDrag=" + isDrag + "getX=" + getX() + ";getY=" + getY() + ";parentWidth=" + parentWidth);
                setCurrentLocation((int) getX(),(int) getY());
                break;
            case MotionEvent.ACTION_UP:
                if (!isNotDrag()) {
                    //恢复按压效果
                    setPressed(false);
                   /* if (rawX >= parentWidth / 2) {
                        //靠右吸附
                        animate().setInterpolator(new DecelerateInterpolator())
                                .setDuration(500)
                                .xBy(parentWidth - getWidth() - getX())
                                .start();
                    } else {
                        //靠左吸附
                        ObjectAnimator oa = ObjectAnimator.ofFloat(this, "x", getX(), 0);
                        oa.setInterpolator(new DecelerateInterpolator());
                        oa.setDuration(500);
                        oa.start();
                    }*/
                }
                break;
            default:
                break;
        }
        //如果是拖拽则消s耗事件，否则正常传递即可。
        return !isNotDrag() || super.onTouchEvent(event);
    }

    private boolean isNotDrag() {
        return !isDrag && (getX() == 0|| (getX() == parentWidth - getWidth()));
    }
}
