package com.pixelized.headache.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.compositionLocalOf
import com.pixelized.headache.ui.theme.color.HeadacheColors
import com.pixelized.headache.ui.theme.color.headacheDarkColorScheme
import com.pixelized.headache.ui.theme.color.headacheLightColorScheme

val LocalHeadacheTheme = compositionLocalOf<HeadacheTheme> {
    error("Local Theme Controller is not yet ready")
}

val MaterialTheme.headache: HeadacheTheme
    @Stable
    @Composable
    @ReadOnlyComposable
    get() = LocalHeadacheTheme.current

@Stable
data class HeadacheTheme(
    val colorScheme: HeadacheColors,
)

@Composable
fun HeadacheTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit,
) {
    val theme = HeadacheTheme(
        colorScheme = when {
            darkTheme -> headacheDarkColorScheme()
            else -> headacheLightColorScheme()
        }
    )
    CompositionLocalProvider(
        LocalHeadacheTheme provides theme,
    ) {
        MaterialTheme(
            colorScheme = theme.colorScheme.base,
            typography = Typography,
            content = content
        )
    }
}