package com.pixelized.desktop.lwa.ui.screen.admin.character.edit.dialog

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.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
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.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.composable.character.LwaDialog
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.color.component.LwaTextFieldColors
import kotlinx.coroutines.flow.StateFlow
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 GMCharacterSheetCopyDialogUio(
    val id: LwaTextFieldUio,
    val name: LwaTextFieldUio,
    val enableConfirm: StateFlow<Boolean>,
)

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

    @Stable
    val spacings = 8.dp
}

@Composable
fun GMCharacterSheetCopyDialog(
    dialog: State<GMCharacterSheetCopyDialogUio?>,
    paddings: PaddingValues = GMCharacterSheetCopyDialogDefault.paddings,
    spacings: Dp = GMCharacterSheetCopyDialogDefault.spacings,
    onDismissRequest: () -> Unit,
    onConfirm: (GMCharacterSheetCopyDialogUio) -> Unit,
) {
    LwaDialog(
        state = dialog,
        onDismissRequest = onDismissRequest,
        onConfirm = { dialog.value?.let(onConfirm) },
    ) {
        GMCharacterSheetCopyContent(
            paddings = paddings,
            spacings = spacings,
            dialog = it,
            onDismissRequest = onDismissRequest,
            onConfirm = onConfirm,
        )
    }
}

@Composable
private fun GMCharacterSheetCopyContent(
    modifier: Modifier = Modifier,
    paddings: PaddingValues = GMCharacterSheetCopyDialogDefault.paddings,
    spacings: Dp = GMCharacterSheetCopyDialogDefault.spacings,
    dialog: GMCharacterSheetCopyDialogUio,
    onDismissRequest: () -> Unit,
    onConfirm: (GMCharacterSheetCopyDialogUio) -> Unit,
) {
    Column(
        modifier = Modifier
            .padding(paddingValues = paddings)
            .then(other = modifier)
    ) {
        LwaTextField(
            modifier = Modifier.fillMaxWidth(),
            colors = LwaTextFieldColors(backgroundColor = Color.Transparent),
            field = dialog.id,
        )
        LwaTextField(
            modifier = Modifier.fillMaxWidth(),
            colors = LwaTextFieldColors(backgroundColor = Color.Transparent),
            field = dialog.name,
        )
        Spacer(
            modifier = Modifier.height(height = spacings)
        )
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.End,
        ) {
            TextButton(
                onClick = onDismissRequest,
            ) {
                Text(
                    color = MaterialTheme.colors.primary.copy(alpha = .7f),
                    text = stringResource(Res.string.dialog__cancel_action)
                )
            }
            TextButton(
                enabled = dialog.enableConfirm.collectAsState().value,
                onClick = { onConfirm(dialog) },
            ) {
                Text(
                    text = stringResource(Res.string.dialog__confirm_action)
                )
            }
        }
    }
}