package com.prank.simulator.sounds.actfrag;

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.simulator.sounds.R;
import com.prank.simulator.sounds.databinding.ActRecordAudioBinding;
import com.prank.simulator.sounds.helper.Recorder;
import com.prank.simulator.sounds.helper.Utils;
import com.prank.simulator.sounds.topon.AdManager;
import com.prank.simulator.sounds.topon.onActionListener;

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

    private Recorder recorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActRecordAudioBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Utils.setStatusBarTextColor(this, true);
        recorder = new Recorder(this);
        AdManager.loadAllAd();
        binding.audioTime.setText(R.string.time_init);
        AdManager.showTopOn(RecordActivity.this, new onActionListener() {
            @Override
            public void onAction() {

            }
        });
        initEvent();
    }

    private void initEvent() {
        binding.back.setOnClickListener(v -> 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) {
            recorder.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 {
            recorder.stopRecording();

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

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