package com.fun.wave.pranksounds.mine;

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

import com.fun.wave.pranksounds.R;
import com.fun.wave.pranksounds.base.BaseActivity;
import com.fun.wave.pranksounds.databinding.RecordingActivityBinding;
import com.fun.wave.pranksounds.listener.OnUpdateRecordingTime;
import com.fun.wave.pranksounds.utils.RecordUtils;

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

    private boolean isCreate = false;

    private RecordUtils recordUtils;

    @Override
    protected void init() {
        recordUtils = new RecordUtils(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 RecordingActivityBinding initViewBinding() {
        return RecordingActivityBinding.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) {
            recordUtils.startRecording();
            viewBind.lottieView.playAnimation();
            viewBind.viewPause.setSelected(true);
            isCreate = true;
        } else {
            if (isStop) {
                viewBind.lottieView.cancelAnimation();
                viewBind.viewPause.setSelected(false);
                recordUtils.stopRecording();
                Intent intent = new Intent(RecordingActivity.this, SaveActivity.class);
//                intent.putExtra(AcSave.KEY_Time, elapsedTime);
                intent.putExtra(SaveActivity.KEY_Path, recordUtils.getTempFileName());
                intent.putExtra(SaveActivity.KEY_import, 1);
                startActivity(intent);
                finish();
            } else {
                Log.d("================","--"+ recordUtils.isRecording());
                if (recordUtils.isRecording()) {
                    viewBind.viewPause.setSelected(false);
                    recordUtils.pauseRecording();
                    viewBind.lottieView.pauseAnimation();
                } else {
                    viewBind.viewPause.setSelected(true);
                    recordUtils.resumeRecording();
                    viewBind.lottieView.resumeAnimation();
                }
            }

        }
    }

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