package ch.dissem.yaep.ui.desktop

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.window.WindowDraggableArea
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.WindowPlacement
import androidx.compose.ui.window.WindowScope
import androidx.compose.ui.window.WindowState
import ch.dissem.yaep.ui.common.focus.FocusFollowingSelectionManager
import ch.dissem.yaep.ui.common.theme.AppTheme
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import yaep.commonui.generated.resources.action_restart
import yaep.commonui.generated.resources.app_name_full
import yaep.commonui.generated.resources.moon
import yaep.commonui.generated.resources.restart
import yaep.commonui.generated.resources.sun
import yaep.commonui.generated.resources.use_dark_mode
import yaep.commonui.generated.resources.use_light_mode
import yaep.commonui.generated.resources.window_close
import yaep.commonui.generated.resources.window_maximize
import yaep.commonui.generated.resources.window_minimize
import yaep.desktop.generated.resources.ic_launcher
import yaep.commonui.generated.resources.Res as CRes
import yaep.desktop.generated.resources.Res as DRes

@Composable
fun WindowScope.DesktopWindow(
    useDarkMode: Boolean,
    selectionManager: FocusFollowingSelectionManager,
    topBar: @Composable () -> Unit,
    content: @Composable (PaddingValues) -> Unit
) {
    AppTheme(darkTheme = useDarkMode) {
        Scaffold(
            modifier = Modifier.onKeyEvent { event -> selectionManager.onKeyEvent(event) },
            topBar = {
                WindowDraggableArea {
                    topBar()
                }
            }
        ) { paddingValues ->
            content(paddingValues)
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppBar(
    useDarkMode: Boolean,
    setDarkMode: (Boolean) -> Unit,
    onCloseRequest: () -> Unit,
    onRestart: () -> Unit,
    windowState: WindowState,
) {
    TopAppBar(
        navigationIcon = {
            IconButton(onClick = { /* do nothing */ }) {
                Image(
                    painter = painterResource(DRes.drawable.ic_launcher),
                    contentDescription = null
                )
            }
        },
        title = { Text(text = stringResource(CRes.string.app_name_full)) },
        actions = {
            IconButton(
                onClick = onRestart
            ) {
                Icon(
                    painter = painterResource(CRes.drawable.action_restart),
                    contentDescription = stringResource(CRes.string.restart),
                    modifier = Modifier.size(SwitchDefaults.IconSize),
                )
            }
            Spacer(Modifier.width(8.dp))
            Switch(
                checked = useDarkMode,
                onCheckedChange = setDarkMode,
                thumbContent = {
                    Icon(
                        painter = painterResource(
                            if (useDarkMode) {
                                CRes.drawable.moon
                            } else {
                                CRes.drawable.sun
                            }
                        ),
                        contentDescription = stringResource(
                            if (useDarkMode) {
                                CRes.string.use_dark_mode
                            } else {
                                CRes.string.use_light_mode
                            }
                        ),
                        modifier = Modifier.size(SwitchDefaults.IconSize),
                    )
                }
            )
            Spacer(Modifier.width(8.dp))
            IconButton(
                onClick = { windowState.isMinimized = !windowState.isMinimized }
            ) {
                Icon(
                    painter = painterResource(CRes.drawable.window_minimize),
                    contentDescription = stringResource(CRes.string.window_minimize),
                    modifier = Modifier.size(SwitchDefaults.IconSize),
                )
            }
            IconButton(
                onClick = {
                    windowState.placement =
                        if (windowState.placement == WindowPlacement.Maximized)
                            WindowPlacement.Floating else WindowPlacement.Maximized
                }
            ) {
                Icon(
                    painter = painterResource(CRes.drawable.window_maximize),
                    contentDescription = stringResource(CRes.string.window_maximize),
                    modifier = Modifier.size(SwitchDefaults.IconSize),
                )
            }
            IconButton(
                onClick = onCloseRequest
            ) {
                Icon(
                    painter = painterResource(CRes.drawable.window_close),
                    contentDescription = stringResource(CRes.string.window_close),
                    modifier = Modifier.size(SwitchDefaults.IconSize),
                )
            }
        },
    )
}