#!/usr/bin/env bash
# description: Manage KDE global shortcuts via KGlobalAccel D-Bus. Args: <list|trigger|get|set> [component] [shortcut] [keys]
set -euo pipefail

SERVICE="org.kde.kglobalaccel"
OBJ_PATH="/kglobalaccel"
IFACE="org.kde.KGlobalAccel"

# Also component interface
COMP_OBJ="/component"
COMP_IFACE="org.kde.kglobalaccel.Component"

cmd="${1:-list}"

case "$cmd" in
    list)
        # List all registered shortcut components
        components=$(gdbus call --session --dest "$SERVICE" \
            --object-path "$OBJ_PATH" \
            --method "$IFACE.allComponents" 2>/dev/null | \
            sed "s/^(\[//;s/\],)$//;s/'//g;s/, /\n/g")

        if [ -z "$components" ]; then
            echo "ERROR: KGlobalAccel not responding" >&2
            exit 1
        fi

        if [ -n "${2:-}" ]; then
            # List shortcuts for specific component
            comp="$2"
            echo "# Shortcuts for: $comp"
            gdbus call --session --dest "$SERVICE" \
                --object-path "/component/$comp" \
                --method "$COMP_IFACE.shortcutNames" 2>/dev/null | \
                sed "s/^(\[//;s/\],)$//;s/'//g;s/, /\n/g"
        else
            echo "$components"
        fi
        ;;
    get)
        # Get key binding for a shortcut
        comp="${2:?usage: gui_shortcuts get <component> <shortcut_name>}"
        name="${3:?usage: gui_shortcuts get <component> <shortcut_name>}"

        gdbus call --session --dest "$SERVICE" \
            --object-path "$OBJ_PATH" \
            --method "$IFACE.shortcut" \
            "['$comp', '$name', '', '']" 2>/dev/null
        ;;
    trigger)
        # Trigger (invoke) a global shortcut by component/name
        comp="${2:?usage: gui_shortcuts trigger <component> <shortcut_name>}"
        name="${3:?usage: gui_shortcuts trigger <component> <shortcut_name>}"

        # First get the shortcut keys, then simulate them
        # Or use the invokeShortcut method if available
        gdbus call --session --dest "$SERVICE" \
            --object-path "/component/$comp" \
            --method "$COMP_IFACE.invokeShortcut" "$name" 2>/dev/null && {
            echo "triggered: $comp/$name"
            exit 0
        }

        echo "ERROR: could not trigger $comp/$name" >&2
        exit 1
        ;;
    set)
        # Set key binding for a shortcut
        comp="${2:?usage: gui_shortcuts set <component> <shortcut_name> <keys>}"
        name="${3:?usage: gui_shortcuts set <component> <shortcut_name> <keys>}"
        keys="${4:?usage: gui_shortcuts set <component> <shortcut_name> <keys>}"

        gdbus call --session --dest "$SERVICE" \
            --object-path "$OBJ_PATH" \
            --method "$IFACE.setShortcut" \
            "['$comp', '$name', '', '']" \
            "[['$keys']]" 0 2>/dev/null && {
            echo "set: $comp/$name = $keys"
            exit 0
        }

        echo "ERROR: could not set shortcut" >&2
        exit 1
        ;;
    *)
        echo "usage: gui_shortcuts <list|get|trigger|set> [component] [shortcut] [keys]" >&2
        echo "" >&2
        echo "Examples:" >&2
        echo "  gui_shortcuts list                    - list all components" >&2
        echo "  gui_shortcuts list kwin               - list kwin shortcuts" >&2
        echo "  gui_shortcuts trigger kwin 'Walk Through Windows' - trigger Alt+Tab" >&2
        echo "  gui_shortcuts get kwin 'Walk Through Windows'" >&2
        exit 1
        ;;
esac
