package com.pixelized.desktop.lwa.ui.composable.character.alterteration

import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.minimumInteractiveComponentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.screen.admin.common.tag.GMTag
import com.pixelized.desktop.lwa.ui.screen.admin.common.tag.GMTagUio
import com.pixelized.desktop.lwa.ui.theme.lwa

@Stable
data class AlterationToggleItemUio(
    val id: String,
    val label: String,
    val tags: List<GMTagUio>,
    val enable: Boolean,
    val active: Boolean,
)

@Stable
object AlterationToggleItemDefault {
    val padding = PaddingValues(start = 8.dp)
}

@Composable
fun AlterationToggleItem(
    modifier: Modifier = Modifier,
    alteration: AlterationToggleItemUio,
    padding: PaddingValues = AlterationToggleItemDefault.padding,
    onAlteration: () -> Unit,
    onTag: (String) -> Unit,
) {
    Row(
        modifier = Modifier
            .clip(shape = MaterialTheme.lwa.shapes.gameMaster)
            .clickable(enabled = alteration.enable, onClick = onAlteration)
            .background(color = MaterialTheme.lwa.colorScheme.elevated.base1dp)
            .minimumInteractiveComponentSize()
            .padding(paddingValues = padding)
            .graphicsLayer { this.alpha = if (alteration.enable) 1f else 0.5f }
            .then(other = modifier),
        horizontalArrangement = Arrangement.spacedBy(space = 8.dp),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        val animatedColor = animateColorAsState(
            targetValue = when (alteration.active) {
                true -> MaterialTheme.lwa.colorScheme.base.secondary
                else -> MaterialTheme.lwa.colorScheme.base.onSurface
            }
        )
        Text(
            style = MaterialTheme.lwa.typography.system.body1,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1,
            color = animatedColor.value,
            text = alteration.label,
        )
        Row(
            modifier = Modifier.weight(1f).height(intrinsicSize = IntrinsicSize.Min),
            horizontalArrangement = Arrangement.spacedBy(space = 2.dp, alignment = Alignment.End)
        ) {
            alteration.tags.forEach { tag ->
                GMTag(
                    elevation = 4.dp,
                    tag = tag,
                    onTag = { onTag(tag.id) },
                )
            }
        }
    }
}