package dev.ulfrx.recipe.ui.screens.mealplaneditor

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
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.ArrowLeft
import com.composables.icons.lucide.Lucide
import com.composables.icons.lucide.Plus
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.components.sheet.BlockSheetDismiss
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_back_a11y
import recipe.composeapp.generated.resources.meal_plan_editor_confirm_a11y
import recipe.composeapp.generated.resources.meal_plan_editor_not_found

@Composable
internal fun MealPlanEditorScreen(
    viewModel: MealPlanEditorViewModel,
    onBack: () -> Unit,
    onConfirm: (PlannedMealUi) -> 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 MealPlanEditorState.Editing -> {
                    BlockSheetDismiss()
                    GlassBackdropSource(state = backdrop, modifier = Modifier.fillMaxSize()) {
                        MealPlanEditorContent(
                            editing = s,
                            catalog = sampleAddableIngredients,
                            topChromeInset = TopActionsTopInset,
                            topChromeHeight = TopPillHeight,
                            onSelectDate = viewModel::selectDate,
                            onSetCalendarExpanded = viewModel::setCalendarExpanded,
                            onSelectSlot = viewModel::selectSlot,
                            onSetServings = viewModel::setServings,
                            onSelectSubstitution = viewModel::selectSubstitution,
                            onRemoveRecipeIngredient = viewModel::removeRecipeIngredient,
                            onRemoveAddedIngredient = viewModel::removeAddedIngredient,
                            onRestoreRemoved = viewModel::restoreRemovedIngredients,
                            onAddIngredient = viewModel::addIngredient,
                        )
                    }

                    EditorChromeRow(
                        showConfirm = true,
                        onBack = onBack,
                        onConfirm = { viewModel.confirm()?.let(onConfirm) },
                        modifier =
                            Modifier
                                .align(Alignment.TopCenter)
                                .fillMaxWidth()
                                .padding(top = TopActionsTopInset, start = spacing.lg, end = spacing.lg),
                    )
                }

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

                    EditorChromeRow(
                        showConfirm = false,
                        onBack = onBack,
                        onConfirm = {},
                        modifier =
                            Modifier
                                .align(Alignment.TopCenter)
                                .fillMaxWidth()
                                .padding(top = TopActionsTopInset, start = spacing.lg, end = spacing.lg),
                    )
                }
            }
        }
    }
}

@Composable
private fun EditorChromeRow(
    showConfirm: Boolean,
    onBack: () -> Unit,
    onConfirm: () -> Unit,
    modifier: Modifier = Modifier,
) {
    Row(
        modifier = modifier,
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically,
    ) {
        CircleGlassButton(
            onClick = onBack,
            icon = Lucide.ArrowLeft,
            contentDescription = stringResource(Res.string.meal_plan_editor_back_a11y),
            size = TopPillHeight,
            iconSize = TopActionIconSize,
            glassStyle = RecipeTheme.glass.button,
        )
        if (showConfirm) {
            CircleGlassButton(
                onClick = onConfirm,
                icon = Lucide.Plus,
                contentDescription = stringResource(Res.string.meal_plan_editor_confirm_a11y),
                size = TopPillHeight,
                iconSize = TopActionIconSize,
                glassStyle = RecipeTheme.glass.button,
            )
        }
    }
}

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