package com.prank.sound.funnyapp.activity;

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 androidx.appcompat.app.AppCompatActivity;

import com.prank.sound.funnyapp.R;
import com.prank.sound.funnyapp.databinding.ActivityStartRecorderBinding;
import com.prank.sound.funnyapp.max.MaxManager;
import com.prank.sound.funnyapp.max.onAdAfterAction;
import com.prank.sound.funnyapp.mytool.Common;
import com.prank.sound.funnyapp.mytool.RecorderManager;

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

    private RecorderManager recorderManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityStartRecorderBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Common.setStatusBarTextColor(this, true);

        MaxManager.onLoadAd();
        MaxManager.startShowMaxAd(this, new onAdAfterAction() {
            @Override
            public void onAction() {

            }
        });

        recorderManager = new RecorderManager(this);

        binding.audioTime.setText(R.string.time_init);

        initEvent();
    }

    private void initEvent() {
        binding.back.setOnClickListener(v -> {
            MaxManager.startShowMaxAd(this, new onAdAfterAction() {
                @Override
                public void onAction() {
                    finish();
                }
            });
        });
        binding.imMic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.imMic.setSelected(!binding.imMic.isSelected());
                createSound();
            }
        });
    }

    private void createSound() {
        if (!isCreate) {
            recorderManager.startRecording();
            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.audioTime.setText(String.format("%02d : %02d : %02d", minutes, seconds, milliseconds));
                    handler.postDelayed(this, 10);
                }
            };
            startTime = SystemClock.elapsedRealtime();
            handler.post(runnable);
        } else {
            recorderManager.stopRecording();

            handler.removeCallbacks(runnable);
            Intent intent = new Intent(StartRecorderActivity.this, SaveSoundsActivity.class);
            intent.putExtra(SaveSoundsActivity.KEY_Time, elapsedTime);
            intent.putExtra(SaveSoundsActivity.KEY_Path, recorderManager.getTempFileName());
            intent.putExtra(SaveSoundsActivity.KEY_import, 1);
            startActivity(intent);
            finish();
        }
    }

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