package main

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import main.component.buttonComponent
import main.component.dropdownButtonComponent

var mostUsedWordList = mutableMapOf<String, Int>()

enum class Language {
    FRENCH, ENGLISH
}

@Composable
fun homePage(
    languageManager: LanguageManager,
    onProjectClick: () -> Unit,
    onCodeToVerifyClick: () -> Unit,
    onCompareClick: () -> Unit,
) {
    val noFileSnackbarVisibleState = remember { mutableStateOf(false) }
    var isCompareClicked by remember { mutableStateOf(false) }
    var selectedGlossary by remember { mutableStateOf<Glossary?>(null) }

    var showHelpPage by remember { mutableStateOf(false) }


    if (showHelpPage) {
        HelpPage {
            // Retour depuis la page d'aide
            showHelpPage = false
            print("Retour depuis la page d'aide")
        }
    } else {

        if (!isCompareClicked && selectedGlossary == null) {
            // Utilisez un Box pour placer le drapeau en haut à droite
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(16.dp),
                contentAlignment = Alignment.TopEnd
            ) {
                // Utilisez les images des drapeaux
                Image(
                    painter = painterResource(if (languageManager.currentLanguage == Language.FRENCH) "FR.png" else "EN.png"),
                    contentDescription = "Language Flag",
                    modifier = Modifier.size(48.dp)
                        .clickable { languageManager.changeLanguage() }
                )
            }

            Column(
                modifier = Modifier.fillMaxSize(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text("Quali'Nomme", style = MaterialTheme.typography.h3)

                Spacer(modifier = Modifier.height(16.dp))

                Column {
                    Row(
                        horizontalArrangement = Arrangement.spacedBy(16.dp)
                    ) {
                        buttonComponent(languageManager.getGlossaryText(), onProjectClick)

                        buttonComponent(languageManager.getCodeToVerifyText(), onCodeToVerifyClick)

                        Button(
                            onClick = {
                                if (mostUsedWordList.isEmpty()) {
                                    noFileSnackbarVisibleState.value = true
                                    return@Button
                                } else {
                                    onCompareClick()
                                }
                            },
                            modifier = Modifier.width(200.dp),
                            colors = ButtonDefaults.buttonColors(
                                backgroundColor = customRedColor,
                                contentColor = Color.White
                            ),
                            enabled = mostUsedWordList.isNotEmpty()
                        ) {
                            Text(languageManager.getCompareText())
                        }
                    }
                }

                if (noFileSnackbarVisibleState.value) {
                    Snackbar(
                        modifier = Modifier.padding(16.dp),
                        action = {
                            buttonComponent("OK", { noFileSnackbarVisibleState.value = false })
                        }
                    ) {
                        Text("Veuillez d'abord importer un fichier")
                    }
                }
            }

            // Example usage with text options
            dropdownButtonComponent(
                languageManager = languageManager,
                items = listOf(languageManager.getGlossaryText(), languageManager.getCodeToVerifyText(), languageManager.getCompareText())
            ) { selectedOption ->
                // Handle the selected option
                when (selectedOption) {
                    languageManager.getGlossaryText() -> onProjectClick()
                    languageManager.getCodeToVerifyText() -> onCodeToVerifyClick()
                    languageManager.getCompareText() -> {
                        if (mostUsedWordList.isEmpty()) {
                            noFileSnackbarVisibleState.value = true
                            return@dropdownButtonComponent
                        } else {
                            currentPage.value = "compareProject"
                        }
                    }
                }
            }
        }

        HelpButton {
            showHelpPage = true
        }

    }
}