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
    default property list<LogoutButton> buttons

    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 int selected: -1

        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.InOutExpo
            }
        }
    }

    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
            contentItem {
                focus: true
                Keys.onPressed: event => {
                    if (event.key == Qt.Key_Escape) {
                        logoutData.open = false;
                    } else {
                        for (let i = 0; i < topLevel.buttons.length; ++i) {
                            let button = topLevel.buttons[i];
                            if (event.key == button.keybind) {
                                logoutData.open = false;
                                button.exec();
                            }
                        }
                    }
                }
            }

            MouseArea {
                anchors.fill: parent
                onClicked: logoutData.open = false
            }

            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) / 12
                }
                anchors {
                    fill: parent
                    margins: parent.width / 2.5
                    topMargin: (parent.height / 8) + topLevel.height * (1.0 - logoutData.openness)
                    bottomMargin: (parent.height / 8) - topLevel.height * (1.0 - logoutData.openness)
                }
                topLeftRadius: 0
                bottomRightRadius: 0
                topRightRadius: 10
                bottomLeftRadius: 10

                Column {
                    spacing: gridParent.margins.v
                    padding: gridParent.margins.v
                    anchors {
                        centerIn: parent
                        alignWhenCentered: true
                        verticalCenter: parent.verticalCenter
                    }
                    width: parent.width / 3 * 2
                    height: parent.height / 6 * 5
                    Repeater {
                        model: topLevel.buttons.length
                        Button {
                            id: button
                            required property int index

                            anchors.horizontalCenter: parent.horizontalCenter

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

                            font {
                                family: "MS W98 UI"
                                pointSize: 12
                            }
                            background: Rectangle {
                                color: button.hovered ? "#ffb852" : "#272a2a"
                                Behavior on color {
                                    ColorAnimation {
                                        duration: 150
                                    }
                                }
                                border.color: "#ffab5b"
                                border.width: 2
                                radius: 6
                            }
                            text: topLevel.buttons[index].text
                            onClicked: {
                                logoutData.open = false;
                                logoutData.selected = index;
                                topLevel.buttons[index].exec();
                            }
                        }
                    }
                }
            }
        }
    }
}
