package com.fun.wave.pranksounds.custom;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;

import com.fun.pranksounds.R;
import com.fun.wave.pranksounds.base.BaseActivity;
import com.fun.pranksounds.databinding.AcRecordingBinding;
import com.fun.wave.pranksounds.utils.MyRecorderUtils;
import com.fun.wave.pranksounds.utils.OnUpdateRecordingTime;

public class AcRecording extends BaseActivity<AcRecordingBinding> {
    private Handler handler = new Handler(Looper.getMainLooper());

    private boolean isCreate = false;

    private MyRecorderUtils myRecorderUtils;

    @Override
    protected void init() {
        myRecorderUtils = new MyRecorderUtils(this, handler, new OnUpdateRecordingTime() {
            @Override
            public void onUpdateTime(long time) {

                int milliseconds = (int) (time % 1000) / 10;
                int seconds = (int) (time / 1000) % 60;
                int minutes = (int) (time / (1000 * 60)) % 60;
                viewBind.audioTime.setText(String.format("%02d : %02d : %02d", minutes, seconds, milliseconds));
            }
        });
        viewBind.audioTime.setText(R.string.time_init);
        initEvent();
    }

    @Override
    protected AcRecordingBinding initViewBinding() {
        return AcRecordingBinding.inflate(getLayoutInflater());
    }

    private void initEvent() {
        viewBind.back.setOnClickListener(v -> finish());
        viewBind.viewBtnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createSound(true);
            }
        });

        viewBind.viewPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createSound(false);
            }
        });
    }

    private void createSound(Boolean isStop) {
        if (!isCreate) {
            myRecorderUtils.startRecording();
            viewBind.lottieView.playAnimation();
            viewBind.viewPause.setSelected(true);
            isCreate = true;
        } else {
            if (isStop) {
                viewBind.lottieView.cancelAnimation();
                viewBind.viewPause.setSelected(false);
                myRecorderUtils.stopRecording();
                Intent intent = new Intent(AcRecording.this, AcSave.class);
//                intent.putExtra(AcSave.KEY_Time, elapsedTime);
                intent.putExtra(AcSave.KEY_Path, myRecorderUtils.getTempFileName());
                intent.putExtra(AcSave.KEY_import, 1);
                startActivity(intent);
                finish();
            } else {
                Log.d("================","--"+myRecorderUtils.isRecording());
                if (myRecorderUtils.isRecording()) {
                    viewBind.viewPause.setSelected(false);
                    myRecorderUtils.pauseRecording();
                    viewBind.lottieView.pauseAnimation();
                } else {
                    viewBind.viewPause.setSelected(true);
                    myRecorderUtils.resumeRecording();
                    viewBind.lottieView.resumeAnimation();
                }
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myRecorderUtils.stopRecording();
    }
}