package dev.ulfrx.recipe.ui.screens.search.catalog

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.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
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.layout.statusBars
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
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.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.composables.icons.lucide.Clock
import com.composables.icons.lucide.Flame
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_kcal_format
import recipe.composeapp.generated.resources.recipe_card_minutes_format
import recipe.composeapp.generated.resources.sample_recipe

@Composable
fun RecipeCatalogGrid(
    state: RecipeCatalogState,
    onRecipeClick: (String) -> Unit,
    modifier: Modifier = Modifier,
    gridState: LazyGridState = rememberLazyGridState(),
) {
    val spacing = RecipeTheme.spacing
    val statusBarTop = WindowInsets.statusBars.asPaddingValues().calculateTopPadding()

    LazyVerticalGrid(
        columns = GridCells.Adaptive(minSize = MinRecipeCardWidth),
        state = gridState,
        modifier = modifier.fillMaxSize(),
        contentPadding =
            PaddingValues(
                start = spacing.lg,
                end = spacing.lg,
                top = statusBarTop + spacing.lg,
                bottom = GridBottomPadding,
            ),
        horizontalArrangement = Arrangement.spacedBy(spacing.sm),
        verticalArrangement = Arrangement.spacedBy(spacing.sm),
    ) {
        items(state.cards, key = { it.id }) { card ->
            RecipeCard(card = card, onClick = { onRecipeClick(card.id) })
        }
    }
}

@Composable
private fun RecipeCard(
    card: RecipeCardUi,
    onClick: () -> Unit,
) {
    val colors = RecipeTheme.colors
    val typography = RecipeTheme.typography
    val cardShape = RoundedCornerShape(CardCornerRadius)

    UnstyledButton(
        onClick = onClick,
        backgroundColor = colors.surface,
        contentColor = colors.content,
        shape = cardShape,
        borderColor = Color.Transparent,
        borderWidth = 0.dp,
        modifier =
            Modifier
                .fillMaxWidth()
                .height(CardHeight)
                .shadow(elevation = CardElevation, shape = cardShape, clip = false)
                .clip(cardShape),
    ) {
        Column(modifier = Modifier.fillMaxSize()) {
            RecipeThumbnail()
            Column(
                modifier =
                    Modifier
                        .fillMaxWidth()
                        .weight(1f)
                        .padding(CardContentPadding),
            ) {
                BasicText(
                    text = card.title,
                    style =
                        typography.body.copy(
                            color = colors.content,
                            fontWeight = FontWeight.SemiBold,
                            fontSize = CardTitleTextSize,
                            lineHeight = CardTitleLineHeight,
                        ),
                    maxLines = 3,
                    overflow = TextOverflow.Ellipsis,
                )
                Spacer(Modifier.weight(1f))
                RecipeMetaRow(card = card)
            }
        }
    }
}

@Composable
private fun RecipeThumbnail() {
    Box(
        modifier =
            Modifier
                .fillMaxWidth()
                .height(ThumbnailHeight),
    ) {
        ThumbnailPlaceholder()
        Image(
            painter = painterResource(Res.drawable.sample_recipe),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier.fillMaxSize(),
        )
    }
}

@Composable
private fun ThumbnailPlaceholder() {
    Box(
        modifier =
            Modifier
                .fillMaxSize()
                .background(RecipeTheme.colors.surfaceGlass),
    )
}

@Composable
private fun RecipeMetaRow(card: RecipeCardUi) {
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically,
    ) {
        MetaItem(
            icon = Lucide.Clock,
            label = stringResource(Res.string.recipe_card_minutes_format, card.cookingMinutes),
        )
        MetaItem(
            icon = Lucide.Flame,
            label = stringResource(Res.string.recipe_card_kcal_format, card.kcal),
        )
    }
}

@Composable
private fun MetaItem(
    icon: ImageVector,
    label: String,
) {
    val colors = RecipeTheme.colors
    Row(
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(RecipeTheme.spacing.xs),
    ) {
        UnstyledIcon(
            imageVector = icon,
            contentDescription = null,
            tint = colors.contentMuted,
            modifier = Modifier.size(MetaIconSize),
        )
        BasicText(
            text = label,
            style =
                RecipeTheme.typography.label.copy(
                    color = colors.contentMuted,
                    fontSize = CardMetaTextSize,
                    lineHeight = CardMetaLineHeight,
                ),
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
        )
    }
}

private val MinRecipeCardWidth = 104.dp
private val GridBottomPadding = 96.dp
private val CardHeight = 190.dp
private val ThumbnailHeight = 87.dp
private val CardCornerRadius = 17.dp
private val CardContentPadding = 10.dp
private val CardElevation = 3.dp
private val MetaIconSize = 11.dp
private val CardTitleTextSize = 11.sp
private val CardTitleLineHeight = 14.sp
private val CardMetaTextSize = 10.sp
private val CardMetaLineHeight = 13.sp
