package com.fun.funnyprank.activity;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;

import com.fun.funnyprank.R;
import com.fun.funnyprank.data.StaticValue;
import com.fun.funnyprank.databinding.ActivityRecordBinding;
import com.fun.funnyprank.tools.AudioRecorder;

public class RecordActivity extends AppCompatActivity {
    ActivityRecordBinding binding;
    private long startTime;
    private long elapsedTime;
    private Handler handler = new Handler();
    private Runnable runnable;
    private boolean isCreate = false;

    private AudioRecorder audioRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRecordBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        audioRecorder = new AudioRecorder(this);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.activity_record), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        binding.recordTime.setText(R.string.time_text);

        initEvent();
    }

    private void initEvent() {
        binding.recordBack.setOnClickListener(v -> finish());
        binding.recordPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createSound();
            }
        });
    }

    private void createSound() {
        if (!isCreate) {
            audioRecorder.startRecording();
            binding.recordPlay.setBackgroundResource(R.drawable.icon_record_pause);
            isCreate = true;
            runnable = new Runnable() {
                @SuppressLint("DefaultLocale")
                @Override
                public void run() {
                    long currentTime = SystemClock.elapsedRealtime();
                    elapsedTime = currentTime - startTime;
                    int milliseconds = (int) (elapsedTime % 1000) / 10;
                    int seconds = (int) (elapsedTime / 1000) % 60;
                    int minutes = (int) (elapsedTime / (1000 * 60)) % 60;
                    binding.recordTime.setText(String.format("%d : %02d : %02d", minutes, seconds, milliseconds));
                    handler.postDelayed(this, 10);
                }
            };
            startTime = SystemClock.elapsedRealtime();
            handler.post(runnable);
        } else {
            audioRecorder.stopRecording();
            binding.recordPlay.setBackgroundResource(R.drawable.icon_record_play);
            isCreate = false;
            handler.removeCallbacks(runnable);
            Intent intent = new Intent(RecordActivity.this, SaveActivity.class);
            intent.putExtra(StaticValue.KEY_Time, elapsedTime);
            intent.putExtra(StaticValue.KEY_Path, audioRecorder.getTempFileName());
            intent.putExtra(StaticValue.KEY_import, 1);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }
}