package com.pixelized.desktop.lwa.ui.theme

import androidx.compose.material.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 androidx.compose.runtime.remember
import com.pixelized.desktop.lwa.ui.theme.color.LwaColors
import com.pixelized.desktop.lwa.ui.theme.color.darkLwaColorTheme
import com.pixelized.desktop.lwa.ui.theme.dimen.LwaDimen
import com.pixelized.desktop.lwa.ui.theme.dimen.lwaDimen
import com.pixelized.desktop.lwa.ui.theme.shapes.LwaShapes
import com.pixelized.desktop.lwa.ui.theme.shapes.lwaShapes
import com.pixelized.desktop.lwa.ui.theme.typography.LwaTypography
import com.pixelized.desktop.lwa.ui.theme.typography.lwaTypography

val LocalLwaTheme = compositionLocalOf<LwaTheme> {
    error("Local Snack Controller is not yet ready")
}

val MaterialTheme.lwa: LwaTheme
    @Stable
    @Composable
    @ReadOnlyComposable
    get() = LocalLwaTheme.current

@Stable
data class LwaTheme(
    val colorScheme: LwaColors,
    val typography: LwaTypography,
    val shapes: LwaShapes,
    val dimen: LwaDimen,
)

@Composable
fun LwaTheme(
    content: @Composable () -> Unit,
) {
    val lwaColors = darkLwaColorTheme()
    val lwaTypography = lwaTypography(colors = lwaColors)
    val lwaShapes = lwaShapes()
    val lwaDimen = lwaDimen()

    val theme = remember(lwaColors, lwaTypography, lwaShapes, lwaDimen) {
        LwaTheme(
            colorScheme = lwaColors,
            typography = lwaTypography,
            shapes = lwaShapes,
            dimen = lwaDimen,
        )
    }

    CompositionLocalProvider(
        LocalLwaTheme provides theme
    ) {
        MaterialTheme(
            colors = lwaColors.base,
            typography = lwaTypography.system,
            shapes = lwaShapes.base,
            content = content,
        )
    }
}