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

import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
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.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.pixelized.desktop.lwa.LocalBlurController
import com.pixelized.desktop.lwa.ui.composable.blur.BlurContentController
import com.pixelized.desktop.lwa.ui.composable.decoratedBox.DecoratedBox
import com.pixelized.desktop.lwa.utils.extention.onPreviewEscape

@Stable
object LwaDialogDefault {
    val paddings = PaddingValues(vertical = 32.dp)
}

@Composable
fun <T> LwaDialog(
    modifier: Modifier = Modifier,
    properties: DialogProperties = DialogProperties(),
    blur: BlurContentController? = LocalBlurController.current,
    paddings: PaddingValues = LwaDialogDefault.paddings,
    color: Color = MaterialTheme.colors.surface,
    state: State<T?>,
    onDismissRequest: () -> Unit,
    onConfirm: () -> Unit,
    content: @Composable BoxScope.(T) -> Unit,
) {
    state.value?.let { dialog ->

        blur?.let {
            DisposableEffect("LwaDialog") {
                blur.show()
                onDispose {
                    blur.hide()
                }
            }
        }

        Dialog(
            properties = properties,
            onDismissRequest = onDismissRequest,
            content = {
                Box(
                    modifier = Modifier
                        .clickable(
                            interactionSource = remember { MutableInteractionSource() },
                            indication = null,
                            onClick = onDismissRequest,
                        )
                        .onPreviewEscape(
                            escape = onDismissRequest,
                            enter = onConfirm,
                        )
                        .fillMaxSize()
                        .padding(paddingValues = paddings)
                        .then(other = modifier),
                    contentAlignment = Alignment.Center,
                ) {
                    DecoratedBox(
                        modifier = Modifier.clickable(
                            interactionSource = remember { MutableInteractionSource() },
                            indication = null,
                            onClick = { },
                        )
                    ) {
                        Surface(
                            color = color,
                        ) {
                            Box {
                                content(dialog)
                            }
                        }
                    }
                }
            }
        )
    }
}