#!/usr/bin/env bash
# android_input_swipe — Send swipe gesture between two points via adb shell input swipe.
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"

# ---------------------------------------------------------------------------
# android_input_swipe [--serial <S>] <x1> <y1> <x2> <y2> [duration_ms]
#
# $1  x1           Start X coordinate in pixels (obligatorio).
# $2  y1           Start Y coordinate in pixels (obligatorio).
# $3  x2           End X coordinate in pixels (obligatorio).
# $4  y2           End Y coordinate in pixels (obligatorio).
# $5  duration_ms  Swipe duration in milliseconds (opcional, default 300).
#
# Envvar ADB_SERIAL overrides --serial.
# ---------------------------------------------------------------------------
android_input_swipe() {
    adb_pick_serial "$@" || exit 3
    local serial="$ADB_PICK_SERIAL"
    set -- "${ADB_PICK_REST[@]}"

    local x1="${1:-}"
    local y1="${2:-}"
    local x2="${3:-}"
    local y2="${4:-}"
    local dur="${5:-300}"

    if [[ -z "$x1" || -z "$y1" || -z "$x2" || -z "$y2" ]]; then
        echo "android_input_swipe: se requieren cuatro argumentos: x1 y1 x2 y2." >&2
        return 1
    fi

    # Validar que los cuatro coordenadas son numericas (enteros o negativos).
    local coord
    for coord in "$x1" "$y1" "$x2" "$y2"; do
        if ! [[ "$coord" =~ ^-?[0-9]+$ ]]; then
            echo "android_input_swipe: coordenada no numerica: '$coord'." >&2
            return 1
        fi
    done

    adb_s "$serial" shell input swipe "$x1" "$y1" "$x2" "$y2" "$dur"
    echo "swipe $x1,$y1 → $x2,$y2 (${dur}ms) on $serial"
}

# Ejecutar si se llama directamente (no sourceado)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    android_input_swipe "$@"
fi
