@file:Suppress("AssignedValueIsNeverRead")

package eu.gaudian.translator.view.dialogs

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.R
import eu.gaudian.translator.model.VocabularyStage
import eu.gaudian.translator.view.composable.AppCheckbox
import eu.gaudian.translator.view.composable.AppDropdownContainer
import eu.gaudian.translator.view.composable.AppDropdownMenuItem


@Composable
fun VocabularyStageDropDown(
    preselectedStages: List<VocabularyStage?>,
    onStageSelected: (List<VocabularyStage?>) -> Unit,
    modifier: Modifier = Modifier,
    noneSelectable: Boolean? = true,
    multipleSelectable: Boolean = false,
) {
    var expanded by remember { mutableStateOf(false) }
    var selectedStages by remember { mutableStateOf(preselectedStages) }
    val context = LocalContext.current

    val buttonText = when {
        selectedStages.isEmpty() -> stringResource(R.string.label_select_stage)
        selectedStages.size == 1 -> selectedStages.first()?.toString(context) ?: stringResource(R.string.text_none)
        else -> stringResource(R.string.stages_selected, selectedStages.size)
    }

    AppDropdownContainer(
        expanded = expanded,
        onDismissRequest = { expanded = false },
        onExpandRequest = { expanded = true },
        buttonText = buttonText,
        modifier = modifier,
        showDoneButton = multipleSelectable,
        onDoneClick = { expanded = false }
    ) {
        if (noneSelectable == true) {
            val noneSelected = selectedStages.contains(null)
            AppDropdownMenuItem(
                text = {
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        if (multipleSelectable) {
                            AppCheckbox(
                                checked = noneSelected,
                                onCheckedChange = {
                                    selectedStages = if (noneSelected) {
                                        selectedStages.filterNotNull()
                                    } else {
                                        selectedStages + listOf(null)
                                    }
                                    onStageSelected(selectedStages)
                                }
                            )
                            Spacer(modifier = Modifier.width(8.dp))
                        }
                        Text(text = stringResource(R.string.text_none))
                    }
                },
                onClick = {
                    if (multipleSelectable) {
                        selectedStages = if (null in selectedStages) {
                            selectedStages.filterNotNull()
                        } else {
                            selectedStages + listOf(null)
                        }
                        onStageSelected(selectedStages)
                    } else {
                        selectedStages = listOf(null)
                        onStageSelected(selectedStages)
                        expanded = false
                    }
                }
            )
        }

        VocabularyStage.entries.forEach { stage ->
            val isSelected = selectedStages.contains(stage)
            AppDropdownMenuItem(
                text = {
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        if (multipleSelectable) {
                            AppCheckbox(
                                checked = isSelected,
                                onCheckedChange = {
                                    selectedStages = if (isSelected) {
                                        selectedStages - stage
                                    } else {
                                        selectedStages + stage
                                    }
                                    onStageSelected(selectedStages)
                                }
                            )
                            Spacer(modifier = Modifier.width(8.dp))
                        }
                        Text(stage.toString(context))
                    }
                },
                onClick = {
                    if (multipleSelectable) {
                        selectedStages = if (stage in selectedStages) {
                            selectedStages - stage
                        } else {
                            selectedStages + stage
                        }
                        onStageSelected(selectedStages)
                    } else {
                        selectedStages = listOf(stage)
                        onStageSelected(selectedStages)
                        expanded = false
                    }
                }
            )
        }
    }
}

@Preview
@Composable
fun VocabularyStageDropDownPreview() {
    VocabularyStageDropDown(
        preselectedStages = listOf(VocabularyStage.NEW),
        onStageSelected = {}
    )
}
