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.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.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.unit.dp
import com.composables.icons.lucide.Clock
import com.composables.icons.lucide.Lucide
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.sample_recipe

@Composable
internal fun RecipeDetailHero(
    cookingMinutes: Int,
    modifier: Modifier = Modifier,
) {
    val spacing = RecipeTheme.spacing

    Column(
        modifier =
            modifier
                .fillMaxWidth()
                .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))

        Row(modifier = Modifier.fillMaxWidth()) {
            MetaChip(
                icon = Lucide.Clock,
                text = stringResource(Res.string.recipe_card_minutes_format, cookingMinutes),
            )
        }
    }
}

@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),
        )
    }
}

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)

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
