package com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.header

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.SizeTransform
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
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.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
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.draw.clip
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.composable.shapes.ArrowShape
import com.pixelized.desktop.lwa.ui.composable.tooltip.BasicTooltipLayout
import com.pixelized.desktop.lwa.ui.composable.tooltip.BasicTooltipUio
import com.pixelized.desktop.lwa.ui.overlay.roll.RollAction
import com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.DetailPanelUio
import com.pixelized.desktop.lwa.ui.theme.lwa
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.character_sheet__level
import lwacharactersheet.composeapp.generated.resources.ic_azm_24dp
import lwacharactersheet.composeapp.generated.resources.ic_blur_on_24dp
import lwacharactersheet.composeapp.generated.resources.ic_close_24dp
import lwacharactersheet.composeapp.generated.resources.ic_cognition_24dp
import lwacharactersheet.composeapp.generated.resources.ic_description_24dp
import lwacharactersheet.composeapp.generated.resources.ic_directions_run_24dp
import lwacharactersheet.composeapp.generated.resources.ic_heart_24dp
import lwacharactersheet.composeapp.generated.resources.ic_heart_plus_24dp
import lwacharactersheet.composeapp.generated.resources.ic_money_bag_24dp
import lwacharactersheet.composeapp.generated.resources.ic_pan_tool_24dp
import lwacharactersheet.composeapp.generated.resources.ic_shield_24dp
import lwacharactersheet.composeapp.generated.resources.ic_skull_24dp
import lwacharactersheet.composeapp.generated.resources.ic_swords_24dp
import lwacharactersheet.composeapp.generated.resources.ic_water_drop_24dp
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource

@Stable
data class CharacterDetailHeaderUio(
    val characterSheetId: String,
    val panel: DetailPanelUio,
    val portrait: String?,
    val diminished: Int,
    val alteration: Boolean,
    val levelUp: Boolean,
    val name: String,
    val level: Int,
    val hp: String,
    val hpTooltip: BasicTooltipUio,
    val maxHp: String,
    val pp: String,
    val ppTooltip: BasicTooltipUio,
    val maxPp: String,
    val mov: String,
    val movTooltip: BasicTooltipUio,
    val armor: String,
    val armorTooltip: BasicTooltipUio,
    val bonus: String,
    val bonusTooltip: BasicTooltipUio,
    val grow: String,
    val growTooltip: BasicTooltipUio,
    val learn: String,
    val learnTooltip: BasicTooltipUio,
    val reflex: String,
    val reflexTooltip: BasicTooltipUio,
    val reflexRoll: RollAction.Uio,
    val initiative: String,
    val initiativeTooltip: BasicTooltipUio,
)

@Stable
object CharacterDetailHeaderDefault {
    @Stable
    val paddings = PaddingValues(start = 16.dp, end = 16.dp, bottom = 8.dp)
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CharacterDetailHeader(
    modifier: Modifier = Modifier,
    paddings: PaddingValues = CharacterDetailHeaderDefault.paddings,
    iconSize: Dp = MaterialTheme.lwa.dimen.sheet.subCategory,
    header: State<CharacterDetailHeaderUio?>,
    onDismissRequest: () -> Unit,
    onLevelUp: () -> Unit,
    onSheet: () -> Unit,
    onInventory: () -> Unit,
    onAlteration: () -> Unit,
    onDiminished: () -> Unit,
    onHp: () -> Unit,
    onPp: () -> Unit,
    onReflex: (RollAction.Uio) -> Unit,
) {
    val layoutDirection = LocalLayoutDirection.current
    val startPadding = remember(layoutDirection) { paddings.calculateStartPadding(layoutDirection) }
    val endPadding = remember(layoutDirection) { paddings.calculateEndPadding(layoutDirection) }
    val bottomPadding = remember { paddings.calculateBottomPadding() }

    Column(
        modifier = modifier,
    ) {
        Row(
            modifier = Modifier.padding(start = startPadding),
        ) {
            Row(
                modifier = Modifier
                    .weight(1f)
                    .align(alignment = Alignment.CenterVertically),
                horizontalArrangement = Arrangement.spacedBy(space = 4.dp),
            ) {
                Text(
                    modifier = Modifier.alignByBaseline(),
                    style = MaterialTheme.typography.h5,
                    text = header.value?.name ?: "",
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                )
                Text(
                    modifier = Modifier.alignByBaseline(),
                    style = MaterialTheme.lwa.typography.system.caption,
                    text = stringResource(
                        resource = Res.string.character_sheet__level,
                        header.value?.level ?: 0
                    ),
                )
            }
            AnimatedVisibility(
                visible = header.value?.levelUp == true,
                enter = fadeIn(),
                exit = fadeOut(),
            ) {
                IconButton(
                    onClick = onLevelUp,
                ) {
                    ArrowShape(
                        color = MaterialTheme.lwa.colorScheme.portrait.levelUp,
                    )
                }
            }
            AnimatedContent(
                targetState = header.value?.panel,
                transitionSpec = { fadeIn() togetherWith fadeOut() }
            ) {
                when (it) {
                    DetailPanelUio.Inventory -> IconButton(
                        onClick = onSheet,
                    ) {
                        Icon(
                            modifier = Modifier.size(size = 24.dp),
                            painter = painterResource(Res.drawable.ic_description_24dp),
                            tint = MaterialTheme.colors.primary,
                            contentDescription = null,
                        )
                    }

                    DetailPanelUio.Sheet -> IconButton(
                        onClick = onInventory,
                    ) {
                        Icon(
                            modifier = Modifier.size(size = 24.dp),
                            painter = painterResource(Res.drawable.ic_money_bag_24dp),
                            tint = MaterialTheme.colors.primary,
                            contentDescription = null,
                        )
                    }

                    null -> Unit
                }

            }
            AnimatedVisibility(
                visible = header.value?.alteration == true,
                enter = fadeIn(),
                exit = fadeOut(),
            ) {
                IconButton(
                    onClick = onAlteration,
                ) {
                    Icon(
                        modifier = Modifier.size(size = 24.dp),
                        painter = painterResource(Res.drawable.ic_blur_on_24dp),
                        tint = MaterialTheme.colors.primary,
                        contentDescription = null,
                    )
                }
            }
            Box {
                IconButton(
                    onClick = onDiminished,
                ) {
                    Icon(
                        modifier = Modifier.size(size = 24.dp),
                        painter = painterResource(Res.drawable.ic_skull_24dp),
                        tint = MaterialTheme.colors.primary,
                        contentDescription = null,
                    )
                }
                AnimatedContent(
                    modifier = Modifier.align(alignment = Alignment.BottomCenter),
                    targetState = header.value?.diminished,
                    transitionSpec = {
                        val sign = if ((targetState ?: 0) > (initialState ?: 0)) 1 else -1
                        val enter = fadeIn() + slideInVertically { 16 * sign }
                        val exit = fadeOut() + slideOutVertically { -16 * sign }
                        enter togetherWith exit using SizeTransform(clip = false)
                    }
                ) {
                    Text(
                        modifier = Modifier.width(width = 48.dp),
                        style = MaterialTheme.typography.caption,
                        color = MaterialTheme.colors.onSurface,
                        textAlign = TextAlign.Center,
                        text = "${it?.takeIf { it > 0 } ?: "  "}",
                    )
                }
            }
            IconButton(
                onClick = onDismissRequest,
            ) {
                Icon(
                    painter = painterResource(Res.drawable.ic_close_24dp),
                    tint = MaterialTheme.lwa.colorScheme.base.primary,
                    contentDescription = null,
                )
            }
        }
        Row(
            modifier = Modifier.padding(
                start = startPadding,
                end = endPadding,
                bottom = bottomPadding,
            ),
            horizontalArrangement = Arrangement.spacedBy(space = 10.dp),
        ) {
            BasicTooltipLayout(
                tooltip = header.value?.hpTooltip,
            ) {
                Row(
                    modifier = Modifier.clip(shape = CircleShape).clickable { onHp() },
                    verticalAlignment = Alignment.Bottom,
                ) {
                    Icon(
                        modifier = Modifier
                            .padding(bottom = 4.dp, end = 2.dp)
                            .size(size = iconSize),
                        painter = painterResource(Res.drawable.ic_heart_24dp),
                        contentDescription = null
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerValue,
                        color = MaterialTheme.lwa.colorScheme.base.primary,
                        fontWeight = FontWeight.Bold,
                        text = header.value?.hp ?: "",
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerCaption,
                        fontWeight = FontWeight.Thin,
                        text = "/${header.value?.maxHp ?: ""}",
                    )
                }
            }
            BasicTooltipLayout(
                tooltip = header.value?.ppTooltip,
            ) {
                Row(
                    modifier = Modifier.clip(shape = CircleShape).clickable { onPp() },
                    verticalAlignment = Alignment.Bottom,
                ) {
                    Icon(
                        modifier = Modifier
                            .padding(bottom = 4.dp, end = 2.dp)
                            .size(size = iconSize),
                        painter = painterResource(Res.drawable.ic_water_drop_24dp),
                        contentDescription = null
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerValue,
                        color = MaterialTheme.lwa.colorScheme.base.primary,
                        fontWeight = FontWeight.Bold,
                        text = header.value?.pp ?: "",
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerCaption,
                        fontWeight = FontWeight.Thin,
                        text = "/${header.value?.maxPp ?: ""}",
                    )
                }
            }

            Spacer(modifier = Modifier.weight(1f))

            BasicTooltipLayout(
                tooltip = header.value?.reflexTooltip,
            ) {
                Row(
                    modifier = Modifier
                        .clip(shape = CircleShape)
                        .clickable { header.value?.reflexRoll?.let(onReflex) },
                    verticalAlignment = Alignment.Bottom,
                ) {
                    Icon(
                        modifier = Modifier
                            .padding(bottom = 4.dp, end = 2.dp)
                            .size(size = iconSize),
                        painter = painterResource(Res.drawable.ic_pan_tool_24dp),
                        contentDescription = null
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerValue,
                        color = MaterialTheme.lwa.colorScheme.base.primary,
                        text = header.value?.reflex ?: "",
                    )
                }
            }
            BasicTooltipLayout(
                tooltip = header.value?.initiativeTooltip,
            ) {
                Row(
                    verticalAlignment = Alignment.Bottom,
                ) {
                    Icon(
                        modifier = Modifier
                            .padding(bottom = 4.dp, end = 2.dp)
                            .size(size = iconSize),
                        painter = painterResource(Res.drawable.ic_azm_24dp),
                        contentDescription = null
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerValue,
                        text = header.value?.initiative ?: "",
                    )
                }
            }
            BasicTooltipLayout(
                tooltip = header.value?.movTooltip,
            ) {
                Row(
                    verticalAlignment = Alignment.Bottom,
                ) {
                    Icon(
                        modifier = Modifier
                            .padding(bottom = 4.dp, end = 2.dp)
                            .size(size = iconSize),
                        painter = painterResource(Res.drawable.ic_directions_run_24dp),
                        contentDescription = null,
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerValue,
                        text = header.value?.mov ?: "",
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerCaption,
                        text = "m",
                    )
                }
            }
            BasicTooltipLayout(
                tooltip = header.value?.armorTooltip,
            ) {
                Row(
                    verticalAlignment = Alignment.Bottom,
                ) {
                    Icon(
                        modifier = Modifier
                            .padding(bottom = 4.dp, end = 2.dp)
                            .size(size = iconSize),
                        painter = painterResource(Res.drawable.ic_shield_24dp),
                        contentDescription = null,
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerValue,
                        text = header.value?.armor ?: "",
                    )
                }
            }
            BasicTooltipLayout(
                tooltip = header.value?.bonusTooltip,
            ) {
                Row(
                    verticalAlignment = Alignment.Bottom,
                ) {
                    Icon(
                        modifier = Modifier
                            .padding(bottom = 4.dp, end = 2.dp)
                            .size(size = iconSize),
                        painter = painterResource(Res.drawable.ic_swords_24dp),
                        contentDescription = null,
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerValue,
                        text = header.value?.bonus ?: "",
                    )
                }
            }
            BasicTooltipLayout(
                tooltip = header.value?.growTooltip,
            ) {
                Row(
                    verticalAlignment = Alignment.Bottom,
                ) {
                    Icon(
                        modifier = Modifier
                            .padding(bottom = 4.dp, end = 2.dp)
                            .size(size = iconSize),
                        painter = painterResource(Res.drawable.ic_heart_plus_24dp),
                        contentDescription = null,
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerValue,
                        text = header.value?.grow ?: "",
                    )
                }
            }
            BasicTooltipLayout(
                tooltip = header.value?.learnTooltip,
            ) {
                Row(
                    verticalAlignment = Alignment.Bottom,
                ) {
                    Icon(
                        modifier = Modifier
                            .padding(bottom = 4.dp, end = 2.dp)
                            .size(size = iconSize),
                        painter = painterResource(Res.drawable.ic_cognition_24dp),
                        contentDescription = null,
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.characterSheet.headerValue,
                        text = header.value?.learn ?: "",
                    )
                }
            }
        }
    }
}