package com.anypreta.bountyboard.ui.theme

import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Shapes
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.unit.dp

val LocalBBPalette: ProvidableCompositionLocal<BBPalette> =
    staticCompositionLocalOf { LightPalette }

/** Convenience accessor: `BB.colors.accent`. */
object BB {
    val colors: BBPalette
        @Composable get() = LocalBBPalette.current
}

@Composable
fun BountyBoardTheme(
    theme: BBTheme = BBTheme.Light,
    content: @Composable () -> Unit,
) {
    val p = paletteFor(theme)
    val radius = p.radius.dp

    val scheme = if (p.isDark) {
        darkColorScheme(
            primary = p.accent,
            onPrimary = p.onAccent,
            secondary = p.accent,
            onSecondary = p.onAccent,
            background = p.bg,
            onBackground = p.text,
            surface = p.surface,
            onSurface = p.text,
            surfaceVariant = p.surface2,
            onSurfaceVariant = p.muted,
            outline = p.border,
            outlineVariant = p.border,
            error = p.err,
            onError = androidx.compose.ui.graphics.Color.White,
            primaryContainer = p.surface2,
            onPrimaryContainer = p.text,
            surfaceContainer = p.surface2,
            surfaceContainerHigh = p.surface2,
            surfaceContainerHighest = p.surface2,
            surfaceContainerLow = p.surface,
            surfaceContainerLowest = p.bg,
            scrim = androidx.compose.ui.graphics.Color(0xAA000000),
        )
    } else {
        lightColorScheme(
            primary = p.accent,
            onPrimary = p.onAccent,
            secondary = p.accent,
            onSecondary = p.onAccent,
            background = p.bg,
            onBackground = p.text,
            surface = p.surface,
            onSurface = p.text,
            surfaceVariant = p.surface2,
            onSurfaceVariant = p.muted,
            outline = p.border,
            outlineVariant = p.border,
            error = p.err,
            onError = androidx.compose.ui.graphics.Color.White,
            primaryContainer = p.surface2,
            onPrimaryContainer = p.text,
            surfaceContainer = p.surface2,
            surfaceContainerHigh = p.surface2,
            surfaceContainerHighest = p.surface2,
            surfaceContainerLow = p.surface,
            surfaceContainerLowest = p.bg,
            scrim = androidx.compose.ui.graphics.Color(0x88000000),
        )
    }

    val shapes = Shapes(
        extraSmall = RoundedCornerShape(radius),
        small = RoundedCornerShape(radius),
        medium = RoundedCornerShape(radius),
        large = RoundedCornerShape(radius),
        extraLarge = RoundedCornerShape(radius),
    )

    CompositionLocalProvider(LocalBBPalette provides p) {
        MaterialTheme(
            colorScheme = scheme,
            typography = BBTypography,
            shapes = shapes,
            content = content,
        )
    }
}
