package com.xunao.effectdemo.activity;

import android.app.Activity;
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.ScaleAnimation;
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 long downTime;
	private long delay;
	private boolean isMove;
	private boolean canDrag;
	private int left;
	private int top;
	RadiusImageView imv1,imv2;
	private float setAlpha;
	ScaleAnimation scaleAnim;
	private SeekBar seek;
	private float scale,toScale;
	private boolean leftDrag,rightDrag;

	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(){
		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();

						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) {
							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);
							isMove = true;
//							imv1.setImageAlpha();
							setAnim(v.getLeft());

						}
						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);
						isMove = false;
						imv1.setAlpha(0.3f);
						imv1.clearAnimation();
						imv2.setAlpha(0.3f);
						imv2.clearAnimation();
						scale = 1f;
						break;
					default:
						break;
				}
				return isMove;
			}
		});
	}

	private void setAnim(int l){
		if (left-l>0){
			setAlpha = ((float)left-l)/left ;
			Log.e("MyTag","设置透明度:"+setAlpha);

			if (setAlpha>0.3){
				imv1.setAlpha(setAlpha);
			}
			toScale = 2*setAlpha+1;
			Log.e("MyTag","数组:"+toScale);
			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){
			setAlpha = ((float)l-left)/left ;
			Log.e("MyTag","设置透明度:"+setAlpha);
			if (setAlpha>0.3){
				imv2.setAlpha(setAlpha);
			}
			toScale = 2*setAlpha+1;
			Log.e("MyTag","数组:"+toScale);
			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);

		}

	}
}
