package com.bassbooster.soundeffects.equalizer.utils

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager


import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaRecorder
import androidx.core.app.ActivityCompat

class AudioSessionIdHelper {

    fun getMicrophoneAudioSessionId(context: Context): Int? {
        val audioRecord: AudioRecord?
        val bufferSize = AudioRecord.getMinBufferSize(
            44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT
        )

        try {
            if (ActivityCompat.checkSelfPermission(
                    context,
                    Manifest.permission.RECORD_AUDIO
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return 0
            }
            audioRecord = AudioRecord(
                MediaRecorder.AudioSource.MIC,
                44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize
            )
        } catch (e: Exception) {
            e.printStackTrace()
            return null
        }

        val audioSessionId = audioRecord.audioSessionId
        audioRecord.release()

        return audioSessionId
    }
}
