// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.*
import com.eim.common.App
import com.konyaco.fluent.*
import com.konyaco.fluent.background.Layer
import com.konyaco.fluent.component.Button
import com.konyaco.fluent.component.Switcher
import com.konyaco.fluent.component.Text
import com.mayakapps.compose.windowstyler.WindowBackdrop
import com.mayakapps.compose.windowstyler.WindowBackdropStyle
import com.mayakapps.compose.windowstyler.WindowCornerPreference
import com.mayakapps.compose.windowstyler.WindowFrameStyle
import java.awt.Dimension


val minimumWidth = 1024
val minimumHeight = 768

fun WindowScope.setMinimumSize() {
    window.minimumSize = Dimension(minimumWidth, minimumHeight)
}


fun main() = application {
    val state =
        rememberWindowState(placement = WindowPlacement.Floating, width = minimumWidth.dp, height = minimumHeight.dp)
    var isVisible by remember { mutableStateOf(true) }
    var canResizeable by remember { mutableStateOf(true) }
    var systemColor by remember { mutableStateOf(Color(0xFF0078D4)) }
    Window(
        onCloseRequest = { isVisible = false },
//        undecorated = true,
//        transparent = true,
        state = state,
        visible = isVisible,
        resizable = canResizeable,
        title = WindowProperty.title,
        icon = TrayIcon
    ) {
        val systemDarkMode = isSystemInDarkTheme()
        var darkMode by remember(systemDarkMode) { mutableStateOf(systemDarkMode) }

        WindowBackdropStyle(
            isDarkTheme = darkMode,
            backdropType = WindowBackdrop.Acrylic(Color.Unspecified),
            frameStyle = WindowFrameStyle(
                cornerPreference = WindowCornerPreference.ROUNDED,
            ),
        )

        setMinimumSize()

        FluentTheme(colors = if (darkMode) darkColors(systemColor) else lightColors(systemColor)) {
            val density = LocalDensity.current
            var scale by remember(density) { mutableStateOf(density.density) }

                Row {
                    Column {
//                        AppWindowTitleBar(
//                            state,
//                            onClose = { isVisible = false },
//                            onPlacementChange = { canResizeable = it })
                        Controller(scale, { scale = it }, darkMode, { darkMode = it })
                        App()

                    }
                }



        }

        AppTray({ isVisible = true })
    }

}

@Composable
private fun Controller(
    scale: Float,
    onScaleChange: (Float) -> Unit,
    darkMode: Boolean,
    onDarkModeChange: (Boolean) -> Unit
) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        val density = LocalDensity.current
        Button(onClick = { onScaleChange(density.density) }) { Text("Reset") }
        Switcher(darkMode, text = "Dark Mode", onCheckStateChange = { onDarkModeChange(it) })
    }
}


@Composable
fun AppWindowBorder(
    modifier: Modifier = Modifier /*= Modifier.border(0.1.dp,FluentTheme.colors.shades.light2,RoundedCornerShape(8.dp))*/,
    shape: Shape = RoundedCornerShape(8.dp), Content: @Composable () -> Unit
) =
    Layer(
        modifier,
        shape = shape
    ) {
        Content()
    }


