#!/usr/bin/env bash
# ============================================================================
# fifadrive.sh — drive & observe FIFA 17's menu from a shell (OpenFUT recon)
#
# FIFA 17 runs under Proton as an XWayland client. XWayland accepts X11
# synthetic input (XTEST, via xdotool) and per-window capture (XGetImage, via
# ImageMagick `import`). So we can navigate the menus and screenshot the result
# entirely headless — no manual relaunch/navigation per test.
#
#   ./fifadrive.sh wid              print the FIFA window id (empty if not up)
#   ./fifadrive.sh focus            raise + focus the FIFA window
#   ./fifadrive.sh shot [name]      capture FIFA window -> screens/<name>.png (print path)
#   ./fifadrive.sh key  <keys...>   focus, then send key(s)  e.g. key Right Right Return
#   ./fifadrive.sh hold <key> <ms>  press-and-hold a key for <ms> (menus that need a beat)
#   ./fifadrive.sh type <text...>   focus, then type literal text (e.g. security answer)
#   ./fifadrive.sh launch           (re)launch FIFA via ~/Desktop/launch-fifa17.sh
#   ./fifadrive.sh wait <secs>      poll until the FIFA window appears (after launch)
#
# XTEST injects to the FOCUSED window, so every input auto-focuses FIFA first.
# The higher-level "read the screenshot and decide the next key" step is done by
# the operator (Claude Reads the PNG) — this script only provides the primitives.
# ============================================================================
set -uo pipefail
export DISPLAY="${DISPLAY:-:0}"
HERE="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
SCREENS="${FIFADRIVE_SCREENS:-$HERE/../.screens}"
TITLE='^FIFA 17$'
LAUNCH="$HOME/Desktop/launch-fifa17.sh"

wid() { xdotool search --name "$TITLE" 2>/dev/null | head -1; }

need_wid() {
  local w; w=$(wid)
  [ -n "$w" ] || { echo "!! FIFA window not found (is the game running?)" >&2; exit 3; }
  printf '%s' "$w"
}

focus() {
  local w; w=$(need_wid)
  xdotool windowactivate --sync "$w" 2>/dev/null
  # tiny settle so the compositor finishes the focus switch before we inject
  perl -e 'select(undef,undef,undef,0.20)' 2>/dev/null || sleep 1
}

shot() {
  local w name out; w=$(need_wid); name="${1:-shot}"
  mkdir -p "$SCREENS"
  out="$SCREENS/${name}.png"
  import -window "$w" "$out" 2>/dev/null || { echo "!! capture failed" >&2; exit 4; }
  echo "$out"
}

case "${1:-}" in
  wid)    wid; echo ;;
  focus)  focus; echo "focused: $(xdotool getactivewindow getwindowname 2>/dev/null)" ;;
  shot)   shift; shot "${1:-shot}" ;;
  key)    shift; focus; xdotool key --clearmodifiers "$@"; echo "sent: $*" ;;
  hold)   shift; focus; k="$1"; ms="${2:-300}"
          xdotool keydown --clearmodifiers "$k"; perl -e "select(undef,undef,undef,$ms/1000)" 2>/dev/null || sleep 1
          xdotool keyup --clearmodifiers "$k"; echo "held: $k ${ms}ms" ;;
  type)   shift; focus; xdotool type --clearmodifiers -- "$*"; echo "typed: $*" ;;
  launch) [ -x "$LAUNCH" ] || { echo "!! no launch script at $LAUNCH" >&2; exit 5; }
          setsid bash -c "exec '$LAUNCH'" </dev/null >/tmp/fifa_launch.log 2>&1 & disown
          echo "launched (log: /tmp/fifa_launch.log)" ;;
  wait)   shift; secs="${1:-60}"; i=0
          while [ $i -lt "$secs" ]; do [ -n "$(wid)" ] && { echo "FIFA window up (${i}s)"; exit 0; }; sleep 1; i=$((i+1)); done
          echo "!! FIFA window did not appear in ${secs}s" >&2; exit 6 ;;
  *) sed -n '2,32p' "$0"; exit 1 ;;
esac
