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

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.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.composables.icons.lucide.ChevronDown
import com.composables.icons.lucide.Lucide
import com.composeunstyled.UnstyledButton
import com.composeunstyled.UnstyledIcon
import dev.ulfrx.recipe.ui.theme.RecipeTheme
import kotlinx.datetime.LocalDate

/**
 * Pill button showing the visible period label. Tapping jumps to today and
 * selects it. Optional chevron at the end toggles week/month when [expandable]
 * is set; the chevron is hidden otherwise so popup variants get a clean pill.
 */
@Composable
internal fun CalendarTopbar(
    mode: CalendarMode,
    anchor: LocalDate,
    today: LocalDate,
    selectedDate: LocalDate,
    locale: CalendarLocale,
    onJumpToToday: () -> Unit,
    expandable: Boolean,
    onToggleMode: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val colors = RecipeTheme.colors
    val onToday = selectedDate == today && isInVisiblePeriod(today, anchor, mode)

    Row(
        modifier = modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.End,
        verticalAlignment = Alignment.CenterVertically,
    ) {
        UnstyledButton(
            onClick = onJumpToToday,
            enabled = !onToday,
            backgroundColor = Color.Transparent,
            contentColor = colors.content,
            shape = CircleShape,
            borderColor = colors.separator,
            borderWidth = 1.dp,
            contentPadding = PaddingValues(horizontal = 12.dp, vertical = 6.dp),
            modifier = Modifier.defaultMinSize(minHeight = 32.dp),
        ) {
            BasicText(
                text = formatPeriodLabel(mode, anchor, locale),
                style =
                    RecipeTheme.typography.label.copy(
                        color = if (onToday) colors.contentMuted else colors.content,
                    ),
                modifier = if (onToday) Modifier.alpha(0.6f) else Modifier,
            )
        }
        if (expandable) {
            Spacer(modifier = Modifier.size(8.dp))
            UnstyledButton(
                onClick = onToggleMode,
                backgroundColor = Color.Transparent,
                contentColor = colors.content,
                shape = CircleShape,
                borderColor = colors.separator,
                borderWidth = 1.dp,
                contentPadding = PaddingValues(6.dp),
                modifier = Modifier.size(32.dp),
            ) {
                Box(contentAlignment = Alignment.Center, modifier = Modifier.fillMaxWidth()) {
                    UnstyledIcon(
                        imageVector = Lucide.ChevronDown,
                        contentDescription = null,
                        tint = colors.contentMuted,
                        modifier =
                            Modifier
                                .size(14.dp)
                                .rotate(if (mode == CalendarMode.Month) 180f else 0f),
                    )
                }
            }
        }
    }
}
