package main

import androidx.compose.foundation.VerticalScrollbar
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import main.component.buttonComponent
import main.component.dropdownButtonComponent

@Composable
fun glossaryList(
    languageManager: LanguageManager,
    glossaries: List<Glossary>,
    onGlossarySelected: (Glossary) -> Unit,
    onBackClick: () -> Unit,
    onProjectClick: () -> Unit,
    onCodeToVerifyClick: () -> Unit
) {

    var isCompareClicked by remember { mutableStateOf(false) }
    var selectedGlossary by remember { mutableStateOf<Glossary?>(null) }
    val noFileSnackbarVisibleState = remember { mutableStateOf(false) }

    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text("Sélectionnez un glossaire", style = MaterialTheme.typography.h5)

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

        Box(
            modifier = Modifier
                .height(500.dp)
                .width(250.dp)
                .padding(10.dp)
        ) {
            val scrollState = rememberLazyListState()
            LazyColumn(
                state = scrollState,
                modifier = Modifier
                    .padding(10.dp)
                    .width(200.dp),
                verticalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                if (glossaries.isEmpty()) {
                    item {
                        Text("Aucun glossaire disponible")
                    }
                } else {
                    items(glossaries) { glossary ->
                        Row(
                            modifier = Modifier.width(200.dp).fillMaxWidth(),
                            horizontalArrangement = Arrangement.SpaceBetween,
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            buttonComponent(
                                glossary.name,
                                {
                                    onGlossarySelected(glossary)
                                    currentPage.value = "compareTable"
                                }
                            )
                        }
                    }
                }
            }
            VerticalScrollbar(
                modifier = Modifier.align(Alignment.CenterEnd).fillMaxHeight(),
                adapter = rememberScrollbarAdapter(scrollState)
            )
            Spacer(modifier = Modifier.height(6.dp))
        }
        Spacer(modifier = Modifier.height(16.dp))

        buttonComponent("Retour", onBackClick)
    }

    dropdownButtonComponent(
        languageManager,
        items = listOf("Glossaire", "Code à vérifier", "Comparer")
    ) { selectedOption ->
        // Handle the selected option
        when (selectedOption) {
            "Glossaire" -> onProjectClick()
            "Code à vérifier" -> onCodeToVerifyClick()
            "Comparer" -> {
                if (mostUsedWordList.isEmpty()) {
                    noFileSnackbarVisibleState.value = true
                    return@dropdownButtonComponent
                } else {
                    currentPage.value = "compareProject"
                }
            }
        }
    }

    when {
        isCompareClicked && selectedGlossary == null -> {
            currentPage.value = "compareGlossary"
        }
    }
}