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

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.Dp
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

/**
 * Scaffold for a bottom-anchored modal overlay (calendar pill today; future
 * bottom-sheets, filter panels). Owns four crosscuts so screens don't repeat
 * them:
 *  - **Local glass backdrop** — Liquid refraction filters the nearest
 *    liquefiable ancestor, so the overlay must be a sibling of its own
 *    backdrop source (not a descendant of the shell's global one).
 *  - **Scrim** — tap-outside dismisses while [open] is true.
 *  - **Tab/route exit** — closes the overlay on dispose to keep state honest
 *    when the user navigates away mid-open.
 *  - **Active-tab tap** — registers with [OverlayDismisser] so a tap on the
 *    already-active tab in the shell closes us too.
 */
@Composable
fun BottomOverlayScaffold(
    open: Boolean,
    onDismiss: () -> Unit,
    bottomInset: Dp,
    modifier: Modifier = Modifier,
    overlay: @Composable () -> Unit,
    content: @Composable () -> Unit,
) {
    val backdrop = rememberGlassBackdropState()
    val latestOnDismiss by rememberUpdatedState(onDismiss)
    val latestOpen by rememberUpdatedState(open)

    DisposableEffect(Unit) {
        onDispose { if (latestOpen) latestOnDismiss() }
    }

    RegisterDismissibleOverlay(active = open, onDismiss = onDismiss)

    CompositionLocalProvider(LocalGlassBackdropState provides backdrop) {
        Box(modifier = modifier.fillMaxSize()) {
            GlassBackdropSource(state = backdrop, modifier = Modifier.fillMaxSize()) {
                Box(modifier = Modifier.fillMaxSize().background(RecipeTheme.colors.background)) {
                    content()
                }
            }

            if (open) {
                Box(
                    modifier =
                        Modifier
                            .fillMaxSize()
                            .pointerInput(Unit) {
                                detectTapGestures { onDismiss() }
                            },
                )
            }

            Box(
                modifier =
                    Modifier
                        .align(Alignment.BottomCenter)
                        .fillMaxWidth()
                        .padding(horizontal = RecipeTheme.spacing.xl)
                        .padding(bottom = bottomInset),
            ) {
                overlay()
            }
        }
    }
}
