package com.example.wabi.ui.theme

import android.util.Log
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.ReadOnlyComposable
import com.example.wabi.models.system.ColorPalette
import com.example.wabi.models.system.Type
import com.example.wabi.view.ui.theme.DarkColorScheme
import com.example.wabi.view.ui.theme.LightColorScheme
import com.example.wabi.view.ui.theme.LocalColors

//объект для обновления состояния провайдера
object WabiTheme {
    val fonts: Type
        @ReadOnlyComposable @Composable get() = LocalFonts.current

    val colors: ColorPalette
        @ReadOnlyComposable @Composable get() = LocalColors.current
}

///Функция для вызова темы
@Composable
fun WabiTheme(
    themeState: Int = ThemeMode.System,
    fonts: Type = WabiTheme.fonts,
    content: @Composable () -> Unit
) {

    Log.d("ChangeTheme -> ", "Theme state $themeState")

    val colors = when (themeState) {
        ThemeMode.Light -> LightColorScheme
        ThemeMode.Dark -> DarkColorScheme
        else -> if (isSystemInDarkTheme()) {
            DarkColorScheme
        } else {
            LightColorScheme
        }
    }

    LaunchedEffect(key1 = colors) {
        Log.d("ChangeTheme -> ", "Colors changed")
    }

    CompositionLocalProvider(
        LocalColors provides colors, LocalFonts provides fonts, content = content
    )
}

data object ThemeMode {
    const val System = 0
    const val Light = 1
    const val Dark = 2
}