package com.pixelized.desktop.lwa.ui.composable.character.diminished

import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.pixelized.desktop.lwa.ui.composable.decoratedBox.DecoratedBox
import com.pixelized.desktop.lwa.utils.extention.onPreviewEscape
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.dialog__cancel_action
import lwacharactersheet.composeapp.generated.resources.dialog__confirm_action
import org.jetbrains.compose.resources.stringResource

@Stable
data class CharacterSheetDiminishedDialogUio(
    val characterSheetId: String,
    val label: String,
    val value: () -> TextFieldValue,
    val onValueChange: (TextFieldValue) -> Unit,
    val additional: String?,
)

@Composable
fun CharacterSheetDiminishedDialog(
    dialog: State<CharacterSheetDiminishedDialogUio?>,
    onConfirm: (CharacterSheetDiminishedDialogUio) -> Unit,
    onDismissRequest: () -> Unit,
) {
    dialog.value?.let {
        Dialog(
            onDismissRequest = onDismissRequest,
            content = {
                CharacterSheetDiminishedContent(
                    dialog = it,
                    onConfirm = onConfirm,
                    onDismissRequest = onDismissRequest,
                )
            }
        )
    }
}

@Composable
private fun CharacterSheetDiminishedContent(
    dialog: CharacterSheetDiminishedDialogUio,
    onConfirm: (CharacterSheetDiminishedDialogUio) -> Unit,
    onDismissRequest: () -> Unit,
) {
    val typography = MaterialTheme.typography
    val colors = MaterialTheme.colors

    val valueTypography = remember {
        typography.h5.copy(
            color = colors.primary,
            textAlign = TextAlign.Center,
            fontWeight = FontWeight.Bold
        )
    }

    val focusRequester = remember { FocusRequester() }
    LaunchedEffect(Unit) {
        focusRequester.requestFocus()
    }

    Box(
        modifier = Modifier
            .clickable(
                interactionSource = remember { MutableInteractionSource() },
                indication = null,
                onClick = onDismissRequest,
            )
            .onPreviewEscape(
                escape = onDismissRequest,
                enter = { onConfirm(dialog) },
            )
            .fillMaxSize()
            .padding(all = 32.dp),
        contentAlignment = Alignment.Center,
    ) {
        DecoratedBox {
            Surface {
                Column(
                    verticalArrangement = Arrangement.spacedBy(space = 8.dp),
                    horizontalAlignment = Alignment.CenterHorizontally,
                ) {
                    Text(
                        modifier = Modifier.padding(start = 24.dp, end = 24.dp, top = 16.dp),
                        style = MaterialTheme.typography.caption,
                        text = dialog.label,
                    )
                    Row(
                        horizontalArrangement = Arrangement.spacedBy(
                            space = 8.dp,
                            alignment = Alignment.CenterHorizontally
                        )
                    ) {
                        BasicTextField(
                            modifier = Modifier
                                .focusRequester(focusRequester = focusRequester)
                                .width(width = 120.dp),
                            textStyle = valueTypography,
                            cursorBrush = SolidColor(MaterialTheme.colors.primary),
                            singleLine = true,
                            keyboardActions = KeyboardActions { onConfirm(dialog) },
                            keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
                            value = dialog.value(),
                            onValueChange = dialog.onValueChange,
                        )
                        dialog.additional?.let {
                            Text(
                                modifier = Modifier.alpha(alpha = 0.66f),
                                style = valueTypography,
                                text = it
                            )
                        }
                    }
                    Row(
                        modifier = Modifier
                            .padding(top = 4.dp)
                            .padding(horizontal = 16.dp)
                            .align(alignment = Alignment.End),
                        horizontalArrangement = Arrangement.spacedBy(
                            space = 4.dp,
                            alignment = Alignment.End
                        )
                    ) {
                        TextButton(
                            onClick = onDismissRequest,
                        ) {
                            Text(
                                color = MaterialTheme.colors.primary.copy(alpha = .7f),
                                text = stringResource(Res.string.dialog__cancel_action)
                            )
                        }
                        TextButton(
                            onClick = { onConfirm(dialog) },
                        ) {
                            Text(
                                text = stringResource(Res.string.dialog__confirm_action)
                            )
                        }
                    }
                }
            }
        }
    }
}