#!/bin/bash
# win-ctl: cliente de terminal para Manitas en Windows.
# 1. Pon aquí la IP Tailscale de tu PC:
BASE="http://100.x.x.x:18080"
# 2. Copia este script a tu ~/bin y dale permiso: chmod +x ~/bin/win-ctl
#
# Uso:
#   win-ctl shot [salida.jpg] [ancho]   captura (default 960px)
#   win-ctl click X Y | clickshot X Y [salida]
#   win-ctl rclick X Y | move X Y | mouse
#   win-ctl type "texto" | typeshoot "texto" [salida]
#   win-ctl key return | keyshot return [salida]
#   win-ctl hotkey ctrl-shift-t | scroll -300 | ping
#   win-ctl clipboard [texto] | apps | launch app | focus app

jtext() { printf '%s' "$1" | python3 -c "import json,sys;print(json.dumps(sys.stdin.read()))"; }

case "$1" in
  shot)      curl -s "$BASE/shot?w=${3:-960}" -o "${2:-/tmp/win-shot.jpg}" && echo "${2:-/tmp/win-shot.jpg}" ;;
  clickshot) curl -s -X POST "$BASE/clickshot" -H 'Content-Type: application/json' -d "{\"x\":$2,\"y\":$3,\"w\":960}" -o "${4:-/tmp/win-shot.jpg}" && echo "${4:-/tmp/win-shot.jpg}" ;;
  typeshoot) curl -s -X POST "$BASE/typeshoot" -H 'Content-Type: application/json' -d "{\"text\":$(jtext "$2"),\"w\":960}" -o "${3:-/tmp/win-shot.jpg}" && echo "${3:-/tmp/win-shot.jpg}" ;;
  keyshot)   curl -s -X POST "$BASE/keyshot" -H 'Content-Type: application/json' -d "{\"key\":\"$2\",\"w\":960}" -o "${3:-/tmp/win-shot.jpg}" && echo "${3:-/tmp/win-shot.jpg}" ;;
  click)     curl -s -X POST "$BASE/click" -H 'Content-Type: application/json' -d "{\"x\":$2,\"y\":$3}" ;;
  dblclick)  curl -s -X POST "$BASE/dblclick" -H 'Content-Type: application/json' -d "{\"x\":$2,\"y\":$3}" ;;
  drag)      curl -s -X POST "$BASE/drag" -H 'Content-Type: application/json' -d "{\"x1\":$2,\"y1\":$3,\"x2\":$4,\"y2\":$5}" ;;
  hotkey)    curl -s -X POST "$BASE/hotkey" -H 'Content-Type: application/json' -d "{\"keys\":\"$2\"}" ;;
  mouse)     curl -s "$BASE/mouse" ;;
  clipboard) if [ -z "$2" ]; then curl -s "$BASE/clipboard";
             else curl -s -X POST "$BASE/clipboard" -H 'Content-Type: application/json' -d "{\"text\":$(jtext "$2")}"; fi ;;
  apps)      curl -s "$BASE/apps" ;;
  launch)    curl -s -X POST "$BASE/launch" -H 'Content-Type: application/json' -d "{\"app\":\"$2\"}" ;;
  focus)     curl -s -X POST "$BASE/focus" -H 'Content-Type: application/json' -d "{\"app\":\"$2\"}" ;;
  rclick)    curl -s -X POST "$BASE/rclick" -H 'Content-Type: application/json' -d "{\"x\":$2,\"y\":$3}" ;;
  move)      curl -s -X POST "$BASE/move" -H 'Content-Type: application/json' -d "{\"x\":$2,\"y\":$3}" ;;
  type)      curl -s -X POST "$BASE/type" -H 'Content-Type: application/json' -d "{\"text\":$(jtext "$2")}" ;;
  key)       curl -s -X POST "$BASE/key" -H 'Content-Type: application/json' -d "{\"key\":\"$2\"}" ;;
  scroll)    curl -s -X POST "$BASE/scroll" -H 'Content-Type: application/json' -d "{\"dy\":${2:--300}}" ;;
  ping)      curl -s "$BASE/ping" ;;
  *) echo "Uso: win-ctl shot|clickshot|click|dblclick|drag x1 y1 x2 y2|rclick|move|mouse|type|typeshoot|key|keyshot|hotkey|scroll|clipboard [txt]|apps|launch|focus|ping ..."; exit 1 ;;
esac
