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

import android.annotation.SuppressLint
import android.icu.text.SimpleDateFormat
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
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.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
import java.util.Date
import java.util.Locale

@Stable
data class EventItemUio(
    val id: Long,
    val date: Date,
    val pills: List<PillUio>,
    val isValid: Boolean,
    val isMisspelled: Boolean,
)

@Stable
data object EventItemDefault {

    @Stable
    val padding: PaddingValues = PaddingValues(horizontal = 16.dp, vertical = 8.dp)

    @Stable
    val spacing: Dp = 4.dp

    @Stable
    val minHeigh: Dp = 56.dp

    @Stable
    val colorBoxWidth: Dp = 4.dp

    @Stable
    val colorBoxPadding: PaddingValues = PaddingValues(vertical = 8.dp)

    @SuppressLint("ConstantLocale")
    @Stable
    val formatter = SimpleDateFormat("dd MMM yyyy", Locale.getDefault())
}

@Composable
fun EventItem(
    modifier: Modifier = Modifier,
    padding: PaddingValues = EventItemDefault.padding,
    spacing: Dp = EventItemDefault.spacing,
    minHeigh: Dp = EventItemDefault.minHeigh,
    colorBoxWidth: Dp = EventItemDefault.colorBoxWidth,
    colorBoxPadding: PaddingValues = EventItemDefault.colorBoxPadding,
    formatter: SimpleDateFormat = EventItemDefault.formatter,
    item: EventItemUio,
    onItem: (EventItemUio) -> Unit,
) {
    Box(
        modifier = Modifier
            .clickable { onItem(item) }
            .heightIn(min = minHeigh)
            .height(IntrinsicSize.Min)
            .fillMaxWidth()
            .then(other = modifier),
        contentAlignment = Alignment.CenterStart,
    ) {
        if (item.isMisspelled || item.isValid.not()) {
            Box(
                modifier = Modifier
                    .padding(paddingValues = colorBoxPadding)
                    .background(
                        color = when {
                            item.isMisspelled -> MaterialTheme.colorScheme.secondary
                            item.isValid.not() -> MaterialTheme.colorScheme.error
                            else -> Color.Transparent
                        }
                    )
                    .fillMaxHeight()
                    .width(width = colorBoxWidth)
            )
        }
        Column(
            modifier = Modifier.padding(paddingValues = padding),
            verticalArrangement = Arrangement.spacedBy(space = spacing),
        ) {
            Text(
                style = MaterialTheme.typography.titleLarge,
                text = formatter.format(item.date),
            )
            if (item.pills.isNotEmpty()) {
                Column {
                    item.pills.forEach { pill ->
                        PillItem(
                            item = pill
                        )
                    }
                }
            }
        }
    }
}

@Composable
@Preview
private fun EventItemPreview(
    @PreviewParameter(EventPreviewProvider::class) preview: EventItemUio,
) {
    HeadacheTheme {
        Surface {
            EventItem(
                item = preview,
                onItem = { },
            )
        }
    }
}

private class EventPreviewProvider() : PreviewParameterProvider<EventItemUio> {
    override val values: Sequence<EventItemUio>
        get() = sequenceOf(
            EventItemUio(
                id = 0,
                date = Date(),
                pills = listOf(
                    PillUio(
                        label = "Spifen 400",
                        amount = 1,
                        description = "Spifen 400",
                        color = HeadacheColorPalette.Additional.Pink,
                        isValid = true,
                        isMisspelled = false,
                    ),
                    PillUio(
                        label = "Élétriptan 40",
                        amount = 1,
                        description = "Élétriptan 40",
                        color = HeadacheColorPalette.Additional.Purple,
                        isValid = true,
                        isMisspelled = false,
                    ),
                ),
                isValid = true,
                isMisspelled = false,
            ),
            EventItemUio(
                id = 1,
                date = Date(),
                pills = emptyList(),
                isValid = true,
                isMisspelled = false,
            ),
            EventItemUio(
                id = 2,
                date = Date(),
                pills = listOf(
                    PillUio(
                        label = "Spifen 400",
                        amount = 1,
                        description = "Spifen 400",
                        color = HeadacheColorPalette.Additional.Yellow,
                        isValid = true,
                        isMisspelled = false,
                    ),
                    PillUio(
                        label = "Élétriptan 40",
                        amount = 1,
                        description = "Élitriptan 40",
                        color = HeadacheColorPalette.Additional.Purple,
                        isValid = true,
                        isMisspelled = true,
                    ),
                ),
                isValid = true,
                isMisspelled = true,
            ),
            EventItemUio(
                id = 3,
                date = Date(),
                pills = listOf(
                    PillUio(
                        label = "Spifen 400",
                        amount = 1,
                        description = "Spifen 400",
                        color = HeadacheColorPalette.Additional.Yellow,
                        isValid = true,
                        isMisspelled = false,
                    ),
                    PillUio(
                        label = "?",
                        amount = 1,
                        description = "Élitriptan 40",
                        color = HeadacheColorPalette.Additional.Purple,
                        isValid = false,
                        isMisspelled = false,
                    ),
                ),
                isValid = false,
                isMisspelled = false,
            ),
        )
}