package com.example.lexilearn.ui.components

import LottieAnimationComponent
import android.speech.tts.TextToSpeech
import android.speech.tts.TextToSpeech.OnInitListener
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import kotlinx.coroutines.delay
import java.util.*

@Composable
fun RightAnswerDialog(onDismiss: () -> Unit) {
    // Inisialisasi TextToSpeech
    val context = LocalContext.current
    val tts = remember {
        TextToSpeech(context, OnInitListener { status ->
        })
    }
    tts.language = Locale("id", "ID")


    // Ketika dialog muncul, TTS akan berbicara "Jawaban Salah"
    LaunchedEffect(Unit) {
        tts.speak("Jawaban Benar", TextToSpeech.QUEUE_FLUSH, null, null)
        delay(2000)  // Delay 3 detik
        onDismiss()
    }

    Dialog(onDismissRequest = onDismiss) {
        Surface(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            color = Color.Transparent,
            shape = MaterialTheme.shapes.medium
        ) {
            LottieAnimationComponent(
                assetName = "right_answer.json",
                playOnce = true,
                modifier = Modifier

            )
        }
    }

    DisposableEffect(context) {
        onDispose {
            tts.stop()
            tts.shutdown()
        }
    }
}
