package mg.dot.feufaro

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import mg.dot.feufaro.config.AppConfigJson
import mg.dot.feufaro.config.DisplayConfig
import kotlinx.serialization.json.Json
import kotlin.String

import kotlinx.serialization.Serializable

@Serializable
data class AppConfigJson(
    val themeMode: String? = null,
    val fontSize: Float? = null,
    val playlist: List<String>? = null,
    val buttonContainerColorHex: String? = null,
    val buttonContentColorHex: String? = null,
    val buttonDisabledContainerColorHex: String? = null,
    val buttonDisabledContentColorHex: String? = null
)

fun AppConfigJson.toDisplayConfig(defaultConfig: DisplayConfig): DisplayConfig {
    return DisplayConfig(
        themeMode = this.themeMode,
        fontSize = this.fontSize,
        playlist = this.playlist,
        buttonContainerColorHex = this.buttonContainerColorHex,
        buttonContentColorHex = this.buttonContentColorHex,
        buttonDisabledContainerColorHex = this.buttonDisabledContainerColorHex,
        buttonDisabledContentColorHex = this.buttonDisabledContentColorHex
    )
}
class DisplayConfigManager(
    private val fileRepository: FileRepository
)
{
    private val _displayConfig = MutableStateFlow(
        DisplayConfig(
            themeMode = "SYSTEM",
            fontSize = 16f,
            playlist = listOf(),
            buttonContainerColorHex = "#FF000000",
            buttonContentColorHex = "#FFFFFFFF",
            buttonDisabledContainerColorHex = "#FF888888",
            buttonDisabledContentColorHex = "#FFAA6666"

    ),
    )
    val displayConfig: StateFlow<DisplayConfig> = _displayConfig.asStateFlow()

    suspend fun loadConfigFromFile(filePath: String) {
        try {
            val jsonString = fileRepository.readFileContent(filePath)
            val appConfigJson = Json.decodeFromString<AppConfigJson>(jsonString)
            _displayConfig.value = appConfigJson.toDisplayConfig(_displayConfig.value)

        } catch (e: Exception) {
            println("DisplayConfigManager Error 22: ${e.message}")
            e.printStackTrace()
        }
    }
    // NOUVELLE MÉTHODE : Pour basculer le mode débogage
}