package com.xunao.effectdemo.activity;

import android.animation.Animator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

import com.xunao.effectdemo.R;
import com.xunao.effectdemo.bean.ListBean;
import com.xunao.effectdemo.dragview.DragViewUtil;
import com.xunao.effectdemo.view.MaskFilterView;

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

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/8/317:12
 * desc :
 * version: 1.0
 */
public class TestActivity extends Activity {
	private final static String TAG = "DragViewActivity";
	/** 三个容器 */
	private LinearLayout view1_container;
	private LinearLayout view2_container;
	private LinearLayout view3_container;
	private LinearLayout view4_container;
	/** 三要个拖动view */
	private LinearLayout view1;
	private LinearLayout view2;
	private LinearLayout view3,view_4;

	private RelativeLayout view_1;
	private LinearLayout view_2;
	private LinearLayout view_3;
	View viewCover;

	private MaskFilterView mask;
	private View v1;
	private ListBean bean;
	private List<ListBean> beanList = new ArrayList<>();//用于记录数据

	private List<LinearLayout> mlist = new ArrayList<>();
	boolean isResult = false;
	private Button btn;
	Animation animDown,animUp;
	private boolean isClose = false;
	private List<View> list = new ArrayList<>();

	private Handler handler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(@NonNull Message message) {
			switch (message.what){
				case 1:
					if (isResult){
						isResult = false;
						setResult();
					}
					break;
				case 2:

					break;
			}
			return false;
		}
	});

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		mask = findViewById(R.id.mask);
		mask.setBlurType(1);
		//三个容器
		view1_container = findViewById(R.id.view1_container);
		view2_container = findViewById(R.id.view2_container);
		view3_container = findViewById(R.id.view3_container);
		view4_container = findViewById(R.id.view4_container);
		//三个可拖拽view
		view1 = findViewById(R.id.view1);
		view2 = findViewById(R.id.view2);
		view3 = findViewById(R.id.view3);

		view_1 = findViewById(R.id.view_1);
		view_2 = findViewById(R.id.view_2);
		view_3 = findViewById(R.id.view_3);
		view_4 = findViewById(R.id.view_4);
		list.add(view_1);
		list.add(view_2);
		list.add(view_3);
		list.add(view_4);

		v1 = findViewById(R.id.v1);
		v1.setOnClickListener(v->{});

		mlist.add(view1_container);
		mlist.add(view2_container);
		mlist.add(view3_container);
		mlist.add(view4_container);

		setTouchText(view1_container);
		setTouchText(view2_container);
		setTouchText(view3_container);
		setTouchText(view4_container);


		btn = findViewById(R.id.btn);
		btn.setOnClickListener(v->setResult());
		handler.post(runnable);
	}

	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			beanList.clear();
			for (int i=0;i<4;i++){
				bean = new ListBean();
				bean.setView(mlist.get(i));
				Log.e("MyTag","top:"+mlist.get(i).getTop()+"  left:"+mlist.get(i).getLeft());
				bean.setAnswer((i+1)%4);
				bean.setNum(i);
				bean.setStatus(false);
				beanList.add(bean);
			}
		}
	};

	/**
	 * 计算最后答案
	 */
	void setResult(){
		for (ListBean b: beanList){
			if (b.getAnswer() != b.getNum()){
				for (ListBean b2: beanList){
					if (b2.getNum() == b.getAnswer()){
						isResult = true;
						changeToView(b.getView(),b2.getView(),500);
						return;
					}
				}
			}
		}
	}

	//交换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);
		moveToWord2(v1p,v2p,time);
	}

	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;

	void setTouchText(View 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();
						endTop = (int)v.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.setLeft(l);
							v.setTop(t);
							v.setRight(r);
							v.setBottom(b);
							isMove = true;
						}
						Log.e("MyTag","x:" + v.getY());
						if(Math.abs(v.getY())<200&&!isClose){
							isClose = true;
//							handler.sendEmptyMessage(2);

							for (ListBean b:beanList){
								if (b.getView().getId() == view.getId()){
									closeView(list.get(b.getNum()));
								}
							}
						}
						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);
				}
			}
		}
	}

	private void closeView(View v){
		v.clearAnimation();
		animUp= new TranslateAnimation(0, 0, 0, 40);
		animUp.setDuration(400);
		animUp.setRepeatCount(0);//动画的反复次数
		animUp.setFillAfter(true);//设置为true，动画转化结束后被应用
		animUp.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {

//				LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) v.getLayoutParams();
//				layoutParams.topMargin = -20;
//				layoutParams.bottomMargin = -20;
//				layoutParams.height = 260;
//				v.setLayoutParams(layoutParams);
//				for (ListBean bean:beanList){
//					if (bean.getView() == viewGroup){
//
//						bean.setStatus(true);
//					}
//				}

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}
		});
		v.startAnimation(animUp);
	}


	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());
	}
	private void moveToWord2(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.addListener(new Animator.AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animator) {

			}

			@Override
			public void onAnimationEnd(Animator animator) {

				handler.sendEmptyMessage(1);
			}

			@Override
			public void onAnimationCancel(Animator animator) {

			}

			@Override
			public void onAnimationRepeat(Animator animator) {

			}
		});
		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);
					}
				}
			}
		});
	}

}
