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

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 com.pixelized.desktop.lwa.ui.theme.lwa
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.roll_history__item__difficulty
import lwacharactersheet.composeapp.generated.resources.roll_history__item__throw
import org.jetbrains.compose.resources.stringResource

@Stable
data class RollTextMessageUio(
    override val id: String,
    override val timestamp: String,
    val character: String,
    val skillLabel: String,
    val rollDifficulty: String?,
    val rollValue: Int,
    val rollSuccessLimit: Int?,
    val resultLabel: String?,
    val resultType: Critical?,
) : TextMessage {
    enum class Critical {
        CRITICAL_SUCCESS,
        SPECIAL_SUCCESS,
        SUCCESS,
        FAILURE,
        CRITICAL_FAILURE
    }
}

@Composable
fun RollTextMessage(
    modifier: Modifier = Modifier,
    message: RollTextMessageUio,
) {
    Row(
        modifier = modifier,
        horizontalArrangement = Arrangement.spacedBy(space = 3.dp),
        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(Res.string.roll_history__item__throw),
        )
        Text(
            modifier = Modifier.alignByBaseline(),
            style = MaterialTheme.lwa.typography.chat.text,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1,
            text = message.skillLabel,
        )
        Text(
            modifier = Modifier.alignByBaseline(),
            style = MaterialTheme.lwa.typography.chat.text,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1,
            text = "-",
        )
        message.resultLabel?.let {
            Text(
                modifier = Modifier.alignByBaseline(),
                style = MaterialTheme.lwa.typography.chat.text,
                color = when (message.resultType) {
                    RollTextMessageUio.Critical.CRITICAL_SUCCESS -> MaterialTheme.lwa.colorScheme.chat.criticalSuccess
                    RollTextMessageUio.Critical.SPECIAL_SUCCESS -> MaterialTheme.lwa.colorScheme.chat.spacialSuccess
                    RollTextMessageUio.Critical.SUCCESS -> MaterialTheme.lwa.colorScheme.chat.success
                    RollTextMessageUio.Critical.FAILURE -> MaterialTheme.lwa.colorScheme.chat.failure
                    RollTextMessageUio.Critical.CRITICAL_FAILURE -> MaterialTheme.lwa.colorScheme.chat.criticalFailure
                    null -> MaterialTheme.lwa.colorScheme.chat.text
                },
                overflow = TextOverflow.Ellipsis,
                maxLines = 1,
                text = it,
            )
        }
        Row {
            Text(
                modifier = Modifier.alignByBaseline(),
                style = MaterialTheme.lwa.typography.chat.text,
                overflow = TextOverflow.Ellipsis,
                maxLines = 1,
                text = "(${message.rollValue}",
            )
            message.rollSuccessLimit?.let {
                Text(
                    modifier = Modifier.alignByBaseline(),
                    style = MaterialTheme.lwa.typography.chat.text,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    text = "/$it",
                )
            }
            Text(
                modifier = Modifier.alignByBaseline(),
                style = MaterialTheme.lwa.typography.chat.text,
                overflow = TextOverflow.Ellipsis,
                maxLines = 1,
                text = ")",
            )
        }
        message.rollDifficulty?.let {
            Text(
                modifier = Modifier.alignByBaseline(),
                style = MaterialTheme.lwa.typography.chat.text,
                overflow = TextOverflow.Ellipsis,
                maxLines = 1,
                text = "-",
            )
            Text(
                modifier = Modifier.alignByBaseline(),
                style = MaterialTheme.lwa.typography.chat.text,
                overflow = TextOverflow.Ellipsis,
                maxLines = 1,
                text = stringResource(Res.string.roll_history__item__difficulty),
            )
            Text(
                modifier = Modifier.alignByBaseline(),
                style = MaterialTheme.lwa.typography.chat.text,
                overflow = TextOverflow.Ellipsis,
                maxLines = 1,
                text = it,
            )
        }
    }
}