import QtQuick
import Quickshell
import Quickshell.Wayland
import Quickshell.Hyprland

import "Windows/QuickSettings"
import "Windows/AppLauncher"
import "Windows/PowerMenu"

PanelWindow {
    id: overlay
    anchors {
        top: true
        left: true
        right: true
        bottom: true
    }

    WlrLayershell.layer: WlrLayer.Overlay
    WlrLayershell.focusable: true
    WlrLayershell.keyboardFocus: WlrKeyboardFocus.Exclusive
    WlrLayershell.namespace: "deltarune-quickshell-overlay"

    visible: ShellStateManager.shellOpen
    exclusionMode: ExclusionMode.Ignore
    aboveWindows: true
    focusable: ShellStateManager.shellOpen
    color: "#00000000"

    HyprlandFocusGrab {
        id: grab
        active: true
        windows: [overlay]
    }

    FocusScope {
        id: overlayFocus
        anchors.fill: parent
        focus: ShellStateManager.shellOpen

        Component.onCompleted: {
            if (ShellStateManager.shellOpen)
                overlayFocus.forceActiveFocus();
        }

        onVisibleChanged: if (visible)
            overlayFocus.forceActiveFocus()

        onFocusChanged: {
            if (focus && ShellStateManager.shellOpen)
                overlayFocus.forceActiveFocus();
        }

        Keys.onReleased: function (event) {
            if (!ShellStateManager.shellOpen)
                return;
            if (ShellInputManager.handleKey(event)) {
                event.accepted = true;
            }
        }
    }

    Connections {
        target: ShellStateManager
        function onShellOpenChanged() {
            if (ShellStateManager.shellOpen)
                overlayFocus.forceActiveFocus();
        }
    }

    QuickSettings {
        id: quickSettingsWindow
        manager: ShellStateManager
    }

    AppLauncher {
        id: appLauncherWindow
        manager: ShellStateManager
    }

    PowerMenu {
        id: powerMenuWindow
        manager: ShellStateManager
    }
}
