package com.xunao.effectdemo.dragview;

import android.content.ClipData;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/8/1013:28
 * desc :
 * version: 1.0
 */
public class DragViewUtil {

	public static void registerDragAction(View v) {
		registerDragAction(v, 0);

	}

	/**
	 * 拖动View方法
	 *
	 * @param v     view
	 * @param delay 延迟
	 */
	public static void registerDragAction(View v, long delay) {
		v.setOnTouchListener(new TouchListener(delay));
		v.setOnDragListener(onDragListener1);
	}

	private static class TouchListener implements View.OnTouchListener {
		private float downX;
		private float downY;
		private long downTime;
		private long delay;
		private boolean isMove;
		private boolean canDrag;
		private int left;
		private int top;

		private TouchListener() {
			this(0);
		}

		private TouchListener(long delay) {
			this.delay = delay;
		}

		private boolean haveDelay() {
			return delay > 0;
		}

		@Override
		public boolean onTouch(View v, MotionEvent event) {

			switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					downX = event.getX();
					downY = event.getY();
					isMove = false;
					downTime = System.currentTimeMillis();
					top = v.getTop();
					left = v.getLeft();
					Log.e("MyTag","top:"+top+  "  left:"+left+"  w:h"+v.getWidth()+":"+v.getHeight());
					if (haveDelay()) {
						canDrag = false;
					} else {
						canDrag = true;
					}
					v.bringToFront();
					break;
				case MotionEvent.ACTION_MOVE:
					if (haveDelay() && !canDrag) {
						long currMillis = System.currentTimeMillis();
						if (currMillis - downTime >= delay) {
							canDrag = true;
						}
					}
					if (!canDrag) {
						break;
					}
					final float xDistance = event.getX() - downX;
					final float yDistance = event.getY() - downY;
					Log.e("MyTag","x:" + event.getX() + "  y:"+event.getY());
					if (xDistance != 0 && yDistance != 0) {
						int l = (int) (v.getLeft() + xDistance);
						int r = (int) (l + v.getWidth());
						int t = (int) (v.getTop() + yDistance);
						int b = (int) (t + v.getHeight());
//                        v.layout(l, t, r, b);
						v.setLeft(l);
						v.setTop(t);
						v.setRight(r);
						v.setBottom(b);
//						v.bringToFront();
						isMove = true;
					}
					break;
				case MotionEvent.ACTION_UP:

					int l = left;
					int r = (int) (l + v.getWidth());
					int t = top;
					int b = (int) (t + v.getHeight());
//                        v.layout(l, t, r, b);
					v.setLeft(l);
					v.setTop(t);
					v.setRight(r);
					v.setBottom(b);

					break;
				default:
					break;
			}
			return isMove;
		}

	}

	private static View.OnDragListener onDragListener1 = new View.OnDragListener() {
		@Override
		public boolean onDrag(View view, DragEvent dragEvent) {
			switch (dragEvent.getAction()) {
				case DragEvent.ACTION_DRAG_STARTED://开始.当有view被拖的时候收到此事件
					break;
				case DragEvent.ACTION_DRAG_ENTERED://进入.当被拖view手指按压的位置进入本view的瞬间收到此事件

					break;
				case DragEvent.ACTION_DRAG_LOCATION://位于.当被拖view手指按压的位置在本view上方的时候收到此事件

					break;
				case DragEvent.ACTION_DROP://丢入.当被拖view在本view上方手指释放开,收到此事件

					break;
				case DragEvent.ACTION_DRAG_EXITED://出去.当被拖view手指按压位置从本view拖出去收到此事件
//                        Log.i(TAG, "出去");
					break;
				case DragEvent.ACTION_DRAG_ENDED://结束.手指释放拖拽结束收到此事件.
					View draggingView = (View) dragEvent.getLocalState();
					draggingView.setVisibility(View.VISIBLE);
					break;
				default:
					break;
			}
			//一定要为true
			return true;
		}
	};

}
