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.Checkbox
import androidx.compose.material.CheckboxDefaults
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.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
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 com.pixelized.desktop.lwa.ui.theme.lwa

@Stable
data class CharacterDetailSheetSkillUio(
    val skillId: String,
    val label: String,
    val value: String,
    val used: Boolean,
    val occupation: Boolean,
    val tooltips: BasicTooltipUio?,
    val roll: RollAction.Uio,
)

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CharacterDetailSheetSkill(
    modifier: Modifier = Modifier,
    paddingValues: PaddingValues = PaddingValues(
        start = 8.dp,
        end = 4.dp,
        top = 1.dp,
        bottom = 1.dp
    ),
    skill: CharacterDetailSheetSkillUio,
    onSkill: (CharacterDetailSheetSkillUio) -> Unit,
    onUse: (CharacterDetailSheetSkillUio) -> Unit,
) {
    BasicTooltipLayout(
        tooltip = skill.tooltips,
        content = {
            Row(
                modifier = Modifier
                    .clickable(onClick = { onSkill(skill) })
                    .padding(paddingValues = paddingValues)
                    .then(other = modifier),
                horizontalArrangement = Arrangement.spacedBy(space = 4.dp),
                verticalAlignment = Alignment.CenterVertically,
            ) {
                MasteryShape(
                    modifier = Modifier.padding(top = 4.dp, end = 2.dp),
                    multiplier = if (skill.occupation) 1 else 0,
                )
                Text(
                    modifier = Modifier.weight(1f),
                    style = MaterialTheme.typography.body1,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    text = skill.label
                )
                Text(
                    style = MaterialTheme.lwa.typography.characterSheet.skill,
                    fontWeight = FontWeight.Bold,
                    color = MaterialTheme.colors.primary,
                    text = skill.value,
                )
                Checkbox(
                    modifier = Modifier.size(size = 32.dp),
                    checked = skill.used,
                    colors = CheckboxDefaults.colors(
                        checkedColor = MaterialTheme.colors.primary,
                    ),
                    onCheckedChange = { onUse(skill) },
                )
            }
        },
    )
}