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

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.PointerMatcher
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
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.foundation.onClick
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.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.graphics.FilterQuality
import androidx.compose.ui.input.pointer.PointerButton
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.pixelized.desktop.lwa.ui.composable.shapes.ArrowShape
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 CharacterRibbonPortraitUio(
    val portrait: String?,
    val name: String,
    val levelUp: Boolean,
    val stats: StatsDetail?,
) {
    @Stable
    data class StatsDetail(
        val hp: Int,
        val maxHp: Int,
        val pp: Int,
        val maxPp: Int,
    )
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CharacterRibbonPortrait(
    modifier: Modifier = Modifier,
    size: DpSize = MaterialTheme.lwa.dimen.portrait.minimized,
    levelUpOffset: Dp = 9.dp,
    character: CharacterRibbonPortraitUio,
    onLevelUp: () -> Unit,
) {
    val colorScheme = MaterialTheme.lwa.colorScheme

    Box(
        modifier = modifier
            .size(size = size)
            .clip(shape = MaterialTheme.lwa.shapes.portrait)
            .background(color = colorScheme.elevated.base1dp)
    ) {
        AnimatedContent(
            targetState = character.portrait,
            transitionSpec = { fadeIn() togetherWith fadeOut() },
        ) { portrait ->
            AsyncImage(
                modifier = Modifier.fillMaxSize(),
                model = portrait,
                contentScale = ContentScale.Crop,
                alignment = Alignment.TopCenter,
                filterQuality = FilterQuality.High,
                contentDescription = null,
            )
        }

        AnimatedVisibility(
            modifier = Modifier
                .align(alignment = Alignment.TopEnd)
                .offset(x = levelUpOffset, y = -levelUpOffset),
            visible = character.levelUp,
            enter = fadeIn(),
            exit = fadeOut(),
        ) {
            IconButton(
                onClick = onLevelUp,
            ) {
                ArrowShape(
                    color = MaterialTheme.lwa.colorScheme.portrait.levelUp,
                )
            }
        }

        character.stats?.let { stats ->

            BloodOverlay(
                maxHp = stats.maxHp.toFloat(),
                hp = stats.hp.toFloat(),
            )

            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .drawWithContent {
                        drawRect(brush = colorScheme.portraitBackgroundBrush)
                        drawContent()
                    }
                    .padding(vertical = 2.dp, horizontal = 4.dp),
                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 = MaterialTheme.lwa.typography.portrait.value,
                        text = "${stats.hp}",
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.portrait.separator,
                        text = "/",
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.portrait.max,
                        text = "${stats.maxHp}",
                    )
                }
                Row {
                    Icon(
                        modifier = Modifier
                            .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(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.portrait.value,
                        text = "${stats.pp}",
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.portrait.separator,
                        text = "/",
                    )
                    Text(
                        modifier = Modifier.alignByBaseline(),
                        style = MaterialTheme.lwa.typography.portrait.max,
                        text = "${stats.maxPp}",
                    )
                }
            }
        }
    }
}