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

Row {   // Workspace Row
    id: workspacesRow

    required property HyprlandMonitor thisMonitor
    spacing: Math.floor(bar.height/4)

    // 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: workspaceText.text.length === 0 ? Math.max(workspaceNum.width + Config.spacing*2, workspaceNum.height + Config.spacing) : Math.max(workspaceNum.width + workspaceText.width + Config.spacing*2, bar.height)
                height: workspaceNum.height + Config.spacing
                radius: Config.small_radius
                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: Config.border_width

                MouseArea {
                    id: workspaceButton
                    hoverEnabled: true
                    cursorShape: Qt.PointingHandCursor
                    anchors.fill: parent
                    onClicked: {
                        Hyprland.dispatch("workspace " + modelData.id)
                        // console.log("workspaceText: " + workspaceText.text.length)
                    }
                    
                    Row {
                        id: workspaceRow
                        anchors.centerIn: parent
                        spacing: Math.floor(bar.height/8)
                        leftPadding: workspaceText.text.length === 0 ? 0 : Math.floor(bar.height / 4)

                        Text {
                            id: workspaceNum
                            text: modelData.id
                            anchors.verticalCenter: parent.verticalCenter
                            color: workspaceButton.containsMouse ? Colors.tertiary
                                : modelData.active ? Colors.primary
                                : Colors.secondary

                            font.pixelSize: Config.medium_scaling(bar.height)
                            font.family: Config.font
                            font.bold: true
                        }

                        Text {
                            id: workspaceText
                            text: ""
                            anchors.verticalCenter: parent.verticalCenter
                            color: workspaceButton.containsMouse ? Colors.tertiary
                                : modelData.active ? Colors.primary
                                : Colors.secondary

                            font.pixelSize: Config.medium_scaling(bar.height)*0.8
                            font.family: Config.font
                            font.letterSpacing: Math.floor(Config.medium_scaling(bar.height) * (2 / 3))
                        }
                    }
                }

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

                    stdout: StdioCollector {
                        onStreamFinished: {
                            if (this.text.length > 1) {
                                workspaceText.text = this.text
                            } else {
                                workspaceText.text = ""
                            }
                            // console.log("workspaceScript output:", workspaceText.text, workspaceText.text.length)
                        }
                    }
                }

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

    Text {
        visible: Hyprland.workspaces.length === 0
        text: "No Workspaces"
        color: Colors.error
        font.pixelSize: Config.big_font_size(bar.height)
    }
}
