package com.pixelized.headache.ui.page.summary.monthly.item

import android.annotation.SuppressLint
import android.icu.text.SimpleDateFormat
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
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.runtime.remember
import androidx.compose.ui.Modifier
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.DpSize
import androidx.compose.ui.unit.dp
import com.pixelized.headache.ui.theme.HeadacheTheme
import com.pixelized.headache.ui.theme.color.HeadacheColorPalette
import com.pixelized.headache.utils.extention.capitalize
import java.util.Date
import java.util.Locale

@Stable
data class MonthSummaryItemUio(
    val date: Date,
    val days: Int,
    val pills: List<MonthSummaryPillItemUio>,
)

@Stable
object MonthSummaryItemDefault {

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

    @Stable
    val spacing: DpSize = DpSize(width = 4.dp, height = 2.dp)

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

@Composable
fun MonthSummaryItem(
    modifier: Modifier = Modifier,
    padding: PaddingValues = MonthSummaryItemDefault.padding,
    spacing: DpSize = MonthSummaryItemDefault.spacing,
    formatter: SimpleDateFormat = MonthSummaryItemDefault.formatter,
    item: MonthSummaryItemUio,
    onItem: (MonthSummaryItemUio) -> Unit,
) {
    Column(
        modifier = Modifier
            .clickable { onItem(item) }
            .fillMaxWidth()
            .padding(paddingValues = padding)
            .then(other = modifier),
        verticalArrangement = Arrangement.spacedBy(space = spacing.height)
    ) {
        Row(
            horizontalArrangement = Arrangement.spacedBy(space = spacing.width)
        ) {
            Text(
                modifier = Modifier.alignByBaseline(),
                style = MaterialTheme.typography.titleLarge,
                text = remember(formatter, item.date) { formatter.format(item.date) }.capitalize(),
            )
            Text(
                modifier = Modifier.alignByBaseline(),
                style = MaterialTheme.typography.titleLarge,
                text = "-",
            )
            Text(
                modifier = Modifier.alignByBaseline(),
                style = MaterialTheme.typography.titleMedium,
                text = "${item.days} jours de migraine.",
            )
        }
        Column {
            item.pills.forEach {
                MonthSummaryPillItem(
                    item = it,
                )
            }
        }
    }
}

@Composable
@Preview
private fun MonthSummaryItemPreview(
    @PreviewParameter(PreviewProvider::class) preview: MonthSummaryItemUio,
) {
    HeadacheTheme {
        Surface {
            MonthSummaryItem(
                modifier = Modifier.fillMaxWidth(),
                item = preview,
                onItem = { },
            )
        }
    }
}

private class PreviewProvider() : PreviewParameterProvider<MonthSummaryItemUio> {
    override val values: Sequence<MonthSummaryItemUio>
        get() = sequenceOf(
            MonthSummaryItemUio(
                date = Date(),
                days = 11,
                pills = listOf(
                    MonthSummaryPillItemUio(
                        label = "Paracétamol 1000",
                        amount = 2,
                        color = HeadacheColorPalette.Pill.Paracetamol1000,
                    ),
                    MonthSummaryPillItemUio(
                        label = "Ibuprofen 400",
                        amount = 4,
                        color = HeadacheColorPalette.Pill.Ibuprofene400,
                    ),
                    MonthSummaryPillItemUio(
                        label = "Spifen 400",
                        amount = 6,
                        color = HeadacheColorPalette.Pill.Spifen400,
                    ),
                    MonthSummaryPillItemUio(
                        label = "Eletripan 40",
                        amount = 0,
                        color = HeadacheColorPalette.Pill.Eletriptan40,
                    ),
                ),
            )
        )
}