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 AddCustomLanguageDialog(
    showDialog: Boolean,
    onDismiss: () -> Unit,
    onAddLanguage: (Language) -> Unit
) {
    if (showDialog) {
        var languageCode by remember { mutableStateOf("") }
        var languageRegion by remember { mutableStateOf("") }
        var languageName by remember { mutableStateOf("") }
        var englishName by remember { mutableStateOf("") }

        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.text_add_custom_language),
                        style = MaterialTheme.typography.titleLarge,
                        color = MaterialTheme.colorScheme.onSurface,
                        modifier = Modifier.padding(bottom = 8.dp)
                    )

                    AppOutlinedTextField(
                        value = languageCode,
                        onValueChange = { languageCode = it },
                        label = { Text(text = stringResource(R.string.text_language_code)) },
                        placeholder = { Text(text = stringResource(R.string.text_e_g_en)) },
                        modifier = Modifier.fillMaxWidth(),
                        singleLine = true
                    )
                    AppOutlinedTextField(
                        value = languageRegion,
                        onValueChange = { languageRegion = it },
                        label = { Text(text = stringResource(R.string.text_region)) },
                        placeholder = { Text(text = stringResource(R.string.text_e_g_us)) },
                        modifier = Modifier.fillMaxWidth(),
                        singleLine = true
                    )
                    AppOutlinedTextField(
                        value = languageName,
                        onValueChange = { languageName = it },
                        label = { Text(text = stringResource(R.string.text_name_of_the_language)) },
                        placeholder = { Text(text = stringResource(R.string.text_e_g_english)) },
                        modifier = Modifier.fillMaxWidth(),
                        singleLine = true
                    )
                    AppOutlinedTextField(
                        value = englishName,
                        onValueChange = { englishName = it },
                        label = { Text(text = stringResource(R.string.name_in_english)) },
                        placeholder = { Text(text = stringResource(R.string.text_e_g_english)) },
                        modifier = Modifier.fillMaxWidth(),
                        singleLine = true
                    )

                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 16.dp),
                        horizontalArrangement = Arrangement.End,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        TextButton(onClick = onDismiss) {
                            Text(stringResource(R.string.label_cancel))
                        }
                        DialogButton(
                            onClick = {
                                val customLanguage = Language(
                                    code = languageCode.lowercase(),
                                    region = languageRegion,
                                    nameResId = -1,
                                    name = languageName,
                                    englishName = englishName,
                                    isCustom = true,
                                    isSelected = true,
                                )
                                onAddLanguage(customLanguage)
                                onDismiss()
                            },
                            enabled = languageCode.isNotBlank() && languageName.isNotBlank() && languageRegion.isNotBlank(),
                            content = {
                                Text(text = stringResource(R.string.label_add))
                            }
                        )
                    }
                }
            }
        }
    }
}