package dev.ulfrx.recipe.ui.screens.recipedetail

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.composables.icons.lucide.Clock
import com.composables.icons.lucide.Lucide
import com.composeunstyled.UnstyledIcon
import dev.ulfrx.recipe.ui.components.glass.GlassBackdropSource
import dev.ulfrx.recipe.ui.components.glass.GlassSurface
import dev.ulfrx.recipe.ui.components.glass.LocalGlassBackdropState
import dev.ulfrx.recipe.ui.components.glass.rememberGlassBackdropState
import dev.ulfrx.recipe.ui.components.recipe.RecipeServingsStepper
import dev.ulfrx.recipe.ui.theme.RecipeTheme
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import recipe.composeapp.generated.resources.Res
import recipe.composeapp.generated.resources.recipe_card_minutes_format
import recipe.composeapp.generated.resources.recipe_detail_servings_decrement_a11y
import recipe.composeapp.generated.resources.recipe_detail_servings_increment_a11y
import recipe.composeapp.generated.resources.sample_recipe

@Composable
internal fun RecipeDetailHero(
    title: String,
    cookingMinutes: Int,
    servings: Int,
    onServingsChange: (Int) -> Unit,
    modifier: Modifier = Modifier,
) {
    val colors = RecipeTheme.colors
    val typography = RecipeTheme.typography
    val spacing = RecipeTheme.spacing
    val heroBackdrop = rememberGlassBackdropState()

    Box(modifier = modifier.fillMaxWidth().height(HERO_HEIGHT)) {
        GlassBackdropSource(state = heroBackdrop, modifier = Modifier.fillMaxSize()) {
            Image(
                painter = painterResource(Res.drawable.sample_recipe),
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier.fillMaxSize().background(colors.surfaceGlass),
            )
        }
        CompositionLocalProvider(LocalGlassBackdropState provides heroBackdrop) {
            GlassSurface(
                modifier =
                    Modifier
                        .align(Alignment.BottomStart)
                        .fillMaxWidth()
                        .padding(HERO_BAND_INSET),
                cornerRadius = HERO_BAND_CORNER,
                glassStyle = RecipeTheme.glass.heroBand,
                recordAsSource = true,
                tint = RecipeTheme.colors.surfaceGlass.copy(alpha = 0.45f)
            ) {
                Column(
                    modifier =
                        Modifier.padding(
                            horizontal = HERO_BAND_PADDING_H,
                            vertical = HERO_BAND_PADDING_V,
                        ),
                ) {
                    BasicText(
                        text = title,
                        style =
                            typography.display.copy(
                                color = colors.content,
                                fontSize = TITLE_TEXT_SIZE,
                                lineHeight = TITLE_LINE_HEIGHT,
                                fontWeight = FontWeight.Bold,
                            ),
                    )
                    Spacer(Modifier.height(spacing.lg))
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.SpaceBetween,
                    ) {
                        MetaChip(
                            text = stringResource(Res.string.recipe_card_minutes_format, cookingMinutes),
                            icon = Lucide.Clock,
                        )
                        RecipeServingsStepper(
                            servings = servings,
                            servingsRange = MIN_RECIPE_SERVINGS..MAX_RECIPE_SERVINGS,
                            decrementContentDescription = stringResource(Res.string.recipe_detail_servings_decrement_a11y),
                            incrementContentDescription = stringResource(Res.string.recipe_detail_servings_increment_a11y),
                            onServingsChange = onServingsChange,
                        )
                    }
                }
            }
        }
    }
}

@Composable
private fun MetaChip(
    text: String,
    icon: ImageVector? = null,
    modifier: Modifier = Modifier,
) {
    val colors = RecipeTheme.colors
    GlassSurface(
        modifier = modifier,
        cornerRadius = CHIP_CORNER_RADIUS,
        glassStyle = RecipeTheme.glass.chipOnGlass,
        tint = RecipeTheme.colors.surfaceGlass.copy(alpha = 0.45f)
    ) {
        Row(
            modifier = Modifier.padding(horizontal = CHIP_PADDING_H, vertical = CHIP_PADDING_V),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(CHIP_GAP),
        ) {
            if (icon != null) {
                UnstyledIcon(
                    imageVector = icon,
                    contentDescription = null,
                    tint = colors.content,
                    modifier = Modifier.size(CHIP_ICON_SIZE),
                )
            }
            BasicText(
                text = text,
                style =
                    RecipeTheme.typography.body.copy(
                        color = colors.content,
                        fontWeight = FontWeight.SemiBold,
                        fontSize = CHIP_TEXT_SIZE,
                    ),
            )
        }
    }
}

private val HERO_HEIGHT = 280.dp
private val HERO_BAND_INSET = 16.dp
private val HERO_BAND_CORNER = 22.dp
private val HERO_BAND_PADDING_H = 16.dp
private val HERO_BAND_PADDING_V = 14.dp

private val CHIP_CORNER_RADIUS = 14.dp
private val CHIP_PADDING_H = 10.dp
private val CHIP_PADDING_V = 7.dp
private val CHIP_GAP = 5.dp
private val CHIP_ICON_SIZE = 11.dp
private val CHIP_TEXT_SIZE = 11.sp

private val TITLE_TEXT_SIZE = 17.sp
private val TITLE_LINE_HEIGHT = 21.sp
