import QtQuick
import Quickshell
import Quickshell.Hyprland
import Quickshell.Io
import "../../../colors"
import "../../../config"
import "../states/Network" as Global

MouseArea {
    id: networkButton

    width: Math.max(networkText.width + Config.spacing*2, bar.height)
    height: bar.height

    hoverEnabled: true
    cursorShape: Qt.PointingHandCursor

    onClicked: {
        Global.NetworkState.networkScript.running = true
        // networkStatus.running = true
    }

    Rectangle {
        anchors.fill: parent
        color: networkButton.containsMouse ? Colors.tertiary : Qt.alpha(Colors.tertiaryContainer, 0.25)
        radius: Config.radius
        border.width: Config.border_width
        border.color: networkButton.containsMouse ? Colors.tertiaryContainer_fg
            : Colors.secondaryContainer_fg

        Text {
            id: networkText
            text: Global.NetworkState.status
            anchors.centerIn: parent
            font.pixelSize: Config.big_font_size(bar.height)
            font.family: Config.font
            color: networkButton.containsMouse ? Colors.tertiary_fg : networkText.text === "󰤭 /󱐤" ? Colors.error : Colors.primary
        }

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

        //     stdout: StdioCollector {
        //         onStreamFinished: {
        //             networkText.text = this.text.trim()
        //             // console.log("networkScript networkText:", this.text.trim())
        //         }
        //     }
        // }

        // Timer {
        //     interval: 10000
        //     running: true
        //     repeat: true
        //     onTriggered: networkStatus.running = true
        // }
    }
}