package com.xunao.effectdemo.activity;


import android.animation.Animator;
import android.annotation.SuppressLint;
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.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.airbnb.lottie.LottieAnimationView;
import com.xuexiang.xui.widget.imageview.RadiusImageView;
import com.xunao.effectdemo.R;
import com.xunao.effectdemo.bean.World;
import com.xunao.effectdemo.utils.FileUtil;
import com.xunao.effectdemo.utils.MediaRecorderDemo;

import java.io.File;

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

	float volume = 10000;
	private MediaRecorderDemo mRecorder;
	private static final int msgWhat = 0x1001;
	private static final int refreshTime = 100;
	private TextView tv;
	private RadiusImageView imv1;
	ScaleAnimation scaleAnim;
	private RelativeLayout rl;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (this.hasMessages(msgWhat)) {
				return;
			}
			volume = mRecorder.getMaxAmplitude();  //获取声压值
			if(volume > 0 && volume < 1000000) {
				World.setDbCount(20 * (float)(Math.log10(volume)));  //将声压值转为分贝值

				scaleAnim = new ScaleAnimation(World.getStart(), World.getToAnim(), World.getStart(), World.getToAnim(), Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
				scaleAnim.setDuration(0);
				scaleAnim.setFillAfter(true);
				imv1.startAnimation(scaleAnim);

			}
			handler.sendEmptyMessageDelayed(msgWhat, refreshTime);
		}
	};


	@SuppressLint("ClickableViewAccessibility")
	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sound);
		tv = findViewById(R.id.tv_sound);
		imv1 = findViewById(R.id.imv_1);

		mRecorder = new MediaRecorderDemo();

		tv.setOnClickListener(v->{
			File file = FileUtil.createFile("temp.amr");
			if (file != null) {
				Log.v("file", "file =" + file.getAbsolutePath());
				startRecord(file);
			} else {
				Toast.makeText(getApplicationContext(), "创建文件失败", Toast.LENGTH_LONG).show();
			}
		});
		imv1.setOnClickListener(v->{

		});
		rl = findViewById(R.id.rl_main);
		rl.setOnClickListener(v->{
		});
		rl.setOnTouchListener((view,event)->{
			switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:

					LottieAnimationView lottieAnimationView = new LottieAnimationView(SoundActivity.this); // 新建一个button
					lottieAnimationView.setAnimation("love.json");
					lottieAnimationView.loop(false);

					lottieAnimationView.addAnimatorListener(new Animator.AnimatorListener() {
						@Override
						public void onAnimationStart(Animator animator) {

						}

						@Override
						public void onAnimationEnd(Animator animator) {
							rl.removeView(lottieAnimationView);
						}

						@Override
						public void onAnimationCancel(Animator animator) {

						}

						@Override
						public void onAnimationRepeat(Animator animator) {

						}
					});
					RelativeLayout.LayoutParams msl1 = new RelativeLayout.LayoutParams(
							200, 200);
					msl1.leftMargin = (int) event.getX()-100;
					msl1.topMargin = (int) event.getY()-100;
					lottieAnimationView.setLayoutParams(msl1);
					rl.addView(lottieAnimationView);
					lottieAnimationView.playAnimation();
					Log.e("MyTag","点击事件2");
					break;
				case MotionEvent.ACTION_UP:

					break;
			}
			return false;
		});

	}

	private void startListenAudio() {
		handler.sendEmptyMessageDelayed(msgWhat, refreshTime);
	}

	/**
	 * 开始记录
	 * @param fFile
	 */
	public void startRecord(File fFile){
		try{
			mRecorder.setMyRecAudioFile(fFile);
			if (mRecorder.startRecorder()) {
				startListenAudio();
			}else{
				Toast.makeText(this, "启动录音失败", Toast.LENGTH_SHORT).show();
			}
		}catch(Exception e){
			Toast.makeText(this, "录音机已被占用或录音权限被禁止", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
	}


	@Override
	protected void onResume() {
		super.onResume();
//		soundDiscView = (SoundDiscView) findViewById(R.id.soundDiscView);

	}

	/**
	 * 停止记录
	 */
	@Override
	protected void onPause() {
		super.onPause();
		mRecorder.delete(); //停止记录并删除录音文件
		handler.removeMessages(msgWhat);
	}

	@Override
	protected void onDestroy() {
		handler.removeMessages(msgWhat);
		mRecorder.delete();
		super.onDestroy();
	}
}
