import QtQuick
import Quickshell
import Quickshell.Hyprland
import Quickshell.Io
import "../../../colors"

Row {   // Workspace Row
    id: workspacesRow

    required property HyprlandMonitor thisMonitor
    spacing: 8

    // Get infos of Type Unknown
    // Repeater {
    //     model: Hyprland.workspaces

    //     Rectangle {
    //         Component.onCompleted: {
    //             console.log("roles:")
    //             console.log("id:", modelData.id)
    //             console.log("monitor:", modelData.monitor)
    //             console.log("monitorId:", modelData.monitorId)
    //             console.log("name:", modelData.name)
    //         }
    //     }
    // }

    Repeater {   // repeats workspaces
        model: Hyprland.workspaces
        Loader {
            active: modelData.monitor === thisMonitor
            sourceComponent: Rectangle {  //workspace
                // visible: modelData.monitor === thisMonitor
                width: (workspaceNum.width + 16) < (workspaceNum.height + 8) ? workspaceNum.height + 8 : workspaceNum.width + 16
                height: workspaceNum.height + 8
                radius: 4
                color: workspaceButton.containsMouse ? Colors.tertiaryContainer
                    : modelData.active ? Colors.primaryContainer
                    : Colors.secondaryContainer
                border.color: workspaceButton.containsMouse ? Colors.tertiaryContainer_fg
                    : modelData.active ? Colors.primaryContainer_fg
                    : Colors.secondaryContainer_fg
                border.width: 2

                MouseArea {
                    id: workspaceButton
                    hoverEnabled: true
                    cursorShape: Qt.PointingHandCursor
                    anchors.fill: parent
                    onClicked: Hyprland.dispatch("workspace " + modelData.id)
        
                    Rectangle {
                        anchors.fill: parent
                        color:  "transparent"
                        radius: 6
                    }
                }

                Text {
                    id: workspaceNum
                    text: modelData.id
                    anchors.centerIn: parent
                    color: workspaceButton.containsMouse ? Colors.tertiary
                        : modelData.active ? Colors.primary
                        : Colors.secondary
                    font.pixelSize: 18
                    font.family: "Inter, sans-serif"
                }

                Process {
                    id: workspaceContent
                    command: ["sh", "-c", Quickshell.env("HOME") + "/.config/quickshell/modules/bar/scripts/Apps.sh " + modelData.id]
                    running: true

                    stdout: StdioCollector {
                        onStreamFinished: {
                            workspaceNum.text = this.text
                            // console.log("workspaceScript output:", this.text)
                        }
                    }
                }

                Timer {
                    interval: 1000
                    running: true
                    repeat: true
                    onTriggered: workspaceContent.running = true
                }
            }
        }
    }

    Text {
        visible: Hyprland.workspaces.length === 0
        text: "No Workspaces"
        color: Colors.error
        font.pixelSize: 18
    }
}
