package cl.homelogic.platform.designsystem.themes

import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.platform.LocalHapticFeedback
import cl.homelogic.platform.designsystem.foundations.HapticSystem
import cl.homelogic.platform.designsystem.foundations.ManropeTypography

val LocalDesignSystem = staticCompositionLocalOf<DesignSystemManager> {
    error("DesignSystemManager not provided")
}

@Composable
fun DesignSystem(
    designSystemManager: DesignSystemManager,
    content: @Composable () -> Unit,
) {
    val state = designSystemManager.state.collectAsState().value

    HapticSystem(
        hapticFeedback = LocalHapticFeedback.current,
    ) {
        MaterialTheme(
            colorScheme = when (state.darkMode) {
                DarkModeState.Disabled -> state.theme.definition.lightVariant
                DarkModeState.Enabled -> state.theme.definition.darkVariant
                DarkModeState.Oled -> state.theme.definition.darkVariant
            },
            typography = ManropeTypography(),
        ) {
            CompositionLocalProvider(
                LocalDesignSystem provides designSystemManager,
            ) {
                content()
            }
        }
    }
}
