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.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.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)

    AppDialog(
        onDismissRequest = onDismissRequest,
        title = {
            Text(text = stringResource(R.string.text_select_categories))
        }
    ) {
        // Dropdown button and menu
        CategoryDropdown(
            onCategorySelected = onCategorySelected,
            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 = {
                    // The selected categories are handled by CategoryDropdown's internal state
                    // and passed to onCategorySelected callback
                    onDismissRequest()
                },
                enabled = true // Always enabled since CategoryDropdown handles validation
            ) {
                Text(stringResource(R.string.label_confirm))
            }
        }
    }
}
