package com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon.common

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.theme.lwa
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.ic_heart_24dp
import lwacharactersheet.composeapp.generated.resources.ic_water_drop_24dp
import org.jetbrains.compose.resources.painterResource

@Stable
data class CharacterRibbonStatsUio(
    val hp: Int,
    val maxHp: Int,
    val pp: Int,
    val maxPp: Int,
)

@Composable
fun CharacterRibbonStats(
    modifier: Modifier = Modifier,
    status: CharacterRibbonStatsUio?,
) {
    val typography = MaterialTheme.lwa.typography
    val valueSpanStyle = remember(typography) { typography.portrait.value.toSpanStyle() }
    val separatorSpanStyle = remember(typography) { typography.portrait.separator.toSpanStyle() }
    val maxSpanStyle = remember(typography) { typography.portrait.max.toSpanStyle() }

    status?.let { status ->
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 2.dp, horizontal = 4.dp)
                .then(other = modifier),
            verticalArrangement = Arrangement.aligned(alignment = Alignment.Bottom),
        ) {
            Row {
                Icon(
                    modifier = Modifier
                        .size(12.dp)
                        .offset(y = 2.dp),
                    painter = painterResource(Res.drawable.ic_heart_24dp),
                    contentDescription = null
                )
                Spacer(
                    modifier = Modifier.width(width = 2.dp),
                )
                Text(
                    modifier = Modifier.alignByBaseline(),
                    style = typography.portrait.value,
                    text = remember(status.hp, status.maxHp) {
                        buildAnnotatedString {
                            withStyle(style = valueSpanStyle) { append("${status.hp}") }
                            withStyle(style = separatorSpanStyle) { append("/") }
                            withStyle(style = maxSpanStyle) { append("${status.maxHp}") }
                        }
                    },
                )
            }
            Row {
                Icon(
                    modifier = Modifier
                        .size(size = 12.dp)
                        .offset(y = 2.dp),
                    painter = painterResource(Res.drawable.ic_water_drop_24dp),
                    contentDescription = null
                )
                Spacer(
                    modifier = Modifier.width(width = 2.dp),
                )
                Text(
                    style = typography.portrait.value,
                    text = remember(status.pp, status.maxPp) {
                        buildAnnotatedString {
                            withStyle(style = valueSpanStyle) { append("${status.pp}") }
                            withStyle(style = separatorSpanStyle) { append("/") }
                            withStyle(style = maxSpanStyle) { append("${status.maxPp}") }
                        }
                    },
                )
            }
        }
    }
}