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

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.TooltipPlacement
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.MaterialTheme
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.graphics.FilterQuality
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import coil3.PlatformContext
import coil3.compose.AsyncImage
import coil3.request.ImageRequest
import com.pixelized.desktop.lwa.ui.composable.tooltip.BasicTooltipLayout
import com.pixelized.desktop.lwa.ui.composable.tooltip.BasicTooltipUio
import com.pixelized.desktop.lwa.ui.theme.lwa
import com.pixelized.desktop.lwa.utils.extention.invert

@Stable
data class CharacterRibbonAlterationUio(
    val icon: String,
    val tooltips: BasicTooltipUio?,
)

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CharacterRibbonAlteration(
    modifier: Modifier = Modifier,
    width: Dp = MaterialTheme.lwa.dimen.portrait.minimized.width,
    status: List<List<CharacterRibbonAlterationUio>>,
) {
    val direction = LocalLayoutDirection.current
    Row(
        modifier = Modifier
            .animateContentSize()
            .width(width = width)
            .then(other = modifier),
        horizontalArrangement = Arrangement.spacedBy(space = 4.dp),
    ) {
        status.forEach { columns ->
            Column(
                modifier = Modifier.animateContentSize(),
                verticalArrangement = Arrangement.spacedBy(space = 2.dp),
            ) {
                columns.forEach {
                    BasicTooltipLayout(
                        delayMillis = 0,
                        tooltip = it.tooltips,
                        tooltipPlacement = remember(direction) {
                            TooltipPlacement.CursorPoint(
                                alignment = when (direction.invert) {
                                    LayoutDirection.Ltr -> Alignment.CenterStart
                                    LayoutDirection.Rtl -> Alignment.CenterEnd
                                },
                            )
                        },
                        content = {
                            AnimatedContent(
                                targetState = it.icon,
                                transitionSpec = { fadeIn() togetherWith fadeOut() },
                            ) { icon ->
                                AsyncImage(
                                    modifier = Modifier.size(24.dp),
                                    model = ImageRequest.Builder(context = PlatformContext.INSTANCE)
                                        .data(data = icon)
                                        .size(size = 48)
                                        .build(),
                                    filterQuality = FilterQuality.High,
                                    contentDescription = null,
                                )
                            }
                        }
                    )
                }
            }
        }
    }
}