#!/usr/bin/env bash
# args_json: {"type":"object","required":["action"],"properties":{"action":{"type":"string","description":"key|type|click|move|scroll"},"keys":{"type":"string","description":"Key combo (for key action, e.g. ctrl+s)"},"text":{"type":"string","description":"Text to type (for type action)"},"button":{"type":"string","description":"Mouse button: 1=left, 2=middle, 3=right (for click)"},"x":{"type":"string","description":"X coordinate (for click/move)"},"y":{"type":"string","description":"Y coordinate (for click/move)"},"direction":{"type":"string","description":"Scroll direction: up|down|left|right"},"amount":{"type":"string","description":"Scroll amount (default: 3)"},"relative":{"type":"boolean","description":"Use relative movement (for move)"}}}
# ollie:prompt
# ## gui_input
#
# Synthetic keyboard/mouse input (ydotool/xdotool).
#
# - Actions: `key`, `type`, `click`, `move`, `scroll`
#
# ```
# gui_input(action="key", keys="ctrl+s")
# gui_input(action="type", text="hello world")
# gui_input(action="click", button="1", x="100", y="200")
# ```
# ollie:end
set -euo pipefail

source "$(dirname "$0")/_lib/args.sh"

cmd=$(arg_require "action")

# Detect input method
if command -v ydotool &>/dev/null; then
    INPUT_TOOL="ydotool"
elif command -v xdotool &>/dev/null; then
    INPUT_TOOL="xdotool"
else
    echo "ERROR: install ydotool (Wayland) or xdotool (X11)" >&2
    exit 1
fi

case "$cmd" in
    key)
        keys=$(arg_require "keys")
        if [ "$INPUT_TOOL" = "ydotool" ]; then
            ydotool key "$keys"
        else
            xdotool key "$keys"
        fi
        echo "key: $keys"
        ;;
    type)
        text=$(arg_require "text")
        if [ "$INPUT_TOOL" = "ydotool" ]; then
            ydotool type -- "$text"
        else
            xdotool type --clearmodifiers -- "$text"
        fi
        echo "typed: ${#text} chars"
        ;;
    click)
        button=$(arg_get "button" "1")
        x=$(arg_get "x" "")
        y=$(arg_get "y" "")
        if [ -n "$x" ] && [ -n "$y" ]; then
            if [ "$INPUT_TOOL" = "ydotool" ]; then
                ydotool mousemove --absolute "$x" "$y"
                sleep 0.05
                ydotool click "$button"
            else
                xdotool mousemove "$x" "$y"
                sleep 0.05
                xdotool click "$button"
            fi
            echo "click: button=$button at $x,$y"
        else
            if [ "$INPUT_TOOL" = "ydotool" ]; then
                ydotool click "$button"
            else
                xdotool click "$button"
            fi
            echo "click: button=$button at cursor"
        fi
        ;;
    move)
        x=$(arg_require "x")
        y=$(arg_require "y")
        relative=$(arg_bool "relative")
        if [ "$INPUT_TOOL" = "ydotool" ]; then
            if [ "$relative" = "true" ]; then
                ydotool mousemove -- "$x" "$y"
            else
                ydotool mousemove --absolute "$x" "$y"
            fi
        else
            if [ "$relative" = "true" ]; then
                xdotool mousemove_relative "$x" "$y"
            else
                xdotool mousemove "$x" "$y"
            fi
        fi
        echo "moved: $x,$y${relative:+ (relative)}"
        ;;
    scroll)
        direction=$(arg_require "direction")
        amount=$(arg_get "amount" "3")
        if [ "$INPUT_TOOL" = "ydotool" ]; then
            case "$direction" in
                up)    ydotool mousemove --wheel -- 0 "-$amount" ;;
                down)  ydotool mousemove --wheel -- 0 "$amount" ;;
                left)  ydotool mousemove --wheel -- "-$amount" 0 ;;
                right) ydotool mousemove --wheel -- "$amount" 0 ;;
            esac
        else
            case "$direction" in
                up)    xdotool click --repeat "$amount" 4 ;;
                down)  xdotool click --repeat "$amount" 5 ;;
                left)  xdotool click --repeat "$amount" 6 ;;
                right) xdotool click --repeat "$amount" 7 ;;
            esac
        fi
        echo "scroll: $direction $amount"
        ;;
    *)
        echo "error: unknown action: $cmd (use key|type|click|move|scroll)" >&2
        exit 1
        ;;
esac
