package com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.sheet.item

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
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.shapes.MasteryShape
import com.pixelized.desktop.lwa.ui.composable.tooltip.BasicTooltipLayout
import com.pixelized.desktop.lwa.ui.composable.tooltip.BasicTooltipUio
import com.pixelized.desktop.lwa.ui.overlay.roll.RollAction
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.ic_d20_24dp
import org.jetbrains.compose.resources.painterResource

@Stable
data class CharacterDetailSheetActionUio(
    val actionId: String,
    val label: String,
    val tooltips: BasicTooltipUio?,
    val roll: RollAction.Uio,
)

@Immutable
data object CharacterDetailSheetActionDefault {
    @Stable
    val paddingValues = PaddingValues(start = 10.dp, end = 9.dp, top = 6.dp, bottom = 6.dp)

    @Stable
    val spacing: Dp = 4.dp
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CharacterDetailSheetAction(
    modifier: Modifier = Modifier,
    paddingValues: PaddingValues = CharacterDetailSheetActionDefault.paddingValues,
    spacing: Dp = CharacterDetailSheetActionDefault.spacing,
    action: CharacterDetailSheetActionUio,
    onClick: (CharacterDetailSheetActionUio) -> Unit,
) {
    BasicTooltipLayout(
        tooltip = action.tooltips,
        content = {
            Row(
                modifier = Modifier
                    .clickable(onClick = { onClick(action) })
                    .padding(paddingValues = paddingValues)
                    .then(other = modifier),
                horizontalArrangement = Arrangement.spacedBy(space = spacing),
                verticalAlignment = Alignment.CenterVertically,
            ) {
                MasteryShape(
                    modifier = Modifier.padding(top = 4.dp, end = 2.dp),
                    multiplier = 2,
                )
                Text(
                    modifier = Modifier.weight(1f),
                    style = MaterialTheme.typography.body1,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    text = action.label,
                )
                Icon(
                    modifier = Modifier.size(size = 24.dp),
                    painter = painterResource(Res.drawable.ic_d20_24dp),
                    tint = MaterialTheme.colors.primary,
                    contentDescription = null,
                )
            }
        }
    )
}