import Quickshell
import Quickshell.Io
import QtQuick
import QtQuick.Layouts
import Quickshell.Widgets
import Quickshell.Hyprland
import "root:/bar/widgets/common" as Common

RowLayout {
    id: root
    spacing: 10
    Layout.fillHeight: true

    Repeater {
        model: Hyprland.workspaces
        delegate: Item {
            id: root2
            required property HyprlandWorkspace modelData
            implicitWidth: 35 + appIconContainer.implicitWidth
            Layout.fillHeight: true
            Item {
                visible: root2.modelData.toplevels.values[0] != undefined
                id: appIconContainer
                implicitWidth: row.implicitWidth
                implicitHeight: 25
                x: 30
                anchors.verticalCenter: root2.verticalCenter
                Rectangle {
                    x: -10
                    radius: 7
                    color: "black"
                    implicitWidth: row.width + 20
                    implicitHeight: 25
                }
                RowLayout {
                    x: 5
                    anchors.verticalCenter: parent.verticalCenter
                    id: row
                    spacing: 15
                    Repeater {
                        model: root2.modelData.toplevels
                        delegate: Item {
                            required property HyprlandToplevel modelData
                            width: icon.implicitWidth
                            height: icon.implicitHeight
                            Text {
                                id: icon
                                color: "white"
                                font.family: "CaskaydiaCove Nerd Font Mono" // https://www.reddit.com/r/Polybar/comments/sh8krs/comment/hv3lykm/
                                font.pointSize: 13
                                // long-ass oneliner for "if there is no class icon, go to fallback"
                                text: (Common.Icons.appIcons[(modelData.lastIpcObject.class)] != undefined ) ? Common.Icons.appIcons[(modelData.lastIpcObject.class)] : Common.Icons.appIcons["fallback"]
                            }
                        }
                    }
                }
            }
            Item {
                width: 30
                height: 30
                MouseArea {
                    width: 30
                    height: 30
                    onPressed: event => {
                        modelData.activate();
                    }
                }
                Rectangle {
                    width: 30
                    height: 30
                    radius: 10
                    color: modelData.focused ? "grey" : "black"
                }
                Text {
                    anchors.centerIn: parent
                    text: modelData.id
                    color: "white"
                }
            }
        }
    }
    Timer {
        id: updateTimer
        running: true
        interval: 1000 // 10secs
        repeat: true
        onTriggered: {
            Hyprland.refreshToplevels()
        }
    }
}
