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

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.request.ImageRequest
import com.pixelized.desktop.lwa.ui.composable.image.LwaAsyncImage
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,
    alterations: List<List<CharacterRibbonAlterationUio>>,
) {
    val direction = LocalLayoutDirection.current

    Row(
        modifier = Modifier
            .width(width = width)
            .then(other = modifier),
        horizontalArrangement = Arrangement.spacedBy(space = 4.dp),
    ) {
        alterations.forEach { columns ->
            Column(
                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 = {
                            LwaAsyncImage(
                                modifier = Modifier.size(24.dp),
                                model = ImageRequest.Builder(context = PlatformContext.INSTANCE)
                                    .data(data = it.icon)
                                    .size(size = 48)
                                    .build(),
                                filterQuality = FilterQuality.High,
                                contentDescription = null,
                            )
                        }
                    )
                }
            }
        }
    }
}