package eu.gaudian.translator.view.dialogs

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
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.res.stringResource
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.R
import eu.gaudian.translator.model.Language
import eu.gaudian.translator.view.composable.AppDialog
import eu.gaudian.translator.view.composable.AppOutlinedTextField
import eu.gaudian.translator.view.composable.DialogButton

@Composable
fun EditLanguageDialog(
    language: Language,
    onDismiss: () -> Unit,
    onSave: (name: String, code: String, region: String) -> Unit
) {
    var code by remember(language) { mutableStateOf(language.code) }
    var region by remember(language) { mutableStateOf(language.region) }
    var name by remember(language) { mutableStateOf(language.name) }

    AppDialog(onDismissRequest = onDismiss) {
        Card(
            modifier = Modifier.fillMaxWidth(),
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.surface
            )
        ) {
            Column(
                modifier = Modifier.padding(24.dp),
                verticalArrangement = Arrangement.spacedBy(16.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = stringResource(R.string.edit),
                    style = MaterialTheme.typography.titleLarge,
                    color = MaterialTheme.colorScheme.onSurface
                )

                // Language name: editable only for custom languages
                AppOutlinedTextField(
                    value = name,
                    onValueChange = { if (language.isCustom == true) name = it },
                    enabled = language.isCustom == true,
                    label = { Text(text = stringResource(R.string.text_name_of_the_language)) },
                    modifier = Modifier.fillMaxWidth()
                )

                AppOutlinedTextField(
                    value = code,
                    onValueChange = { code = it },
                    label = { Text(text = stringResource(R.string.text_language_code)) },
                    placeholder = { Text(text = stringResource(R.string.text_e_g_en)) },
                    modifier = Modifier.fillMaxWidth()
                )
                AppOutlinedTextField(
                    value = region,
                    onValueChange = { region = it },
                    label = { Text(text = stringResource(R.string.text_region)) },
                    placeholder = { Text(text = stringResource(R.string.text_e_g_us)) },
                    modifier = Modifier.fillMaxWidth()
                )

                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 8.dp),
                    horizontalArrangement = Arrangement.End
                ) {
                    TextButton(onClick = onDismiss) {
                        Text(stringResource(R.string.label_cancel))
                    }
                    DialogButton(
                        onClick = { onSave(name, code, region) },
                        enabled = name.isNotBlank() && code.isNotBlank() && region.isNotBlank(),
                        modifier = Modifier.padding(start = 8.dp)
                    ) {
                        Text(text = stringResource(R.string.label_save))
                    }
                }
            }
        }
    }
}