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

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.Spacer
import androidx.compose.foundation.layout.aspectRatio
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.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
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.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.composables.icons.lucide.Calendar
import com.composables.icons.lucide.Clock
import com.composables.icons.lucide.Lucide
import com.composeunstyled.UnstyledButton
import com.composeunstyled.UnstyledIcon
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_plan_button
import recipe.composeapp.generated.resources.sample_recipe

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

    Column(
        modifier =
            modifier
                .fillMaxWidth()
                .padding(
                    top = HERO_TOP_PADDING,
                    bottom = spacing.lg,
                    start = spacing.lg,
                    end = spacing.lg,
                ),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        Image(
            painter = painterResource(Res.drawable.sample_recipe),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier =
                Modifier
                    .fillMaxWidth()
                    .aspectRatio(BANNER_ASPECT_RATIO)
                    .shadow(
                        elevation = BANNER_SHADOW_ELEVATION,
                        shape = RoundedCornerShape(BANNER_CORNER),
                        ambientColor = BANNER_SHADOW_COLOR,
                        spotColor = BANNER_SHADOW_COLOR,
                    )
                    .clip(RoundedCornerShape(BANNER_CORNER)),
        )

        Spacer(Modifier.height(spacing.lg))

        BasicText(
            text = title,
            style =
                typography.display.copy(
                    color = colors.content,
                    fontSize = TITLE_FONT_SIZE,
                    lineHeight = TITLE_LINE_HEIGHT,
                    fontWeight = FontWeight.Bold,
                    textAlign = TextAlign.Center,
                ),
        )

        Spacer(Modifier.height(spacing.lg))

        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(spacing.sm),
            ) {
                MetaChip(
                    icon = Lucide.Clock,
                    text = stringResource(Res.string.recipe_card_minutes_format, cookingMinutes),
                )
            }
            PlanButton(
                text = stringResource(Res.string.recipe_detail_plan_button),
                onClick = onPlanClick,
            )
        }
    }
}

@Composable
private fun MetaChip(
    icon: ImageVector,
    text: String,
) {
    val colors = RecipeTheme.colors
    Row(
        modifier =
            Modifier
                .clip(CHIP_SHAPE)
                .background(colors.surface)
                .border(1.dp, colors.separator, CHIP_SHAPE)
                .padding(horizontal = CHIP_PADDING_H, vertical = CHIP_PADDING_V),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(CHIP_ICON_GAP),
    ) {
        UnstyledIcon(
            imageVector = icon,
            contentDescription = null,
            tint = colors.contentMuted,
            modifier = Modifier.size(CHIP_ICON_SIZE),
        )
        BasicText(
            text = text,
            style = RecipeTheme.typography.label.copy(color = colors.contentMuted),
        )
    }
}

@Composable
private fun PlanButton(
    text: String,
    onClick: () -> Unit,
) {
    val colors = RecipeTheme.colors
    UnstyledButton(
        onClick = onClick,
        shape = CHIP_SHAPE,
        backgroundColor = colors.accent,
        contentColor = colors.surface,
        contentPadding = PaddingValues(horizontal = PLAN_PADDING_H, vertical = PLAN_PADDING_V),
    ) {
        UnstyledIcon(
            imageVector = Lucide.Calendar,
            contentDescription = null,
            tint = colors.surface,
            modifier = Modifier.size(CHIP_ICON_SIZE),
        )
        Spacer(Modifier.width(CHIP_ICON_GAP))
        BasicText(
            text = text,
            style =
                RecipeTheme.typography.label.copy(
                    color = colors.surface,
                    fontWeight = FontWeight.Bold,
                ),
        )
    }
}

private const val BANNER_ASPECT_RATIO = 16f / 9f
private val BANNER_CORNER = 20.dp
private val BANNER_SHADOW_ELEVATION = 14.dp
private val BANNER_SHADOW_COLOR = Color.Black.copy(alpha = 0.45f)

// Leave room for the sheet handle (8dp top padding + 5dp handle) plus breathing room.
private val HERO_TOP_PADDING = 32.dp

private val TITLE_FONT_SIZE = 24.sp
private val TITLE_LINE_HEIGHT = 28.sp

private val CHIP_SHAPE = RoundedCornerShape(percent = 50)
private val CHIP_PADDING_H = 12.dp
private val CHIP_PADDING_V = 7.dp
private val CHIP_ICON_SIZE = 14.dp
private val CHIP_ICON_GAP = 5.dp

// Plan button is slightly more padded than meta chips so it reads as a CTA, not just a coloured chip.
private val PLAN_PADDING_H = 14.dp
private val PLAN_PADDING_V = 9.dp
