package eu.gaudian.translator.view.dialogs

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
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.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import eu.gaudian.translator.R
import eu.gaudian.translator.model.TagCategory
import eu.gaudian.translator.model.VocabularyCategory
import eu.gaudian.translator.utils.findActivity
import eu.gaudian.translator.view.composable.AppDialog
import eu.gaudian.translator.view.composable.DialogButton
import eu.gaudian.translator.viewmodel.CategoryViewModel

@Composable
fun CategorySelectionDialog(
    onCategorySelected: (List<VocabularyCategory?>) -> Unit,
    onDismissRequest: () -> Unit,
) {
    val activity = LocalContext.current.findActivity()
    val categoryViewModel: CategoryViewModel = hiltViewModel(viewModelStoreOwner = activity)
    
    val categories by categoryViewModel.categories.collectAsState(initial = emptyList())
    var selectedCategories by remember { mutableStateOf<List<VocabularyCategory?>>(emptyList()) }
    var newCategoryName by remember { mutableStateOf("") }
    var expanded by remember { mutableStateOf(false) }

    AppDialog(
        onDismissRequest = onDismissRequest,
        title = {
            Text(text = stringResource(R.string.text_select_categories))
        }
    ) {
        // Dropdown button and menu
        CategoryDropdownContent(
            state = CategoryDropdownState(
                expanded = expanded,
                selectedCategories = selectedCategories,
                newCategoryName = newCategoryName,
                categories = categories,
            ),
            onExpand = { isExpanded -> expanded = isExpanded },
            onCategorySelected = { selectedCategories = it },
            onNewCategoryNameChange = { newCategoryName = it },
            onAddCategory = { name ->
                val newCategory = TagCategory(id = 0, name = name.trim())
                categoryViewModel.createCategory(newCategory)
                newCategoryName = ""
            },
            noneSelectable = false,
            multipleSelectable = true,
            onlyLists = true,
            addCategory = true,
            modifier = Modifier.fillMaxWidth(),
        )

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 24.dp),
            horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.End)
        ) {
            DialogButton(onClick = onDismissRequest) {
                Text(stringResource(R.string.label_cancel))
            }

            DialogButton(
                onClick = {
                    onCategorySelected(selectedCategories)
                    onDismissRequest()
                },
                enabled = selectedCategories.isNotEmpty()
            ) {
                Text(stringResource(R.string.label_confirm))
            }
        }
    }
}
