import QtQuick
import Quickshell
import Quickshell.Wayland
import Quickshell.Hyprland
import Quickshell.Services.Pipewire

import "Topbar"

PanelWindow {
    id: topbarWindow

    anchors {
        top: true
        left: true
        right: true
    }

    WlrLayershell.layer: WlrLayer.Top
    WlrLayershell.focusable: false
    WlrLayershell.keyboardFocus: WlrKeyboardFocus.None
    WlrLayershell.namespace: "deltarune-quickshell"

    mask: Region {}

    visible: ShellStateManager.shellOpen
    exclusionMode: ExclusionMode.Ignore
    aboveWindows: true
    focusable: false

    implicitHeight: 182
    color: "#000000"

    readonly property var focusedWorkspace: Hyprland.focusedWorkspace
    readonly property string workspaceLabel: {
        if (!focusedWorkspace)
            return "WS --";

        if (focusedWorkspace.id >= 0)
            return "WS " + focusedWorkspace.id;

        return "WS " + String(focusedWorkspace.name || "--");
    }

    readonly property var defaultSink: Pipewire.defaultAudioSink
    readonly property string volumeLabel: {
        if (!defaultSink || !defaultSink.audio)
            return "VOL --";

        if (defaultSink.audio.muted)
            return "VOL MUTE";

        return "VOL " + Math.round(defaultSink.audio.volume * 100) + "%";
    }

    PwObjectTracker {
        objects: [Pipewire.defaultAudioSink]
    }

    SystemClock {
        id: clock
        precision: SystemClock.Minutes
    }

    Topbar {
        manager: ShellStateManager
    }

    Text {
        anchors.left: parent.left
        anchors.leftMargin: 18
        anchors.top: parent.top
        anchors.topMargin: 10
        color: "#ffffff"
        text: topbarWindow.workspaceLabel
        font.pixelSize: 36
        antialiasing: false
        font.family: "8bitoperator JVE"
        renderType: Text.NativeRendering
        font.hintingPreference: Font.PreferNoHinting
    }

    Column {
        spacing: 2
        anchors.right: parent.right
        anchors.rightMargin: 18
        anchors.top: parent.top
        anchors.topMargin: 10

        Text {
            anchors.right: parent.right
            color: "#ffffff"
            text: topbarWindow.volumeLabel
            font.pixelSize: 36
            antialiasing: false
            font.family: "8bitoperator JVE"
            renderType: Text.NativeRendering
            font.hintingPreference: Font.PreferNoHinting
        }

        Text {
            anchors.right: parent.right
            color: "#ffffff"
            text: Qt.formatTime(clock.date, "hh:mm")
            font.pixelSize: 36
            antialiasing: false
            font.family: "8bitoperator JVE"
            renderType: Text.NativeRendering
            font.hintingPreference: Font.PreferNoHinting
        }
    }
}
