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

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.MaterialTheme
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.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.type
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.composable.button.SignButton
import com.pixelized.desktop.lwa.ui.composable.character.LwaDialog
import com.pixelized.desktop.lwa.ui.composable.checkbox.LwaCheckBox
import com.pixelized.desktop.lwa.ui.composable.checkbox.LwaCheckBoxUio
import com.pixelized.desktop.lwa.ui.composable.key.KeyHandler
import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextField
import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextFieldUio
import com.pixelized.desktop.lwa.ui.theme.lwa
import kotlinx.coroutines.flow.StateFlow
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.character_sheet_edit__use_armor
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 CharacterSheetCharacteristicDialogUio(
    val characterSheetId: String,
    val characteristic: Characteristic,
    val label: String,
    val add: StateFlow<Boolean>,
    val value: LwaTextFieldUio,
    val enableArmor: LwaCheckBoxUio?,
    val enableConfirm: StateFlow<Boolean>,
) {
    @Stable
    enum class Characteristic {
        Damage,
        Fatigue,
    }
}

@Stable
object CharacterSheetCharacteristicDialogDefault {
    @Stable
    val paddings: PaddingValues =
        PaddingValues(top = 16.dp, start = 8.dp, end = 8.dp, bottom = 8.dp)

    @Stable
    val spacings: DpSize = DpSize(width = 4.dp, height = 8.dp)
}

@Composable
fun CharacterSheetCharacteristicDialog(
    dialog: State<CharacterSheetCharacteristicDialogUio?>,
    paddings: PaddingValues = CharacterSheetCharacteristicDialogDefault.paddings,
    spacings: DpSize = CharacterSheetCharacteristicDialogDefault.spacings,
    onConfirm: (CharacterSheetCharacteristicDialogUio) -> Unit,
    onSwapSign: (CharacterSheetCharacteristicDialogUio) -> Unit,
    onDismissRequest: () -> Unit,
) {
    LwaDialog(
        state = dialog,
        onDismissRequest = onDismissRequest,
        onConfirm = { dialog.value?.let(onConfirm) }
    ) { state ->
        val focusRequester = remember { FocusRequester() }
        LaunchedEffect(Unit) { focusRequester.requestFocus() }

        CharacterSheetCharacteristicDialogKeyHandler(
            onSwap = { onSwapSign(state) },
        )

        Column(
            modifier = Modifier.padding(paddingValues = paddings),
            verticalArrangement = Arrangement.spacedBy(space = spacings.height),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Text(
                style = MaterialTheme.typography.caption,
                text = state.label,
            )

            Row(
                horizontalArrangement = Arrangement.spacedBy(space = spacings.width),
                verticalAlignment = Alignment.Bottom,
            ) {
                SignButton(
                    modifier = Modifier
                        .size(size = 56.dp)
                        .background(
                            color = MaterialTheme.lwa.colorScheme.elevated.base1dp,
                            shape = MaterialTheme.shapes.small,
                        ),
                    add = state.add,
                    onClick = { onSwapSign(state) },
                )
                LwaTextField(
                    modifier = Modifier
                        .focusRequester(focusRequester = focusRequester)
                        .width(192.dp),
                    field = state.value,
                )
            }
            state.enableArmor?.let {
                Row (
                    modifier = Modifier.align(alignment = Alignment.End),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.spacedBy(
                        space = spacings.width,
                        alignment = Alignment.End
                    ),
                ) {
                    Text(
                        style = MaterialTheme.typography.caption,
                        text = stringResource(Res.string.character_sheet_edit__use_armor),
                    )
                    LwaCheckBox(
                        field = it,
                    )
                }
            }
            Row(
                modifier = Modifier.align(alignment = Alignment.End),
                horizontalArrangement = Arrangement.spacedBy(
                    space = spacings.width,
                    alignment = Alignment.End
                )
            ) {
                TextButton(
                    onClick = onDismissRequest,
                ) {
                    Text(
                        color = MaterialTheme.colors.primary.copy(alpha = .7f),
                        text = stringResource(Res.string.dialog__cancel_action)
                    )
                }
                TextButton(
                    enabled = state.enableConfirm.collectAsState().value,
                    onClick = { onConfirm(state) },
                ) {
                    Text(
                        text = stringResource(Res.string.dialog__confirm_action)
                    )
                }
            }
        }
    }
}


@Composable
private fun CharacterSheetCharacteristicDialogKeyHandler(
    onSwap: () -> Unit,
) {
    KeyHandler {
        if (it.type == KeyEventType.KeyDown) {
            when (it.key) {
                Key.AltLeft, Key.AltRight -> {
                    onSwap()
                    true
                }

                else -> false
            }
        } else {
            false
        }
    }
}