import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.window.WindowDraggableArea
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
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 com.konyaco.fluent.FluentTheme
import com.konyaco.fluent.component.Icon
import com.konyaco.fluent.component.SubtleButton
import com.konyaco.fluent.component.Text
import com.konyaco.fluent.icons.Icons
import com.konyaco.fluent.icons.regular.*

@Composable
fun WindowScope.AppWindowTitleBar(state: WindowState, onClose:()->Unit , onPlacementChange: (Boolean) -> Unit){
    var maximize by remember {mutableStateOf(false)}
    var onMaximize by remember  { mutableStateOf({ state.placement = WindowPlacement.Maximized }) }
    when (state.placement) {
        WindowPlacement.Floating -> {
            onPlacementChange(true)
            onMaximize = { state.placement = WindowPlacement.Maximized }
            maximize=false
        }
        WindowPlacement.Maximized ->  {
            onPlacementChange(false)
            onMaximize = { state.placement = WindowPlacement.Floating }
            maximize=true
        }
        WindowPlacement.Fullscreen -> {
            onPlacementChange(false)
            onMaximize = { state.placement = WindowPlacement.Floating }
            maximize=false
        }
    }

        if (maximize)
            TitleBar(
                maximize = maximize,
                onClose = onClose,
                onMaximize = onMaximize,
                onMinimize = {state.isMinimized = true},
                onDoubleClick = onMaximize,
            )
        else
            WindowDraggableArea(Modifier) {
                TitleBar(
                    maximize = maximize,
                    onClose = onClose,
                    onMaximize = onMaximize,
                    onMinimize = {state.isMinimized = true},
                    onDoubleClick = onMaximize,
                )
            }

}

@Composable
private fun TitleBar(
    maximize:Boolean = false,
    onClose: () -> Unit = {},
    onMaximize: () -> Unit = {},
    onMinimize: () -> Unit = {},
    onDoubleClick: () -> Unit
) =
    Row(
        Modifier.fillMaxWidth().height(32.dp)
            .background(FluentTheme.colors.background.layer.default),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Row(
            Modifier.fillMaxHeight().padding(10.dp, 0.dp), verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            Icon(Icons.Default.Play, contentDescription = null)
            Text(text = WindowProperty.title)
        }
//        Row(Modifier.fillMaxHeight().background(FluentTheme.colors.background.layer.default).combinedClickable(
//            onClick = {},
//            onDoubleClick = onDoubleClick,
//            onLongClick = {}), verticalAlignment = Alignment.CenterVertically
//        ){
//            Text("Double Click Area")
//        }
        Row(Modifier.fillMaxHeight()) {
            SubtleButton(
                modifier = Modifier.fillMaxHeight().width(48.dp),
                onClick = onMinimize, iconOnly = true
            ) {
                Icon(Icons.Default.Subtract, contentDescription = null)
            }
//            SubtleButton(
//                modifier = Modifier.fillMaxHeight().width(48.dp),
//                onClick = onMaximize, iconOnly = true
//            ) {
//                Icon(if(maximize) Icons.Default.Window else  Icons.Default.Maximize , contentDescription = null)
//            }
            SubtleButton(modifier = Modifier.fillMaxHeight().width(48.dp), onClick = onClose, iconOnly = true) {
                Icon(Icons.Default.Dismiss, contentDescription = null)
            }
        }
    }