package com.fun.wave.pranksounds.utils;

import android.content.Context;
import android.media.MediaRecorder;
import android.os.Handler;
import android.util.Log;

import com.fun.wave.pranksounds.listener.OnUpdateRecordingTime;

import java.io.File;
import java.io.IOException;


public class RecordUtils {
    private MediaRecorder mediaRecorder;
    private String tempFileName;
    private boolean isRecording = false;
    private boolean isPaused = false;

    private long startTime = 0L;
    private long pauseTime = 0L;
    private long totalPauseTime = 0L;


    private Runnable updateRunnable;
    private Handler mHandler;
    public RecordUtils(Context context, Handler handler, OnUpdateRecordingTime listener) {
        mHandler = handler;
        File dir = context.getCacheDir();
        File file = new File(dir, "temp_audio.mp3");
        tempFileName = file.getAbsolutePath();
        updateRunnable = new Runnable() {
            @Override
            public void run() {
                if (!isRecording || isPaused) return;

                long currentTime = System.currentTimeMillis();
                long recordTime = currentTime - startTime - totalPauseTime;
                listener.onUpdateTime(recordTime);
                handler.postDelayed(updateRunnable, 1000);
            }
        };
    }
    public void startTimer() {
        startTime = System.currentTimeMillis();
        totalPauseTime = 0L;
        isRecording = true;
        isPaused = false;
        mHandler.post(updateRunnable);
    }
    public void pauseTimer() {
        pauseTime = System.currentTimeMillis();
        isPaused = true;
        isRecording = false;
    }
    public void resumeTimer() {
        totalPauseTime += System.currentTimeMillis() - pauseTime;
        isPaused = false;
        isRecording = true;
        mHandler.post(updateRunnable);
    }
    public void stopTimer() {
        isRecording = false;
        isPaused = false;
        mHandler.removeCallbacks(updateRunnable);
    }


    public boolean isRecording() {
        return isRecording;
    }

    public void startRecording() {
        if (isRecording) return;
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setOutputFile(tempFileName);
        Log.d("================","=========startRecording");
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            startTimer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void resumeRecording() {
        if (!isRecording && isPaused) {
            mediaRecorder.resume();
            Log.d("================","=========resume");
            resumeTimer();
        }

    }
    public void pauseRecording() {
        if (isRecording && !isPaused) {
            Log.d("================","=========pauseRecording");
            mediaRecorder.pause();
            pauseTimer();

        }

    }

    public void stopRecording() {
        if (!isRecording) return;
        try {
            Log.d("================","=========stopRecording");
            mediaRecorder.stop();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } finally {
            stopTimer();
            mediaRecorder.release();
            mediaRecorder = null;
        }
    }

    public String getTempFileName() {
        return tempFileName;
    }
}
