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

import androidx.compose.foundation.background
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.height
import androidx.compose.foundation.layout.padding
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.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import dev.ulfrx.recipe.ui.theme.RecipeTheme
import org.jetbrains.compose.resources.stringResource
import recipe.composeapp.generated.resources.Res
import recipe.composeapp.generated.resources.nutrition_grams_format
import recipe.composeapp.generated.resources.nutrition_macro_carbs
import recipe.composeapp.generated.resources.nutrition_macro_fat
import recipe.composeapp.generated.resources.nutrition_macro_kcal
import recipe.composeapp.generated.resources.nutrition_macro_protein

data class RecipeNutritionUi(
    val kcal: Int,
    val protein: Int,
    val fat: Int,
    val carbs: Int,
)

internal fun RecipeNutritionUi.scaledBy(servings: Int) =
    RecipeNutritionUi(
        kcal = kcal * servings,
        protein = protein * servings,
        fat = fat * servings,
        carbs = carbs * servings,
    )

@Composable
fun NutritionSummary(
    nutrition: RecipeNutritionUi,
    modifier: Modifier = Modifier,
) {
    val colors = RecipeTheme.colors
    Row(
        modifier = modifier,
        horizontalArrangement = Arrangement.spacedBy(RecipeTheme.spacing.sm),
    ) {
        MacroCard(
            modifier = Modifier.weight(1f),
            value = nutrition.kcal.toString(),
            label = stringResource(Res.string.nutrition_macro_kcal),
            valueColor = colors.content,
        )
        MacroCard(
            modifier = Modifier.weight(1f),
            value = stringResource(Res.string.nutrition_grams_format, nutrition.protein),
            label = stringResource(Res.string.nutrition_macro_protein),
            valueColor = colors.macroProtein,
        )
        MacroCard(
            modifier = Modifier.weight(1f),
            value = stringResource(Res.string.nutrition_grams_format, nutrition.fat),
            label = stringResource(Res.string.nutrition_macro_fat),
            valueColor = colors.macroFat,
        )
        MacroCard(
            modifier = Modifier.weight(1f),
            value = stringResource(Res.string.nutrition_grams_format, nutrition.carbs),
            label = stringResource(Res.string.nutrition_macro_carbs),
            valueColor = colors.macroCarbs,
        )
    }
}

@Composable
private fun MacroCard(
    value: String,
    label: String,
    valueColor: Color,
    modifier: Modifier = Modifier,
) {
    val colors = RecipeTheme.colors
    Column(
        modifier =
            modifier
                .clip(RoundedCornerShape(CardCornerRadius))
                .background(colors.surface)
                .padding(vertical = RecipeTheme.spacing.sm, horizontal = RecipeTheme.spacing.xs),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        BasicText(
            text = value,
            style =
                RecipeTheme.typography.body.copy(
                    color = valueColor,
                    fontWeight = FontWeight.Bold,
                    fontSize = ValueTextSize,
                ),
        )
        Spacer(Modifier.height(RecipeTheme.spacing.xs))
        BasicText(
            text = label,
            style =
                RecipeTheme.typography.label.copy(
                    color = colors.contentMuted,
                    fontSize = LabelTextSize,
                    fontWeight = FontWeight.Normal,
                ),
        )
    }
}

private val CardCornerRadius = 12.dp
private val ValueTextSize = 16.sp
private val LabelTextSize = 11.sp
