#!/usr/bin/env bash
# The desktop version for real, as on a computer: WinZik (not headless) on a virtual 1920x1080 screen in Docker,
# xdotool plays with it, and the whole screen is photographed at each step into var/screenshots/native/:
# splash.png (the splash alone), player.png (the windows of the computer, the lyrics at the bottom), dragged.png
# (the player dragged by its play button: moved, not playing), menu.png (the right click menu, a window of its own),
# equalizer.png (its entry clicked). Open them and look.     apps/winzik/tools/native/check.sh
# WINZIK_CHECK_ANIM=1: the animations instead: anim-unroll-NN.png (the windows unrolling after the splash, one picture
# every ~0.2 s) and anim-quit-NN.png (the close button of the player: they roll up, then WinZik quits).
# WINZIK_CHECK_COMPACT=1: the mini bar unfolded and folded (compact-*.png, its place in compact.txt "compact <step> x y w h"):
# where the player was, against the bottom of the screen, then on the title bar of another program (openbox + xclock).
# WINZIK_CHECK_KARAOKE=1: the lyrics on the whole screen (key F over the player: karaoke-1-full.png, its buttons on the
# left), then the key Escape: karaoke-2-escaped.png (back to the bar at the bottom).
set -euo pipefail
APP="$(cd "$(dirname "$0")/../.." && pwd)"
ROOT="$(cd "$APP/../.." && pwd)"
OUT="$APP/var/screenshots/native"
mkdir -p "$OUT"
docker run --rm -e "WINZIK_CHECK_ANIM=${WINZIK_CHECK_ANIM:-}" -e "WINZIK_CHECK_KARAOKE=${WINZIK_CHECK_KARAOKE:-}" -e "WINZIK_CHECK_COMPACT=${WINZIK_CHECK_COMPACT:-}" -v "$ROOT:/workspace" -v "$OUT:/out" "${JUMPHP_DESKTOP_IMAGE:-jumphp/desktop:dev}" sh -c '
apt-get update -qq >/dev/null 2>&1; apt-get install -y -qq x11-apps x11-utils imagemagick xdotool >/dev/null 2>&1
Xvfb :99 -screen 0 1920x1080x24 -nolisten tcp >/tmp/xvfb.log 2>&1 &
sleep 1
export DISPLAY=:99 SDL_AUDIODRIVER=dummy JUMPHP_VAR=/tmp/wzvar
mkdir -p /tmp/wzvar
cd /workspace/apps/winzik
php app.php >/out/log.txt 2>&1 &
pid=$!
shot() { xwd -root -silent | convert xwd:- "/out/$1.png"; }
if [ "$WINZIK_CHECK_ANIM" = 1 ]; then
  rm -f /out/anim-*.png
  sleep 1.8; i=10; while [ $i -lt 26 ]; do shot anim-unroll-$i; i=$((i+1)); done
  sleep 3
  xdotool mousemove 520 40 click 1
  i=10; while [ $i -lt 22 ]; do import -window root /out/anim-quit-$i.png 2>>/out/log.txt; i=$((i+1)); done
  sleep 1; kill -0 $pid 2>/dev/null && echo "still running after quit" >>/out/log.txt || echo "quit: ended" >>/out/log.txt
  kill $pid 2>/dev/null; chown -R '"$(id -u):$(id -g)"' /out; exit 0
fi
if [ "$WINZIK_CHECK_KARAOKE" = 1 ]; then
  rm -f /out/karaoke-*.png
  sleep 15
  xdotool mousemove 272 194; sleep 0.5; xdotool key f; sleep 2; shot karaoke-1-full
  xdotool key Escape; sleep 2; shot karaoke-2-escaped
  kill $pid 2>/dev/null; sleep 1; chown -R '"$(id -u):$(id -g)"' /out; exit 0
fi
if [ "$WINZIK_CHECK_COMPACT" = 1 ]; then
  apt-get install -y -qq openbox >/dev/null 2>&1
  rm -f /out/compact-*.png /out/compact.txt
  # the windows by their names (xdotool search does not find the windows of SDL): W H X Y on the screen
  geo() { xwininfo -root -tree | grep "\"$1\":" | grep -v 64x64 | head -1 | sed "s/.* \([0-9]*\)x\([0-9]*\)[-+][-0-9]*[-+][-0-9]* *+\([-0-9]*\)+\([-0-9]*\).*/WIDTH=\1 HEIGHT=\2 X=\3 Y=\4/"; }
  where() { eval "$(geo "WinZik - compact")"; echo "compact $1 $X $Y $WIDTH $HEIGHT" >>/out/compact.txt; shot "compact-$1"; }
  open() { eval "$(geo "WinZik - compact")"; xdotool mousemove $((X + WIDTH * 88 / 112)) $((Y + WIDTH * 6 / 112)) click 1; sleep 1.2; }
  sleep 15
  eval "$(geo WinZik)"
  xdotool mousemove $((X + WIDTH * 232 / 258)) $((Y + WIDTH * 7 / 258)) click 1; sleep 1.5    # its compact button
  where 1-folded; open; where 2-unfolded; open; where 3-folded
  eval "$(geo "WinZik - compact")"
  xdotool mousemove $((X + 10)) $((Y + 5)) mousedown 1; sleep 0.3
  for i in 1 2 3 4 5 6 7 8 9 10; do xdotool mousemove $((X + 10)) $((Y + 5 + (1075 - Y) * i / 10)); sleep 0.05; done
  sleep 0.3; xdotool mouseup 1; sleep 1
  where 4-bottom-folded; open; where 5-bottom-unfolded; open; where 6-bottom-folded
  openbox >/dev/null 2>&1 & sleep 1
  xclock -geometry 900x300+500+400 & sleep 1.5
  # the clock in front (xdotool search does not find windows here: its id from xwininfo)
  c=$(xwininfo -root -tree | sed -n "s/^ *\(0x[0-9a-f]*\) \"xclock\".*/\1/p" | head -1); xdotool windowactivate "$c" 2>/dev/null; xdotool windowfocus "$c"; sleep 2.5
  where 7-title-folded; open; sleep 1; where 8-title-unfolded; open; sleep 1; where 9-title-folded
  kill $pid 2>/dev/null; sleep 1; chown -R '"$(id -u):$(id -g)"' /out; exit 0
fi
sleep 1.5; shot splash
sleep 14; shot player
xdotool mousemove 272 194 mousedown 1; sleep 0.3
for i in 1 2 3 4 5 6 7 8 9 10; do xdotool mousemove_relative 30 20; sleep 0.05; done
sleep 0.3; xdotool mouseup 1; sleep 1; shot dragged
xdotool mousemove 500 400 click 3; sleep 1; shot menu
xdotool mousemove 570 527 click 1; sleep 1; shot equalizer
kill $pid 2>/dev/null; sleep 1
chown -R '"$(id -u):$(id -g)"' /out'
grep -v iCCP "$OUT/log.txt" || true
[ -f "$OUT/compact.txt" ] && cat "$OUT/compact.txt"
echo "==> $OUT"
