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.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.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.window.DialogProperties
import androidx.compose.ui.zIndex
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.pixelized.desktop.lwa.ui.composable.character.LwaDialog
import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextFieldUio
import com.pixelized.desktop.lwa.ui.screen.admin.common.GMFilterHeader
import com.pixelized.desktop.lwa.ui.screen.admin.common.tag.GMTagUio
import com.pixelized.desktop.lwa.ui.theme.lwa
import com.pixelized.desktop.lwa.utils.extention.calculatePaddings
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(
    dialog: State<CharacterSheetAlterationDialogUio?>,
    onTag: (String) -> Unit,
    onAlteration: (characterSheetId: String, alterationId: String, active: Boolean) -> Unit,
    onDismissRequest: () -> Unit,
) {
    LwaDialog(
        properties = DialogProperties(
            usePlatformDefaultWidth = false,
            usePlatformInsets = false,
        ),
        state = dialog,
        onDismissRequest = onDismissRequest,
        onConfirm = onDismissRequest,
        content = {
            CharacterSheetAlterationContent(
                dialog = it,
                onTag = onTag,
                onAlteration = onAlteration,
                onDismissRequest = onDismissRequest,
            )
        }
    )
}

@Composable
fun CharacterSheetAlterationContent(
    dialog: CharacterSheetAlterationDialogUio,
    paddingValues: PaddingValues = MaterialTheme.lwa.dimen.paddingValues,
    onTag: (String) -> Unit,
    onAlteration: (characterSheetId: String, alterationId: String, active: Boolean) -> Unit,
    onDismissRequest: () -> Unit,
) {
    val padding = MaterialTheme.lwa.dimen.paddingValue
    val (start, _, end, _) = paddingValues.calculatePaddings()

    Column(
        modifier = Modifier
            .fillMaxHeight()
            .width(width = 128.dp * 4)
    ) {
        // Add a surface and a zIndex() to avoid the animation coming from : .animateItem() (see below)
        // to draw over this part of the dialog.
        Surface(
            modifier = Modifier.zIndex(1f)
        ) {
            Column {
                Row(
                    modifier = Modifier
                        .padding(start = start)
                        .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,
                    padding = padding,
                    tags = dialog.tags.collectAsStateWithLifecycle(),
                    onTag = onTag,
                )
            }
        }
        LazyColumn(
            modifier = Modifier
                .fillMaxWidth()
                .weight(weight = 1f, fill = true),
            contentPadding = paddingValues,
            verticalArrangement = Arrangement.spacedBy(space = padding),
        ) {
            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(start = start, end = end),
                    alteration = alteration,
                    onAlteration = {
                        onAlteration(
                            dialog.characterSheetId,
                            alteration.id,
                            alteration.active,
                        )
                    },
                    onTag = onTag,
                )
            }
        }
    }
}