package dev.ulfrx.recipe.ui.components.calendar

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import dev.ulfrx.recipe.ui.theme.RecipeTheme
import kotlinx.datetime.LocalDate

/**
 * Project-default wrapping of [CalendarPill] — collapsed state shows a paged
 * week strip plus the current month's short name. Used by the planner pill,
 * the meal-plan editor's in-sheet calendar, and any other surface that wants
 * the "swipe weeks, drag to expand to a month grid" pattern.
 *
 * Callers tweak [expandDirection] / [glass] / [tint] / [plannedDates] to match
 * their host context but the layout, typography and gesture handling stay
 * unified across screens.
 */
@Composable
fun RecipeCalendarPill(
    selectedDate: LocalDate,
    expanded: Boolean,
    onExpandedChange: (Boolean) -> Unit,
    onSelectDate: (LocalDate) -> Unit,
    onSelectionShift: (LocalDate) -> Unit,
    modifier: Modifier = Modifier,
    plannedDates: Set<LocalDate> = emptySet(),
    expandDirection: CalendarPillExpandDirection = CalendarPillExpandDirection.Up,
    glass: Boolean = true,
    tint: Color = RecipeTheme.colors.surfaceGlass,
    locale: CalendarLocale = CalendarLocale.PL,
) {
    val today = remember { todayInSystemTz() }
    val dayState =
        remember(plannedDates) {
            { date: LocalDate -> DayState(indicator = date in plannedDates) }
        }
    val pillTextStyle =
        RecipeTheme.typography.label.copy(
            fontWeight = FontWeight.Light,
            fontSize = PillTextSize,
        )

    val handleDayPick: (LocalDate) -> Unit = { date ->
        onSelectDate(date)
        if (expanded) onExpandedChange(false)
    }

    CalendarPill(
        expanded = expanded,
        onExpandedChange = onExpandedChange,
        selectedDate = selectedDate,
        today = today,
        onSelectDate = handleDayPick,
        expandDirection = expandDirection,
        glass = glass,
        tint = tint,
        collapsedContent = {
            CalendarWeekStripPager(
                selectedDate = selectedDate,
                today = today,
                onSelectDate = handleDayPick,
                onSelectionShift = onSelectionShift,
                numberStyle = pillTextStyle,
                dayState = dayState,
                modifier = Modifier.weight(1f),
            )
            BasicText(
                text = locale.monthsShort[selectedDate.monthNumber - 1],
                style = pillTextStyle.copy(color = RecipeTheme.colors.contentMuted),
            )
        },
        dayState = dayState,
        modifier = modifier.fillMaxWidth(),
    )
}

private val PillTextSize = 12.sp
