package com.pixelized.desktop.lwa.ui.composable.confirmation

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.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.LocalBlurController
import com.pixelized.desktop.lwa.ui.composable.character.LwaDialog
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 ConfirmationDialogUio(
    val title: String,
    val description: String,
    val onConfirmRequest: () -> Unit,
    val onDismissRequest: () -> Unit,
)

@Stable
object ConfirmationDialogDefault {

    @Stable
    val paddings = PaddingValues(start = 16.dp, top = 16.dp, end = 16.dp)

    @Stable
    val spacings: Dp = 8.dp
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ConfirmationDialog(
    modifier: Modifier = Modifier,
    paddingValues: PaddingValues = ConfirmationDialogDefault.paddings,
    spacing: Dp = ConfirmationDialogDefault.spacings,
    dialog: State<ConfirmationDialogUio?>,
) {
    LwaDialog(
        modifier = modifier,
        blur = LocalBlurController.current,
        state = dialog,
        onDismissRequest = { dialog.value?.onDismissRequest?.invoke() },
        onConfirm = { dialog.value?.onConfirmRequest?.invoke() },
    ) {
        Column(
            modifier = Modifier.padding(paddingValues = paddingValues),
            verticalArrangement = Arrangement.spacedBy(space = spacing),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Text(
                style = MaterialTheme.typography.caption,
                text = it.title,
            )
            Text(
                style = MaterialTheme.typography.body1,
                text = it.description,
            )
            Row(
                modifier = Modifier.align(alignment = Alignment.End),
                horizontalArrangement = Arrangement.spacedBy(
                    space = spacing / 2,
                    alignment = Alignment.End,
                ),
            ) {
                TextButton(
                    onClick = it.onDismissRequest,
                ) {
                    Text(
                        color = MaterialTheme.colors.primaryVariant,
                        text = stringResource(Res.string.dialog__cancel_action)
                    )
                }
                TextButton(
                    onClick = it.onConfirmRequest,
                ) {
                    Text(
                        color = MaterialTheme.colors.primary,
                        text = stringResource(Res.string.dialog__confirm_action)
                    )
                }
            }
        }
    }
}