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

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.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.minimumInteractiveComponentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
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.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.textfield.LwaTextFieldUio
import com.pixelized.desktop.lwa.ui.screen.gamemaster.common.GMFilterHeader
import com.pixelized.desktop.lwa.ui.screen.gamemaster.common.tag.GMTagUio
import com.pixelized.desktop.lwa.ui.theme.lwa
import kotlinx.coroutines.flow.StateFlow
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.ic_close_24dp
import org.jetbrains.compose.resources.painterResource

@Stable
data class CharacterSheetAlterationDialogUio(
    val characterSheetId: String,
    val characterName: String,
    val filter: LwaTextFieldUio,
    val tags: StateFlow<List<GMTagUio>>,
    val alterations: List<AlterationToggleItemUio>,
)

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun CharacterSheetAlterationDialog(
    blur: BlurContentController? = LocalBlurController.current,
    dialog: State<CharacterSheetAlterationDialogUio?>,
    onTag: (String) -> Unit,
    onAlteration: (characterSheetId: String, alterationId: String, active: Boolean) -> Unit,
    onDismissRequest: () -> Unit,
) {
    dialog.value?.let {

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

        Dialog(
            properties = DialogProperties(
                usePlatformDefaultWidth = false,
                usePlatformInsets = false,
            ),
            onDismissRequest = onDismissRequest,
            content = {
                CharacterSheetAlterationContent(
                    dialog = it,
                    onTag = onTag,
                    onAlteration = onAlteration,
                    onDismissRequest = onDismissRequest,
                )
            }
        )
    }
}

@Composable
fun CharacterSheetAlterationContent(
    dialog: CharacterSheetAlterationDialogUio,
    onTag: (String) -> Unit,
    onAlteration: (characterSheetId: String, alterationId: String, active: Boolean) -> Unit,
    onDismissRequest: () -> Unit,
) {
    Surface(
        modifier = Modifier
            .fillMaxHeight()
            .width(width = 128.dp * 4)
            .padding(vertical = 16.dp),
    ) {
        Column(
            modifier = Modifier.fillMaxSize(),
        ) {
            Row(
                modifier = Modifier.padding(start = 16.dp).fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween,
            ) {
                Text(
                    style = MaterialTheme.typography.h6,
                    color = MaterialTheme.colors.onSurface,
                    text = dialog.characterName,
                )
                IconButton(
                    onClick = onDismissRequest,
                ) {
                    Icon(
                        painter = painterResource(Res.drawable.ic_close_24dp),
                        tint = MaterialTheme.lwa.colorScheme.base.primary,
                        contentDescription = null,
                    )
                }
            }
            GMFilterHeader(
                filter = dialog.filter,
                tags = dialog.tags.collectAsState(),
                onTag = onTag,
            )
            LazyColumn(
                modifier = Modifier
                    .fillMaxWidth()
                    .weight(weight = 1f),
                contentPadding = PaddingValues(start = 8.dp, end = 8.dp, bottom = 8.dp),
                verticalArrangement = Arrangement.spacedBy(space = 8.dp),
            ) {
                items(
                    items = dialog.alterations,
                    key = { it.id }
                ) { alteration ->
                    AlterationToggleItem(
                        modifier = Modifier
                            .animateItem()
                            .background(
                                color = MaterialTheme.lwa.colorScheme.elevated.base1dp,
                                shape = MaterialTheme.lwa.shapes.base.small,
                            )
                            .minimumInteractiveComponentSize()
                            .padding(horizontal = 8.dp),
                        alteration = alteration,
                        onAlteration = {
                            onAlteration(
                                dialog.characterSheetId,
                                alteration.id,
                                alteration.active,
                            )
                        },
                        onTag = onTag,
                    )
                }
            }
        }
    }
}