pragma ComponentBehavior: Bound
import QtQuick
import "../"

import "topbar"

Item {
    id: topbar
    implicitWidth: 1312
    implicitHeight: 182

    property var manager: ShellStateManager

    anchors.centerIn: parent

    property var iconSources: ["item.png", "equip.png", "power.png", "config.png"]
    property int iconCount: iconSources.length
    property int selectedIndex: 0

    function moveSelection(delta) {
        if (iconCount <= 0)
            return;
        selectedIndex = (selectedIndex + delta + iconCount) % iconCount;
    }

    onIconCountChanged: {
        if (iconCount <= 0) {
            selectedIndex = 0;
        } else if (selectedIndex >= iconCount) {
            selectedIndex = iconCount - 1;
        }
    }

    QtObject {
        id: overlayKeyHandler
        function handle(key) {
            if (key === Qt.Key_Left) {
                moveSelection(-1);
                return true;
            }
            if (key === Qt.Key_Right) {
                moveSelection(1);
                return true;
            }
            if (key === Qt.Key_Return || key === Qt.Key_Enter || key === Qt.Key_Z) {
                if (iconSources[selectedIndex] === "config.png") {
                    ShellStateManager.openQuickSettings({
                        source: "Topbar"
                    });
                    return true;
                }
                if (iconSources[selectedIndex] === "item.png") {
                    ShellStateManager.openAppLauncher({
                        source: "Topbar"
                    });
                    return true;
                }
                if (iconSources[selectedIndex] === "power.png") {
                    ShellStateManager.openPowerMenu({
                        source: "Topbar"
                    });
                    return true;
                }
            }
            return false;
        }

        Component.onCompleted: ShellInputManager.registerHandler("topbar", handle)
        Component.onDestruction: ShellInputManager.unregisterHandler("topbar")
    }

    DarkDollarsDisplay {}

    Image {
        source: "./topbar/setting_names/" + topbar.iconSources[topbar.selectedIndex]
        x: -64
    }

    Row {
        id: iconRow
        spacing: 76
        anchors.horizontalCenter: parent.horizontalCenter
        y: 45
        anchors.horizontalCenterOffset: -38

        Repeater {
            model: topbar.iconSources

            delegate: Item {
                id: repeatitem
                required property int index
                width: 149
                height: 108

                TopbarSettingIcon {
                    anchors.centerIn: parent
                    selected: topbar.selectedIndex == repeatitem.index
                    iconSource: topbar.iconSources[repeatitem.index]
                    showSoul: !topbar.manager.quickSettingsOpen && !topbar.manager.appLauncherOpen && !topbar.manager.powerMenuOpen
                }

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        topbar.selectedIndex = repeatitem.index;
                        if (topbar.iconSources[repeatitem.index] === "config.png") {
                            ShellStateManager.openQuickSettings({
                                source: "Topbar"
                            });
                        }
                        if (topbar.iconSources[repeatitem.index] === "item.png") {
                            ShellStateManager.openAppLauncher({
                                source: "Topbar"
                            });
                        }
                        if (topbar.iconSources[repeatitem.index] === "power.png") {
                            ShellStateManager.openPowerMenu({
                                source: "Topbar"
                            });
                        }
                    }
                }
            }
        }
    }
}
