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.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.theme.AppTheme
import org.jetbrains.compose.resources.painterResource
import yaep.commonui.generated.resources.Res
import yaep.commonui.generated.resources.moon
import yaep.commonui.generated.resources.sun
import yaep.commonui.generated.resources.window_close
import yaep.commonui.generated.resources.window_maximize
import yaep.commonui.generated.resources.window_minimize

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

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppBar(
    useDarkMode: Boolean,
    setDarkMode: (Boolean) -> Unit,
    onCloseRequest: () -> Unit,
    windowState: WindowState,
) {
    TopAppBar(
        navigationIcon = {
            // TODO Icon(painterResource(Res.drawable.heart), null)
        },
        title = { Text(text = "Yet Another Einstein Puzzle") },
        actions = {
            Switch(
                checked = useDarkMode,
                onCheckedChange = setDarkMode,
                thumbContent = {
                    Icon(
                        painter = painterResource(
                            if (useDarkMode) {
                                Res.drawable.sun
                            } else {
                                Res.drawable.moon
                            }
                        ),
                        contentDescription = null,
                        modifier = Modifier.size(SwitchDefaults.IconSize),
                    )
                }
            )
            Spacer(Modifier.width(8.dp))
            IconButton(
                onClick = { windowState.isMinimized = !windowState.isMinimized }
            ) {
                Icon(
                    painter = painterResource(Res.drawable.window_minimize),
                    contentDescription = null,
                    modifier = Modifier.size(SwitchDefaults.IconSize),
                )
            }
            IconButton(
                onClick = {
                    windowState.placement =
                        if (windowState.placement == WindowPlacement.Maximized)
                            WindowPlacement.Floating else WindowPlacement.Maximized
                }
            ) {
                Icon(
                    painter = painterResource(Res.drawable.window_maximize),
                    contentDescription = null,
                    modifier = Modifier.size(SwitchDefaults.IconSize),
                )
            }
            IconButton(
                onClick = onCloseRequest
            ) {
                Icon(
                    painter = painterResource(Res.drawable.window_close),
                    contentDescription = null,
                    modifier = Modifier.size(SwitchDefaults.IconSize),
                )
            }
        },
    )
}