package ch.dissem.yaep.ui.desktop

import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.platform.Font
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowPlacement
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import ch.dissem.yaep.domain.generateGame
import ch.dissem.yaep.ui.common.App
import ch.dissem.yaep.ui.common.focus.FocusFollowingSelectionManager
import ch.dissem.yaep.ui.common.theme.emojiFontFamily
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import yaep.commonui.generated.resources.app_name
import yaep.desktop.generated.resources.ic_launcher
import yaep.commonui.generated.resources.Res as CRes
import yaep.desktop.generated.resources.Res as DRes

fun main(): Unit = application {
    emojiFontFamily = FontFamily(
        Font(
            resource = "NotoColorEmoji-Regular.ttf",
            weight = FontWeight.W400,
            style = FontStyle.Normal
        )
    )
    val windowState = rememberWindowState(
        placement = WindowPlacement.Floating,
        size = DpSize(1250.dp, 800.dp)
    )
    var game by remember { mutableStateOf(generateGame()) }

    Window(
        onCloseRequest = ::exitApplication,
        undecorated = true,
        title = stringResource(CRes.string.app_name),
        state = windowState,
        icon = painterResource(DRes.drawable.ic_launcher)
    ) {
        val rootSelectionManager = FocusFollowingSelectionManager
        var useDarkMode by remember { mutableStateOf(true) }
        var resetCluesBeacon by remember { mutableStateOf(Any()) }
        DesktopWindow(
            useDarkMode = useDarkMode,
            selectionManager = rootSelectionManager,
            topBar = {
                AppBar(
                    useDarkMode = useDarkMode,
                    setDarkMode = { useDarkMode = it },
                    onCloseRequest = ::exitApplication,
                    onRestart = {
                        @Suppress("ControlFlowWithEmptyBody")
                        do /* nothing */ while (game.grid.undo())
                        resetCluesBeacon = Any()
                    },
                    windowState = windowState,
                )
            }
        ) {
            App(
                modifier = Modifier.padding(it),
                rootSelectionManager = rootSelectionManager,
                spacing = 8.dp,
                game = game,
                onNewGame = { game = generateGame() },
                resetCluesBeacon = resetCluesBeacon
            )
        }
    }
}