package com.pixelized.headache.ui.page.calendar

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.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.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.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.pixelized.headache.ui.theme.HeadacheTheme

@Stable
data class CalendarItemUio(
    val id: Long,
    val name: String,
    val type: String,
    val account: String,
    val color: Color,
    val used: Boolean,
)

@Stable
object CalendarItemDefault {

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

    @Stable
    val spacing: Dp = 8.dp
}

@Composable
fun CalendarItem(
    modifier: Modifier = Modifier,
    padding: PaddingValues = CalendarItemDefault.padding,
    spacing: Dp = CalendarItemDefault.spacing,
    item: CalendarItemUio,
    onItem: (CalendarItemUio) -> Unit,
) {
    Row(
        modifier = Modifier
            .clickable { onItem(item) }
            .minimumInteractiveComponentSize()
            .padding(padding)
            .then(other = modifier),
        horizontalArrangement = Arrangement.spacedBy(space = spacing),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Box(
            modifier = Modifier
                .background(color = item.color)
                .size(24.dp),
        )
        Text(
            modifier = Modifier.weight(weight = 1f),
            style = MaterialTheme.typography.bodyLarge,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
            text = item.name,
        )
        Switch(
            checked = item.used,
            onCheckedChange = null,
        )
    }
}

@Composable
@Preview
private fun CalendarItemPreview() {
    HeadacheTheme {
        Surface {
            CalendarItem(
                item = CalendarItemUio(
                    id = 3,
                    name = "Céphalée",
                    type = "",
                    account = "andres.gomez.thomas@gmail.com",
                    color = Color.Red,
                    used = false,
                ),
                onItem = { },
            )
        }
    }
}