package com.xunao.effectdemo.activity;

import android.animation.Animator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xuexiang.xui.widget.imageview.RadiusImageView;
import com.xunao.effectdemo.R;

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

	private View view;

	private float downX;
	private float downY;
	private float inX;
	private float inY;
	private long downTime;
	private long delay;
	private boolean isMove;
	private boolean canDrag;
	private float left;
	private float top;
	private int lestLeft,lestTop;
	RadiusImageView imv1,imv2;
	private float setAlpha;
	ScaleAnimation scaleAnim;
	private SeekBar seek;
	private float scale,toScale;
	private RotateAnimation leftAnim,rightAnim;
	private boolean isLeft,isRight;
	private Animation returnAnim;

	Handler handler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(@NonNull Message message) {
			switch (message.what){
				case 1:
					imv1.setAlpha(setAlpha);
					break;
			}
			return false;
		}
	});

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_drag_select);
		init();
	}

	void init(){
		leftAnim = new RotateAnimation(0f,-30f,Animation.ABSOLUTE,0.5f,Animation.ABSOLUTE,0.5f);
		leftAnim.setDuration(300);
		leftAnim.setRepeatCount(0);
		leftAnim.setFillAfter(true);
		rightAnim = new RotateAnimation(0f,30f,Animation.ABSOLUTE,0.5f,Animation.ABSOLUTE,0.5f);
		rightAnim.setDuration(300);
		rightAnim.setRepeatCount(0);
		rightAnim.setFillAfter(true);


		view = findViewById(R.id.view);
		imv1 = findViewById(R.id.imv_1);
		seek = findViewById(R.id.seek);
		imv2 = findViewById(R.id.imv_2);

//		imv2.setAlpha(0.3f);
//		imv1.setAlpha(0.3f);
		scale = 1f;

		Log.e("MyTag","获取:"+getWindow().getWindowManager().getDefaultDisplay().getHeight());
		view.setOnClickListener(v->{});
		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 = true;
						downTime = System.currentTimeMillis();
						top = v.getTop();
						left = v.getLeft();
						v.bringToFront();

						inX = view.getX();
						inY = view.getY();
						break;
					case MotionEvent.ACTION_MOVE:
						final float xDistance = event.getX() - downX;
						final float yDistance = event.getY() - downY;
						Log.e("MyTag","x:" + v.getLeft() + "  y:"+event.getY());
						if (xDistance != 0 && yDistance != 0) {
							lestLeft = (int) (v.getLeft() + xDistance);
							int r = (int) (lestLeft + v.getWidth());
							lestTop = (int) (v.getTop() + yDistance);
							int b = (int) (lestTop + v.getHeight());
//                        v.layout(l, t, r, b);
							v.setLeft(lestLeft);
							v.setTop(lestTop);
							v.setRight(r);
							v.setBottom(b);
							isMove = true;
//							imv1.setImageAlpha();
							setAnim(v.getLeft());

						}
						break;
					case MotionEvent.ACTION_UP:
						isLeft = false;
						isRight = false;
						view.clearAnimation();
						int l = (int)left;
						int r = (int) (l + v.getWidth());
						int t = (int)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);
						setReturn(lestLeft,lestTop);
//						moveToWord(view,left,top,500,l,r,t,b);
						isMove = false;
						imv1.setAlpha(0.3f);
						imv1.clearAnimation();
						imv2.setAlpha(0.3f);
						imv2.clearAnimation();
						scale = 1f;
						break;
					default:
						break;
				}
				return isMove;
			}
		});
	}

	private void setReturn(float endLeft,float endTop){

		returnAnim = new TranslateAnimation(endLeft-left,0,endTop-top,0);
		returnAnim.setDuration(500);
		view.startAnimation(returnAnim);

	}

	private void setAnim(int l){
		if (left-l>0){
			setAlpha = ((float)left-l)/left ;
			if (!isLeft){
				isLeft = true;
				isRight = false;
				view.clearAnimation();
				view.startAnimation(leftAnim);
			}

			if (setAlpha>0.3){
				imv1.setAlpha(setAlpha);
			}
			toScale = 2*setAlpha+1;
			scaleAnim = new ScaleAnimation(scale, toScale, scale, toScale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
			scaleAnim.setDuration(0);
			scale = toScale;
			scaleAnim.setFillAfter(true);
			imv1.startAnimation(scaleAnim);

		}else if(left-l<0){
			if (!isRight){
				isRight = true;
				isLeft = false;
				view.clearAnimation();
				view.startAnimation(rightAnim);
			}
			setAlpha = ((float)l-left)/left ;
			if (setAlpha>0.3){
				imv2.setAlpha(setAlpha);
			}
			toScale = 2*setAlpha+1;
			scaleAnim = new ScaleAnimation(scale, toScale, scale, toScale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
			scaleAnim.setDuration(0);
			scale = toScale;
			scaleAnim.setFillAfter(true);
			imv2.startAnimation(scaleAnim);

		}

	}

	private void moveToWord(View view ,float targetX, float targetY, int time,int l,int r,int t,int b) {
		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) {
//				view.setLeft(l);
//				view.setTop(t);
//				view.setRight(r);
//				view.setBottom(b);
			}

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