package com.pixelized.headache.ui.page.event.list

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.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.graphics.Color
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.pixelized.headache.ui.theme.HeadacheTheme
import com.pixelized.headache.ui.theme.color.HeadacheColorPalette

@Stable
data class PillUio(
    val label: String,
    val amount: Int,
    val description: String,
    val color: Color,
    val isValid: Boolean,
    val isMisspelled: Boolean,
)

@Stable
data object PillItemDefault {
    @Stable
    val spacing = 4.dp
}

@Composable
fun PillItem(
    modifier: Modifier = Modifier,
    spacing: Dp = PillItemDefault.spacing,
    item: PillUio,
) {
    Row(
        modifier = modifier,
        horizontalArrangement = Arrangement.spacedBy(space = spacing),
    ) {
        Text(
            modifier = Modifier.alignByBaseline(),
            style = MaterialTheme.typography.bodyMedium,
            text = "-",
        )
        Box(
            modifier = Modifier
                .align(alignment = Alignment.CenterVertically)
                .size(12.dp)
                .background(color = item.color)
        )
        Text(
            modifier = Modifier.alignByBaseline(),
            style = MaterialTheme.typography.bodyMedium,
            text = "${item.label}: ${item.amount}",
        )
        Text(
            modifier = Modifier.alignByBaseline(),
            style = MaterialTheme.typography.labelSmall,
            fontStyle = FontStyle.Italic,
            color = when (item.isValid.not() || item.isMisspelled) {
                true -> MaterialTheme.colorScheme.error
                else -> MaterialTheme.colorScheme.onSurface
            },
            text = "\"${item.description}\"",
        )
    }
}

@Composable
@Preview
private fun PillItemPreview(
    @PreviewParameter(PillPreviewProvider::class) preview: PillUio,
) {
    HeadacheTheme {
        Surface {
            PillItem(
                item = preview,
            )
        }
    }
}

private class PillPreviewProvider : PreviewParameterProvider<PillUio> {
    override val values: Sequence<PillUio>
        get() = sequenceOf(
            PillUio(
                label = "Élétriptan 40",
                amount = 2,
                description = "Élétriptan 400 x2",
                color = HeadacheColorPalette.Additional.Purple,
                isValid = true,
                isMisspelled = false,
            ),
            PillUio(
                label = "Élétriptan 400",
                amount = 2,
                description = "Élitriptan 400 x2",
                color = HeadacheColorPalette.Additional.Purple,
                isValid = true,
                isMisspelled = true,
            ),
            PillUio(
                label = "?",
                amount = 2,
                description = "Ilitriptan 400 x2",
                color = HeadacheColorPalette.Additional.Purple,
                isValid = false,
                isMisspelled = false,
            ),
        )

}