package com.rugvedsarika.easymath.utils

import android.content.Context
import android.media.AudioManager as SystemAudioManager
import android.media.ToneGenerator
import android.speech.tts.TextToSpeech
import java.util.Locale

class AudioManager(context: Context) {

    private var tts: TextToSpeech? = null
    private val toneGenerator = ToneGenerator(SystemAudioManager.STREAM_MUSIC, 100)

    var isSoundEnabled = true
    var isTtsEnabled = true

    init {
        // Initialize TTS
        tts = TextToSpeech(context) { status ->
            if (status == TextToSpeech.SUCCESS) {
                tts?.language = Locale.US
            }
        }
    }

    fun playCorrectSound() {
        if (!isSoundEnabled) return
        toneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP, 150)
    }

    fun playWrongSound() {
        if (!isSoundEnabled) return
        toneGenerator.startTone(ToneGenerator.TONE_CDMA_ABBR_ALERT, 200) // Lower/buzz sound
    }

    fun speak(text: String) {
        if (!isTtsEnabled) return
        tts?.speak(text, TextToSpeech.QUEUE_FLUSH, null, null)
    }

    fun shutdown() {
        tts?.stop()
        tts?.shutdown()
        toneGenerator.release()
    }
}
