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

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

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

Options:
  run <application>     Start an application inside lxqt-session
  openmenu              Open Fancy Menu
  lxqt-clip             Show lxqt-clip
  showdesktop           Show desktop on stacking WMs
  restart [module]      Restart a lxqt-module
                        Main modules: desktop, panel, runner, notifications,
                        powermanagement, policykit-agent
  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.2"
        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" "lxqt-clip" "showdesktop" "restart" "task" "volume")
if [[ ! " ${valid[*]} " =~ " $1 " ]]; then
    echo "Invalid argument: $1"
    print_help
    exit 1
fi

MODE=$1
# argument: <application> | module | [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" = "lxqt-clip" ]; then # open lxqt-clip
    # check for dbus object
    clip=$($QDBUS org.lxqt.lxqt-clip | grep lxqt)
    if [[ -z "$clip" ]]; then
        notify-send --icon=lxqt "Is lxqt-clip installed and running?"
    else
        $QDBUS org.lxqt.lxqt-clip /org/lxqt/clip show
    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" = "restart" ]; then # restart <module>
    # check for dbus object
    modules=$($QDBUS org.lxqt.session /LXQtSession org.lxqt.session.listModules | grep lxqt-$2.desktop)
    if [[ -z "$modules" ]]; then
        notify-send --icon=lxqt "Module lxqt-$2 not found"
    else
        $QDBUS org.lxqt.session /LXQtSession org.lxqt.session.stopModule lxqt-"$2".desktop;
        sleep 1;
        $QDBUS org.lxqt.session /LXQtSession org.lxqt.session.startModule lxqt-"$2".desktop;
    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
