#!/bin/bash
# Helper script for setting shortcuts on Wayland compositors

print_help() {
    cat <<EOF
Dbus wrapper for keybinds on Wayland compositors.

Syntax: ${0##*/} [options]

Options:
  run <application>     Start an application inside lxqt-session
  openmenu              Open Fancy Menu
  showdesktop           Show desktop on stacking WMs
  task [1-10]           Focus Window <number on taskbar>
  volume [up|down|mute] Change volume in volume plugin
  -h, --help            Show this help message and exit
  -v, --version         Show version information
EOF
}

case "$1" in
    -h|--help)
        print_help
        exit 0
        ;;
      -v|--version)
        echo "${0##*/} version 0.1"
        exit 0
        ;;
    *)
        ;;
esac

# Search and set qdbus command
QDBUS=$(command -v qdbus6 qdbus-qt6 qdbus 2>/dev/null |head -n1)
if [[ -z "${QDBUS:-}" ]]; then
    notify-send -i lxqt "No qdbus command available, please install it"
    exit 1
fi

valid=("run" "openmenu" "showdesktop" "task" "volume")
if [[ ! " ${valid[*]} " =~ " $1 " ]]; then
    echo "Invalid argument: $1"
    print_help
    exit 1
fi

MODE=$1
# argument: <application> | [1-10] | up , down, mute
ARGUMENT=$2

if [ "$MODE" = "run" ]; then
    for dir in ${XDG_DATA_DIRS//:/ }; do
        DESKTOP_FILE=$(grep -l "^Exec=[a-zA-Z0-9_/]*\b$ARGUMENT\b" $dir/applications/*.desktop 2>/dev/null | head -n1)
        [ -n "$DESKTOP_FILE" ] && break
    done
    # Checks
    if [ -n "$DESKTOP_FILE" ]; then
        DESKTOP_FILE=$(basename "$DESKTOP_FILE")
        $QDBUS org.lxqt.session /LXQtSession execDesktopFile $DESKTOP_FILE
    else
        notify-send -i configure-shortcuts -a "LXQt Wayland Shortcut" "No matching desktop entry found for '$ARGUMENT'"
        exit 1
    fi
elif  [ "$MODE" = "openmenu" ]; then # open main|fancy menu
    # check for dbus object
    menu=$($QDBUS org.kde.StatusNotifierWatcher | grep -E '(fancy|main)menu' | head -n1)
    if [[ -z "$menu" ]]; then
        notify-send --icon=lxqt "No fancy menu plugin found on panels"
    else
    $QDBUS org.kde.StatusNotifierWatcher $menu/show_hide org.lxqt.global_key_shortcuts.client.activated
    fi
elif  [ "$MODE" = "showdesktop" ]; then # show desktop
    # check for dbus object
    showdesktop=$($QDBUS org.kde.StatusNotifierWatcher | grep showdesktop | head -n1)
    if [[ -z "$showdesktop" ]]; then
        notify-send --icon=lxqt "No showdesktop plugin found on panels"
    else
        $QDBUS org.kde.StatusNotifierWatcher $showdesktop/show_hide org.lxqt.global_key_shortcuts.client.activated
    fi
elif  [ "$MODE" = "task" ]; then # activate taskbar button
    # check for dbus object
    taskbar=$($QDBUS org.kde.StatusNotifierWatcher | grep taskbar | head -n1)
    if [[ -z "$taskbar" ]]; then
        notify-send --icon=lxqt "No taskbar plugin found on panels"
    else
        $QDBUS org.kde.StatusNotifierWatcher $taskbar/task_$2 org.lxqt.global_key_shortcuts.client.activated
    fi
elif  [ "$MODE" = "volume" ]; then
    # check for dbus object
    volume=$($QDBUS org.kde.StatusNotifierWatcher | grep volume | head -n1)
    # Fallback for no volume plugin on panels
    if [[ -z "$volume" ]]; then
        notify-send "No volume plugin found on panels" # debug for niri
    echo $ARGUMENT
        if [[ $ARGUMENT = "up" ]]; then
            wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+
        elif [[ $ARGUMENT = "down" ]]; then
            wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-
        elif [[ $ARGUMENT = "mute" ]]; then
            wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle
       fi
    else
        $QDBUS org.kde.StatusNotifierWatcher $volume/$ARGUMENT org.lxqt.global_key_shortcuts.client.activated
    fi
fi
