pragma ComponentBehavior: Bound

import Quickshell
import QtQuick
import QtQuick.Controls
import Quickshell.Io
import Quickshell.Wayland

Singleton {
    id: topLevel
    required property real width
    required property real height
    property real openTime: 600

    Timer {
        id: closeTimer
        interval: topLevel.openTime
        running: false
        repeat: false
        onTriggered: logoutData.active = false
    }

    PersistentProperties {
        id: logoutData
        property bool open: false
        property bool active: false
        property real openness: 0
        property var buttons: [
            {
                name: "Logout",
                icon: "",
                key: "e",
                action: ["loginctl", "terminate-user", "1000"]
            },
            {
                name: "Suspend",
                icon: "",
                key: "s",
                action: ["systemctl", "suspend"]
            },
            {
                name: "Shutdown",
                icon: "",
                key: "d",
                action: ["systemctl", "poweroff"]
            },
            {
                name: "Reboot",
                icon: "",
                key: "r",
                action: ["systemctl", "reboot"]
            }
        ]

        onOpenChanged: {
            if (open) {
                openness = 1.0;
                logoutData.active = true;
            } else {
                openness = 0.0;
                closeTimer.start();
            }
        }
        Behavior on openness {
            NumberAnimation {
                duration: topLevel.openTime
                easing.type: Easing.InOutQuad
            }
        }
    }

    IpcHandler {
        target: "logout"
        function open(): void {
            logoutData.open = true;
        }
        function close(): void {
            logoutData.open = false;
        }
        function toggle(): void {
            logoutData.open = !logoutData.open;
        }
    }

    LazyLoader {
        id: loader
        activeAsync: logoutData.active

        WlrLayershell {
            id: logoutBase
            anchors {
                top: true
                bottom: true
                right: true
                left: true
            }

            color: "transparent"
            implicitWidth: topLevel.width
            implicitHeight: topLevel.height
            layer: WlrLayer.Overlay
            namespace: "shell:logout"
            keyboardFocus: WlrKeyboardFocus.Exclusive
            focusable: true
            exclusionMode: ExclusionMode.Ignore

            Rectangle {
                id: gridParent
                radius: 24
                implicitWidth: parent.width
                color: "#1b2021"
                border.color: "#ffab5b"
                border.width: 2
                property var margins: {
                    "h": parent.width / 24,
                    "v": parent.height / 12
                }
                property var dims: {
                    "h": (parent.width - margins.h) / 8,
                    "v": (parent.height - margins.v) / 8
                }
                anchors {
                    fill: parent
                    leftMargin: 128 - topLevel.width * (1.0 - logoutData.openness)
                    rightMargin: 128 + topLevel.width * (1.0 - logoutData.openness)
                    margins: 128
                }
                Column {
                    spacing: gridParent.margins.v
                    padding: gridParent.margins.v

                    // horizontalItemAlignment: Grid.AlignHCenter
                    // verticalItemAlignment: Grid.AlignVCenter
                    anchors {
                        fill: parent
                        alignWhenCentered: true
                        // topMargin: gridParent.margins.v
                        // bottomMargin: gridParent.margins.v
                    }
                    Repeater {
                        model: logoutData.buttons.length
                        Button {
                            id: button
                            required property int index

                            Process {
                                id: thisProcess
                                running: false
                                command: logoutData.buttons[button.index].action
                            }

                            implicitWidth: gridParent.dims.h
                            implicitHeight: gridParent.dims.v

                            font {
                                family: "MS W98 UI"
                                pointSize: 12
                            }
                            background: Rectangle {
                                color: button.hovered ? "#ffb852" : "#272a2a"
                                border.color: "#ffab5b"
                                border.width: 2
                                radius: 6
                            }
                            text: logoutData.buttons[index].name
                            onClicked: {
                                logoutData.open = false;
                                thisProcess.running = true;
                            }
                        }
                    }
                }
            }
        }
    }
}
