package com.pixelized.desktop.lwa.ui.screen.admin.common.tag

import androidx.compose.animation.animateColorAsState
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.shape.CircleShape
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape
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.theme.color.component.LwaButtonColors
import com.pixelized.desktop.lwa.ui.theme.lwa


@Stable
data class GMTagUio(
    val id: String,
    val label: String,
    val highlight: Boolean,
    val meta: Boolean,
)

@Immutable
object GmTagDefault {
    @Stable
    val padding = PaddingValues(horizontal = 8.dp, vertical = 4.dp)
}

@Composable
fun GMTag(
    modifier: Modifier = Modifier,
    padding: PaddingValues = GmTagDefault.padding,
    shape: Shape = CircleShape,
    elevation: Dp = 2.dp,
    tag: GMTagUio,
    onTag: (() -> Unit)? = null,
) {
    val animatedColor = animateColorAsState(
        when (tag.highlight) {
            true -> MaterialTheme.lwa.colorScheme.base.secondary
            else -> MaterialTheme.lwa.colorScheme.base.onSurface
        }
    )
    Surface(
        modifier = modifier,
        shape = shape,
        elevation = elevation,
    ) {
        Row(
            modifier = Modifier
                .clickable(enabled = onTag != null) { onTag?.invoke() }
                .padding(paddingValues = padding),
            horizontalArrangement = Arrangement.spacedBy(space = 1.dp),
        ) {
            if (tag.meta) {
                Text(
                    modifier = Modifier.alignByBaseline(),
                    style = MaterialTheme.lwa.typography.system.caption,
                    color = animatedColor.value,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    text = "⬫",
                )
            }
            Text(
                modifier = Modifier.alignByBaseline(),
                style = MaterialTheme.lwa.typography.system.caption,
                color = animatedColor.value,
                overflow = TextOverflow.Ellipsis,
                maxLines = 1,
                text = tag.label,
            )
        }
    }
}

@Immutable
object GmTagButtonDefault {
    @Stable
    val padding = PaddingValues()
}

@Composable
fun GMTagButton(
    modifier: Modifier = Modifier,
    padding: PaddingValues = GmTagButtonDefault.padding,
    shape: Shape = MaterialTheme.shapes.small,
    tag: GMTagUio,
    onTag: (() -> Unit)? = null,
) {
    val animatedColor = animateColorAsState(
        when (tag.highlight) {
            true -> MaterialTheme.lwa.colorScheme.base.secondary
            else -> MaterialTheme.lwa.colorScheme.base.onSurface
        }
    )
    Button(
        modifier = modifier,
        colors = LwaButtonColors(contentColor = animatedColor.value),
        shape = shape,
        enabled = onTag != null,
        onClick = { onTag?.invoke() },
    ) {
        Row(
            modifier = Modifier.padding(paddingValues = padding),
            horizontalArrangement = Arrangement.spacedBy(space = 1.dp),
        ) {
            if (tag.meta) {
                Text(
                    modifier = Modifier.alignByBaseline(),
                    color = animatedColor.value,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    text = "⬫",
                )
            }
            Text(
                modifier = Modifier.alignByBaseline(),
                color = animatedColor.value,
                overflow = TextOverflow.Ellipsis,
                maxLines = 1,
                text = tag.label,
            )
        }
    }
}