package com.pixelized.desktop.lwa.ui.composable.currency

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.TooltipPlacement
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
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.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.composable.tooltip.TooltipDefault
import com.pixelized.desktop.lwa.ui.composable.tooltip.TooltipLayout
import com.pixelized.desktop.lwa.ui.theme.lwa
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.generic__copper__singular
import lwacharactersheet.composeapp.generated.resources.generic__gold__singular
import lwacharactersheet.composeapp.generated.resources.generic__silver__singular
import lwacharactersheet.composeapp.generated.resources.ic_copper_32px
import lwacharactersheet.composeapp.generated.resources.ic_gold_32px
import lwacharactersheet.composeapp.generated.resources.ic_silver_32px
import org.jetbrains.compose.resources.DrawableResource
import org.jetbrains.compose.resources.StringResource
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource

@Stable
sealed class CurrencyUio(
    val icon: DrawableResource,
    val tooltip: StringResource,
) {
    @Stable
    data object Gold : CurrencyUio(
        icon = Res.drawable.ic_gold_32px,
        tooltip = Res.string.generic__gold__singular,
    )

    @Stable
    data object Silver : CurrencyUio(
        icon = Res.drawable.ic_silver_32px,
        tooltip = Res.string.generic__silver__singular,
    )

    @Stable
    data object Copper : CurrencyUio(
        icon = Res.drawable.ic_copper_32px,
        tooltip = Res.string.generic__copper__singular,
    )
}

@Stable
object CurrencyDefault {
    @Stable
    val size = DpSize(width = 16.dp, height = 16.dp)

    @Stable
    val padding = PaddingValues(bottom = 2.dp)

    @Stable
    val brush = Color(0xFF121212).let { color ->
        Brush.horizontalGradient(
            listOf(
                color.copy(alpha = 0f),
                color,
                color,
                color.copy(alpha = 0f),
            )
        )
    }

    @OptIn(ExperimentalFoundationApi::class)
    @Stable
    val placement = TooltipPlacement.ComponentRect(
        anchor = Alignment.Center,
        alignment = Alignment.BottomEnd,
    )
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Currency(
    modifier: Modifier = Modifier,
    paddings: PaddingValues = CurrencyDefault.padding,
    tooltipPlacement: TooltipPlacement = CurrencyDefault.placement,
    size: DpSize = CurrencyDefault.size,
    currency: CurrencyUio,
) {
    TooltipLayout(
        modifier = modifier,
        tooltipPlacement = tooltipPlacement,
        delayMillis = 0,
        tips = currency.tooltip,
        tooltip = {
            Box(
                modifier = Modifier
                    .background(brush = CurrencyDefault.brush)
                    .padding(horizontal = 8.dp, vertical = 4.dp),
            ) {
                Text(
                    style = MaterialTheme.lwa.typography.system.caption,
                    text = stringResource(it),
                )
            }
        },
        content = {
            Image(
                modifier = Modifier
                    .padding(paddingValues = paddings)
                    .size(size = size),
                painter = painterResource(currency.icon),
                contentDescription = null,
            )
        }
    )
}