@file:JvmName("MainKt")
package mg.dot.feufaro

import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowPosition
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
//import androidx.compose.ui.window.rememberWindowState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import mg.dot.feufaro.di.commonModule
import mg.dot.feufaro.di.desktopModule
import mg.dot.feufaro.di.platformModule
import org.koin.compose.KoinContext
import org.koin.core.context.GlobalContext.startKoin
import org.koin.core.context.KoinContext
import org.koin.core.logger.Level
import mg.dot.feufaro.WindowState as MyWindowState


fun main() = application {
    startKoin {
        printLogger(Level.INFO) // Mettez Level.INFO pour voir les logs de Koin
        modules(commonModule, platformModule) // Incluez seulement le module commun
    }

    // Testez l'injection
    val fileRepository = org.koin.core.context.GlobalContext.get().get<FileRepository>()
    println("FileRepository initialized for Desktop: $fileRepository")

    val stateManager = remember { SaveSettings() }
    val saved: MyWindowState = stateManager.loadWindowState()
    val composeWindowState = rememberWindowState(
        position = WindowPosition(saved.x.dp, saved.y.dp),
        size = DpSize(saved.width.dp, saved.height.dp)
    )

    Window(
        state = composeWindowState,
        onCloseRequest = {
            exitApplication();
        },
        title = "Feufaro",
    ) {
        KoinContext {
            App()
        }
        DisposableEffect(Unit){
            onDispose {
                val stateToSave = MyWindowState(
                    x = composeWindowState.position.x.value.toInt(),
                    y = composeWindowState.position.y.value.toInt(),
                    width = composeWindowState.size.width.value.toInt(),
                    height = composeWindowState.size.height.value.toInt()
                )
                stateManager.saveWindowState(stateToSave)
            }
        }
    }
}