package com.pixelized.desktop.lwa.ui.screen.campaign.chatbox.messages

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.TooltipPlacement
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
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.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.composable.currency.Currency
import com.pixelized.desktop.lwa.ui.composable.currency.CurrencyUio
import com.pixelized.desktop.lwa.ui.theme.lwa
import org.jetbrains.compose.resources.StringResource
import org.jetbrains.compose.resources.stringResource

@Stable
data class PurseTextMessageUio(
    override val id: String,
    override val timestamp: String,
    val character: String,
    val action: StringResource,
    val gold: Int?,
    val silver: Int?,
    val copper: Int?,
) : TextMessage

@Stable
object PurseTextDefault {
    @Stable
    val spacings: Dp = 3.dp

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

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun PurseTextMessage(
    modifier: Modifier = Modifier,
    spacings: Dp = PurseTextDefault.spacings,
    placement : TooltipPlacement = PurseTextDefault.placement,
    message: PurseTextMessageUio,
) {
    Row(
        modifier = modifier,
        horizontalArrangement = Arrangement.spacedBy(space = spacings),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Text(
            modifier = Modifier.alignByBaseline(),
            style = MaterialTheme.lwa.typography.chat.timestamp,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1,
            text = message.timestamp,
        )
        Text(
            modifier = Modifier.alignByBaseline(),
            style = MaterialTheme.lwa.typography.chat.timestamp,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1,
            text = ">",
        )
        Text(
            modifier = Modifier.alignByBaseline(),
            style = MaterialTheme.lwa.typography.chat.text,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1,
            text = message.character,
        )
        Text(
            modifier = Modifier.alignByBaseline(),
            style = MaterialTheme.lwa.typography.chat.text,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1,
            text = stringResource(message.action),
        )
        message.gold?.let {
            Row {
                Text(
                    modifier = Modifier.alignByBaseline(),
                    style = MaterialTheme.lwa.typography.chat.text,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    text = "$it",
                )
                Currency(
                    tooltipPlacement = placement,
                    currency = CurrencyUio.Gold,
                )
            }
        }
        message.silver?.let {
            Row {
                Text(
                    modifier = Modifier.alignByBaseline(),
                    style = MaterialTheme.lwa.typography.chat.text,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    text = "$it",
                )
                Currency(
                    tooltipPlacement = placement,
                    currency = CurrencyUio.Silver,
                )
            }
        }

        message.copper?.let {
            Row {
                Text(
                    modifier = Modifier.alignByBaseline(),
                    style = MaterialTheme.lwa.typography.chat.text,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    text = "$it",
                )
                Currency(
                    tooltipPlacement = placement,
                    currency = CurrencyUio.Copper,
                )
            }
        }
    }
}