package com.pixelized.desktop.lwa.ui.screen.levelup.items

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.SizeTransform
import androidx.compose.animation.animateColorAsState
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.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.composable.shapes.MasteryShape
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.theme.lwa
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.level_up__skill_level
import org.jetbrains.compose.resources.stringResource

@Stable
data class LevelUpSkillUio(
    val skillId: String,
    val label: String,
    val value: Int,
    val level: Int,
    val levelUp: Boolean,
    val occupation: Boolean,
    val tooltips: BasicTooltipUio?,
    val roll: RollAction.Uio?,
)

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LevelUpSkill(
    modifier: Modifier = Modifier,
    paddingValues: PaddingValues = PaddingValues(start = 8.dp, end = 8.dp),
    skill: LevelUpSkillUio,
    onSkill: (LevelUpSkillUio) -> Unit,
) {
    val valueColor = animateColorAsState(
        targetValue = when (skill.roll) {
            null -> MaterialTheme.lwa.colorScheme.base.onSurface
            else -> MaterialTheme.lwa.colorScheme.base.primary
        }
    )
    BasicTooltipLayout(
        modifier = Modifier
            .let {
                when (skill.roll) {
                    null -> it
                    else -> it.clickable(onClick = { onSkill(skill) })
                }
            }
            .padding(paddingValues = paddingValues)
            .then(other = modifier),
        tooltip = skill.tooltips,
    ) {
        Row(

            horizontalArrangement = Arrangement.spacedBy(space = 8.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            MasteryShape(
                modifier = Modifier.padding(top = 4.dp),
                multiplier = if (skill.occupation) 1 else 0,
            )
            Row(
                horizontalArrangement = Arrangement.spacedBy(space = 4.dp),
            ) {
                Text(
                    modifier = Modifier.alignByBaseline().weight(1f),
                    style = MaterialTheme.typography.body1,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    text = skill.label
                )
                AnimatedContent(
                    targetState = skill.levelUp,
                    transitionSpec = {
                        val enter = fadeIn() + slideInVertically { -16 }
                        val exit = fadeOut() + slideOutVertically { 16 }
                        enter togetherWith exit using SizeTransform(clip = false)
                    }
                ) {
                    Row(
                        horizontalArrangement = Arrangement.spacedBy(space = 4.dp),
                    ) {
                        Text(
                            modifier = Modifier.alignByBaseline(),
                            style = MaterialTheme.typography.caption,
                            overflow = TextOverflow.Ellipsis,
                            maxLines = 1,
                            text = stringResource(
                                resource = Res.string.level_up__skill_level,
                                when (it) {
                                    true -> skill.level + 1
                                    else -> skill.level
                                },
                            )
                        )
                        Text(
                            modifier = Modifier.alignByBaseline(),
                            style = MaterialTheme.typography.body1,
                            fontWeight = FontWeight.Bold,
                            color = when (it) {
                                true -> MaterialTheme.lwa.colorScheme.base.secondary
                                else -> valueColor.value
                            },
                            text = when (it) {
                                true -> "${skill.value + 5}"
                                else -> "${skill.value}"
                            },
                        )
                    }
                }
            }
        }
    }
}