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

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.composables.icons.lucide.Calendar
import com.composables.icons.lucide.Lucide
import com.composables.icons.lucide.X
import dev.ulfrx.recipe.ui.components.glass.CircleGlassButton
import dev.ulfrx.recipe.ui.components.glass.GlassBackdropSource
import dev.ulfrx.recipe.ui.components.glass.LocalGlassBackdropState
import dev.ulfrx.recipe.ui.components.glass.rememberGlassBackdropState
import dev.ulfrx.recipe.ui.theme.RecipeTheme
import org.jetbrains.compose.resources.stringResource
import recipe.composeapp.generated.resources.Res
import recipe.composeapp.generated.resources.meal_plan_editor_title_a11y
import recipe.composeapp.generated.resources.recipe_detail_close_a11y
import recipe.composeapp.generated.resources.recipe_detail_not_found

@Composable
internal fun RecipeDetailScreen(
    viewModel: RecipeDetailViewModel,
    onPlan: (RecipeDetailState.Ready) -> Unit,
    onClose: () -> Unit,
) {
    val backdrop = rememberGlassBackdropState()
    val state by viewModel.state.collectAsStateWithLifecycle()
    val spacing = RecipeTheme.spacing

    CompositionLocalProvider(LocalGlassBackdropState provides backdrop) {
        Box(
            modifier =
                Modifier
                    .fillMaxSize()
                    .background(RecipeTheme.colors.background),
        ) {
            when (val s = state) {
                is RecipeDetailState.Ready -> {
                    GlassBackdropSource(state = backdrop, modifier = Modifier.fillMaxSize()) {
                        RecipeDetailContent(
                            ready = s,
                            onServingsChange = viewModel::setServings,
                            onSelectSubstitution = viewModel::selectSubstitution,
                            topChromeInset = TopActionsTopInset,
                            topChromeHeight = TopPillHeight,
                        )
                    }

                    CircleGlassButton(
                        onClick = { onPlan(s) },
                        icon = Lucide.Calendar,
                        contentDescription = stringResource(Res.string.meal_plan_editor_title_a11y),
                        size = TopPillHeight,
                        iconSize = TopActionIconSize,
                        glassStyle = RecipeTheme.glass.button,
                        modifier =
                            Modifier
                                .align(Alignment.TopEnd)
                                .padding(top = TopActionsTopInset, end = spacing.lg),
                    )
                }

                RecipeDetailState.NotFound -> {
                    BasicText(
                        text = stringResource(Res.string.recipe_detail_not_found),
                        style = RecipeTheme.typography.body,
                        modifier = Modifier.align(Alignment.Center),
                    )
                }
            }

            CircleGlassButton(
                onClick = onClose,
                icon = Lucide.X,
                contentDescription = stringResource(Res.string.recipe_detail_close_a11y),
                size = TopPillHeight,
                iconSize = TopActionIconSize,
                glassStyle = RecipeTheme.glass.button,
                modifier =
                    Modifier
                        .align(Alignment.TopStart)
                        .padding(top = TopActionsTopInset, start = spacing.lg),
            )
        }
    }
}

private val TopPillHeight = 44.dp
private val TopActionIconSize = 18.dp
private val TopActionsTopInset = 28.dp
