import Quickshell
import Quickshell.Wayland
import Quickshell.Hyprland
import QtQuick
import QtQuick.Layouts

PanelWindow {
    anchors.top: true
    anchors.left: false
    anchors.right: true
    anchors.bottom: true

    property bool expanded: false
    property int collapsedWidth: 48
    property int expandedWidth: 320

    implicitWidth: expanded ? expandedWidth : collapsedWidth

    margins.top: 10
    margins.bottom: 10
    margins.right: 4

    color: "transparent"

    Behavior on implicitWidth {
        NumberAnimation {
            duration: 220
        }
    }

    Rectangle {
        anchors.fill: parent
        radius: 16
        color: "#77330077"
        border.width: 1
        border.color: "#000000"
    }

    Item {
        width: 48
        height: 48

        Image {
            anchors.fill: parent
            source: Quickshell.shellDir + "/../../img/launcher/nixos_black.png"
            fillMode: Image.PreserveAspectFit
            smooth: true
        }

        MouseArea {
            anchors.fill: parent
            cursorShape: Qt.PointingHandCursor
            onClicked: {
                Quickshell.execDetached(["wofi", "--show", "drun"])
            }
        }
    }

    Text {
        anchors.left: parent.left
        anchors.leftMargin: 56
        anchors.verticalCenter: parent.verticalCenter
        rightPadding: 12

        visible: expanded
        opacity: expanded ? 1.0 : 0.0
        Behavior on opacity {
            NumberAnimation {
                duration: 160
            }
        }

        text: Hyprland.activeToplevel ? Hyprland.activeToplevel.title : ""
        color: "white"
        elide: Text.ElideRight
    }

    GlobalShortcut {
        name: "toggle_title_bar"
        description: "Expand bar and show active window title"
        onPressed: {
            expanded = !expanded
        }
    }

    GlobalShortcut {
        name: "show_active_title"
        description: "Show the active window title"
        onPressed: {
            expanded = true
        }
    }
}
