package dev.ulfrx.recipe.ui.screens.shell.dock

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.times
import androidx.compose.ui.util.lerp
import dev.ulfrx.recipe.ui.components.glass.GlassSurface
import dev.ulfrx.recipe.ui.theme.RecipeTheme
import kotlin.math.roundToInt

private val PressOverlayVerticalInset = 0.dp
private val ActiveIndicatorVerticalInset = 5.dp
private const val PRESS_OVERLAY_SCALE = 1.22f

@Composable
internal fun DockSubstrate(cornerRadius: Dp) {
    GlassSurface(
        modifier = Modifier.fillMaxSize(),
        cornerRadius = cornerRadius,
        recordAsSource = true,
    ) {}
}

@Composable
internal fun DockActiveIndicatorLayer(
    activeIndex: Int,
    tabBounds: Map<Int, TabBounds>,
    dockWidthPx: Float,
    alpha: Float,
) {
    val bounds = tabBounds[activeIndex] ?: return
    if (alpha <= 0f || dockWidthPx <= 0f) return

    val density = LocalDensity.current
    val bbox = activeIndicatorBboxFor(bounds, dockWidthPx, density)

    Box(
        modifier =
            Modifier
                .offset { IntOffset(bbox.leftPx.roundToInt(), 0) }
                .width(with(density) { bbox.widthPx.toDp() })
                .fillMaxHeight()
                .padding(vertical = ActiveIndicatorVerticalInset)
                .alpha(alpha)
                .background(
                    color = RecipeTheme.colors.chromeActive,
                    shape = RoundedCornerShape(50),
                ),
    )
}

@Composable
internal fun DockPressOverlayLayer(
    overlayCenterX: Float,
    overlayWidthPx: Float,
    overlayAlpha: Float,
    overlayPeakProgress: Float,
    dockWidthPx: Float,
    dockHeight: Dp,
) {
    if (overlayAlpha <= 0f || dockWidthPx <= 0f || overlayWidthPx <= 0f) return

    val density = LocalDensity.current
    val dockHeightPx = with(density) { dockHeight.toPx() }
    val activeInsetPx = with(density) { ActiveIndicatorVerticalInset.toPx() }
    val activeStartScaleY = if (dockHeightPx > 0f) (dockHeightPx - 2 * activeInsetPx) / dockHeightPx else 1f
    val scaleX = lerp(1f, PRESS_OVERLAY_SCALE, overlayPeakProgress)
    val scaleY = lerp(activeStartScaleY, PRESS_OVERLAY_SCALE, overlayPeakProgress)

    val cornerRadius = (dockHeight - 2 * PressOverlayVerticalInset) / 2
    val leftPx = overlayCenterX - overlayWidthPx / 2f
    GlassSurface(
        modifier =
            Modifier
                .offset { IntOffset(leftPx.roundToInt(), 0) }
                .width(with(density) { overlayWidthPx.toDp() })
                .fillMaxHeight()
                .padding(vertical = PressOverlayVerticalInset)
                .graphicsLayer {
                    this.scaleX = scaleX
                    this.scaleY = scaleY
                }.alpha(overlayAlpha),
        cornerRadius = cornerRadius,
        glassStyle = RecipeTheme.glass.dockPress,
    ) {}
}
