package com.xunao.effectdemo.activity;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.ClipData;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.xunao.effectdemo.R;
import com.xunao.effectdemo.bean.ListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/8/3116:18
 * desc :
 * version: 1.0
 */
public class SequentialDragActivity extends Activity {

	private static String TAG = "SequentialDragActivity";
	private RelativeLayout rl1,rl2,rl3,rl4,rl5;
	private RelativeLayout view1,view2,view3,view4,view5;
	private ListBean bean;
	private List<ListBean> beanList = new ArrayList<>();//用于记录数据
	private List<View> list = new ArrayList<>();
	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sequential_drag);
		rl1 = findViewById(R.id.rl_1);
		rl2 = findViewById(R.id.rl_2);
		rl3 = findViewById(R.id.rl_3);
		rl4 = findViewById(R.id.rl_4);
		rl5 = findViewById(R.id.rl_5);

		view1 = findViewById(R.id.view1);
		view2 = findViewById(R.id.view2);
		view3 = findViewById(R.id.view3);
		view4 = findViewById(R.id.view4);
		view5 = findViewById(R.id.view5);
		setTouchText(rl1);
		setTouchText(rl2);
		setTouchText(rl3);
		setTouchText(rl4);
		setTouchText(rl5);
//		setDrag(rl1,view1);
//		setDrag(rl2,view2);
//		setDrag(rl3,view3);
//		setDrag(rl4,view4);
//		setDrag(rl5,view5);
	}

	private void setDrag(View view1,View view2){
		view2.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent motionEvent) {

				//定义一个裁剪数据对象.随便定义一个就行,本demo用不到这个clipData.
				ClipData clipData = new ClipData(ClipData.newPlainText("view1", "aaa"));

				//定义一个拖拽阴影
				View.DragShadowBuilder myShadow = new View.DragShadowBuilder(view);

				//开始拖拽. 第三个参数在拖拽过程用来传递数据,这里我传view即被拖动的view自己,
				//后面可在DragEvent中获得这个参数view
				view.startDrag(clipData, myShadow, view, 0);

				return false;
			}
		});
		view1.setOnDragListener(onDragListener1);
	}

	View.OnDragListener onDragListener1 = new View.OnDragListener() {
		@Override
		public boolean onDrag(View view, DragEvent dragEvent) {
			Log.i(TAG, "container.onDrag called======");
			switch (dragEvent.getAction()) {
				case DragEvent.ACTION_DRAG_STARTED://开始.当有view被拖的时候收到此事件
					((View) dragEvent.getLocalState()).setVisibility(View.INVISIBLE);
					break;
				case DragEvent.ACTION_DRAG_ENTERED://进入.当被拖view手指按压的位置进入本view的瞬间收到此事件
					break;
				case DragEvent.ACTION_DRAG_LOCATION://位于.当被拖view手指按压的位置在本view上方的时候收到此事件

					//此时dragEvent里包含要被拖view
					View draggingView = (View) dragEvent.getLocalState();
					//当前容器
					ViewGroup thisContainer = ((ViewGroup) view);
					//被拖拽view覆盖的view
					View coveredView = thisContainer.getChildAt(0);
					//当拖拽的view跟被覆盖的view不同时
					if (draggingView != coveredView) {
						ViewGroup draggingViewContainer = (ViewGroup) draggingView.getParent();
						//两个容器交换子view
						thisContainer.removeView(coveredView);
						draggingViewContainer.removeView(draggingView);
						thisContainer.addView(draggingView);
						draggingViewContainer.addView(coveredView);
					}
					break;
				case DragEvent.ACTION_DROP://丢入.当被拖view在本view上方手指释放开,收到此事件
					Log.i(TAG, "丢入view_container2");
					break;
				case DragEvent.ACTION_DRAG_EXITED://出去.当被拖view手指按压位置从本view拖出去收到此事件
					break;
				case DragEvent.ACTION_DRAG_ENDED://结束.手指释放拖拽结束收到此事件.
					((View) dragEvent.getLocalState()).setVisibility(View.VISIBLE);
					break;
				default:
					break;
			}
			return true;
		}
	};

	private float downX;
	private float downY;
	private long downTime;
	private long delay;
	private boolean isMove;
	private boolean canDrag;
	private int left,enfLeft;
	private int top,endTop;
	private boolean isClose = false;
	void setTouchText(View view){
		list.add(view);
		view.setOnClickListener(v1->{});
		view.setOnTouchListener(new View.OnTouchListener() {
			@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();
						canDrag = true;
						v.bringToFront();
						break;
					case MotionEvent.ACTION_MOVE:

						if (!canDrag) {
							break;
						}
						final float xDistance = event.getX() - downX;
						final float yDistance = event.getY() - downY;
						enfLeft = (int)v.getX()-left;
						endTop = (int)v.getY()-top;
						Log.e("MyTag","x:"+enfLeft+"   y:"+endTop);
						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.setLeft(l);
							v.setTop(t);
							v.setRight(r);
							v.setBottom(b);
							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.setLeft(l);
						v.setTop(t);
						v.setRight(r);
						v.setBottom(b);
						judgeView(v,enfLeft,endTop);
						isClose = false;
						break;
					default:
						break;
				}
				return isMove;
			}
		});
	}

	private void judgeView(View view,int left,int top){
		for (ListBean b:beanList){
			if (b.getView().getId() != view.getId()){
				if (Math.abs(b.getView().getX()-left)<50&&
						Math.abs(b.getView().getY()-top)<50){
					changeToView((ViewGroup)view,b.getView(),0);
				}
			}
		}
	}
	//交换2个view
	private void changeToView(ViewGroup v1p,ViewGroup v2p,int time){

		int num;
		for (ListBean b:beanList){
			if (b.getView().getId() == v1p.getId()){
				num = b.getNum();
				for (ListBean b2:beanList){
					if (b2.getView().getId() == v2p.getId()){
						b.setNum(b2.getNum());
						b2.setNum(num);
					}
				}
			}
		}
		moveToWord(v2p,v1p,time);
		moveToWord(v1p,v2p,time);
	}

	private void moveToWord(View view, View targetView,int time) {
		final float x = view.getX();
		final float y = view.getY();

		final float targetX = targetView.getX();
		final float targetY = targetView.getY();
		ValueAnimator valueAnimator = new ValueAnimator();
		valueAnimator.setDuration(time);//600
		valueAnimator.setObjectValues(new PointF(x, y));
		valueAnimator.setInterpolator(new DecelerateInterpolator());
		//首先判断 目标点在上还是在下
		final boolean flagX = ((x - targetX) > 0) ? true : false;
		final boolean flagY = ((y - targetY) > 0) ? true : false;
		valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
			@Override
			public PointF evaluate(float fraction, PointF startValue,
								   PointF endValue) {
				//从0.0 --->>   1.0
				PointF point = new PointF();
				//这里是需要倒着来  最后要到达200 200 这个点

				float fractionNeed = 1 - fraction;

				if (flagX) {
					float vX = x - targetX;
					point.x = vX * fractionNeed + targetX;
				} else {
					float vX = targetX - x;
					point.x = x + vX * fraction;
				}

				if (flagY) {
					float vY = y - targetY;
					point.y = vY * fractionNeed + targetY;
				} else {
					float vY = targetY - y;
					point.y = y + vY * fraction;
				}
				return point;
			}
		});
		valueAnimator.start();
		valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				PointF point = (PointF) animation.getAnimatedValue();
				float vX = point.x;
				float vY = point.y;
				//说明vx 最大值就是view原坐标
				if (flagX) {
					if (vX <= x && vX >= targetX) {
						view.setX(vX);
					}
				} else {//说明vx 最小值就是view原坐标
					if (vX >= x && vX <= targetX) {
						view.setX(vX);
					}
				}

				//说明vY 最大值就是view原坐标
				if (flagY) {
					if (vY <= y && vY >= targetY) {
						view.setY(vY);
					}
				} else {//说明vx 最小值就是view原坐标
					if (vY >= y && vY <= targetY) {
						view.setY(vY);
					}
				}
			}
		});
//		view.setLeft(targetView.getLeft());
//		view.setTop(targetView.getTop());
//		view.setRight(targetView.getWidth());
//		view.setBottom(targetView.getHeight());
	}
}
