package com.laseroptek.raman.utils

import android.content.Context
import android.media.AudioManager

object SystemSound {
    fun getCurrentSystemVolume(context: Context, streamType: Int): Int {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        return audioManager.getStreamVolume(streamType)
    }

    fun setCurrentSystemVolume(context: Context, streamType: Int, level: Int)  {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        return audioManager.setStreamVolume(streamType, level, AudioManager.FLAG_PLAY_SOUND)
    }

    fun getMaximumVolumeForStream(context: Context, streamType: Int): Int {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        return audioManager.getStreamMaxVolume(streamType)
    }

    fun getMinimumVolumeForStream(context: Context, streamType: Int): Int {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            audioManager.getStreamMinVolume(streamType)
        } else {
            0 // Common practice for older APIs
        }
    }
}

/*
@Composable
fun PlayBeepSound() {
    val toneGenerator = remember { ToneGenerator(AudioManager.STREAM_ALARM, 100) }

    DisposableEffect(Unit) {
        toneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP, 150 * 10)
        onDispose {
            toneGenerator.release() // Release the ToneGenerator when the composable is disposed
        }
    }
}

@Composable
fun PlayAlertSound(type: Int) {
    val context = LocalContext.current

    LaunchedEffect(Unit) {
        //val notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        //val notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
        //val notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
        val notification = RingtoneManager.getDefaultUri(type)
        val r = RingtoneManager.getRingtone(context, notification)
        r.play()
    }
}

@Composable
fun PlayDefaultSystemAlertSound(isPlaying: Boolean) {
    val context = LocalContext.current
    val notificationUri = remember { RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) }
    val ringtone = remember { RingtoneManager.getRingtone(context, notificationUri) }

    DisposableEffect(isPlaying) {
        if (isPlaying) {
            ringtone.play()
        } else {
            ringtone.stop()
        }
        onDispose {
            ringtone.stop() // Ensure sound is stopped when composable is disposed
        }
    }
}
*/

/*
@Composable
fun MyScreen() {
    var isSoundPlaying by remember { mutableStateOf(false) }

    PlayDefaultSystemAlertSound(isPlaying = isSoundPlaying)

    Button(onClick = { isSoundPlaying = !isSoundPlaying }) {
        Text(if (isSoundPlaying) "Stop Sound" else "Play Sound")
    }
}
*/