package eu.gaudian.translator.view.translation

import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import eu.gaudian.translator.utils.TextToSpeechHelper
import eu.gaudian.translator.utils.findActivity
import eu.gaudian.translator.viewmodel.LanguageViewModel
import eu.gaudian.translator.viewmodel.SettingsViewModel
import eu.gaudian.translator.viewmodel.TranslationViewModel
import kotlinx.coroutines.launch


@Composable
fun TranslationScreen(
    navController: NavController,
) {
    val activity = LocalContext.current.findActivity()
    val languageViewModel: LanguageViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val translationViewModel: TranslationViewModel = hiltViewModel(viewModelStoreOwner = activity)
    var showHistorySheet by remember { mutableStateOf(false) }
    val sheetState = rememberModalBottomSheetState()

    val scope = rememberCoroutineScope()
    val context = LocalContext.current
    val settingsViewModel: SettingsViewModel = hiltViewModel(viewModelStoreOwner = activity)


    TranslationScreen(
        translationViewModel = translationViewModel,
        languageViewModel = languageViewModel,
        onHistoryClick = { showHistorySheet = true },
        onSettingsClick = {
            @Suppress("HardCodedStringLiteral")
            navController.navigate("custom_translation_prompt")
        },
        navController = navController as NavHostController
    )

    if (showHistorySheet) {
        ModalBottomSheet(
            onDismissRequest = {
                @Suppress("AssignedValueIsNeverRead")
                showHistorySheet = false
            },
            sheetState = sheetState
        ) {

            HistorySheetContent(
                translationViewModel = translationViewModel,
                onPlayAudio = { text, language ->
                    scope.launch {
                        val voice =
                            settingsViewModel.getTtsVoiceForLanguage(language.code, language.region)
                        TextToSpeechHelper.speakOut(
                            context = context,
                            text = text,
                            language = language,
                            voiceName = voice,
                        )
                    }
                },
                onItemClick = { item ->
                    translationViewModel.applyHistoryItem(item)
                    @Suppress("AssignedValueIsNeverRead")
                    showHistorySheet = false
                },
                languageViewModel = languageViewModel
            )
        }
    }

}

@Preview
@Composable
fun TranslationScreenPreview() {
    val navController = NavController(LocalContext.current)
    TranslationScreen(
        navController = navController,
    )
}