package mg.dot.feufaro

import SharedScreenModel
import androidx.compose.foundation.layout.RowScope
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import mg.dot.feufaro.musicXML.MusicXML
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.koin.compose.koinInject
import cafe.adriel.voyager.navigator.CurrentScreen
import cafe.adriel.voyager.navigator.Navigator
import mg.dot.feufaro.viewmodel.SolfaScreenModel

@Composable
fun App() {

    Navigator(screen = ScreenSolfa) {
        CurrentScreen()
    }
    val fileRepository = koinInject<FileRepository>()

    val displayConfigManager = koinInject<DisplayConfigManager>()
    val currentDisplayConfig by displayConfigManager.displayConfig.collectAsState()
    // Load Configurations
    val configScope = CoroutineScope(Dispatchers.Default)
    val sharedScreenModel = koinInject<SharedScreenModel>()
    val solfaScreenModel = koinInject<SolfaScreenModel>()

    LaunchedEffect(Unit) {
        configScope.launch {
            try {
                displayConfigManager.loadConfigFromFile("assets://config.json")
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    LaunchedEffect(currentDisplayConfig) {
        if (currentDisplayConfig.playlist.isNotEmpty()) {
            if (DeepLinkHandler.hasConsumedDeepLink) {
                sharedScreenModel.setPlaylist(currentDisplayConfig.playlist)
            } else {
                if(sharedScreenModel.activeFilePath.value.isEmpty()) {
                    try {
                        sharedScreenModel.setPlaylist(currentDisplayConfig.playlist)
                        solfaScreenModel.reload()
                    } catch (e: Exception) {
                        println("Error loading playlist : ${e.message}")
                    }
                }
            }
        }
    }

    val musicXML: MusicXML = koinInject()

}


fun String?.toColor(foreGround: Boolean = false): Color {
    val defaultColor = if (foreGround) {
        "00FFFFFF"
    } else {
        "00008800"
    }
    var hex = this?.removePrefix("#") ?: defaultColor
    if (hex.length == 6) {
        hex = "00" + hex
    }
    val red = hex.substring(2, 4).toInt(16)
    val green = hex.substring(4, 6).toInt(16)
    val blue = hex.substring(6, 8).toInt(16)

    return try {
        Color(red, green, blue)
    } catch (e: NumberFormatException) {
        if (foreGround) Color.White else Color.Black
    }
}

@Composable
fun MGButton(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    content: @Composable RowScope.() -> Unit
) {
    val displayConfigManager = koinInject<DisplayConfigManager>()
    val currentDisplayConfig by displayConfigManager.displayConfig.collectAsState()
    val buttonColors = ButtonDefaults.buttonColors(
        containerColor = currentDisplayConfig.buttonContainerColorHex.toColor(),
        disabledContainerColor = currentDisplayConfig.buttonDisabledContainerColorHex.toColor(),
        contentColor = currentDisplayConfig.buttonDisabledContentColorHex.toColor(),
        disabledContentColor = currentDisplayConfig.buttonDisabledContentColorHex.toColor()
    )
    Button(
        onClick = onClick,
        modifier = modifier,
        enabled = enabled,
        colors = buttonColors,
        content = content
    )
}