#!/usr/bin/env bash
# updateScreenshots.sh - regenerate all demo screenshots and save them
# at 704x462 (double the native 704x231 to give a proper aspect ratio).
#
# For each demo:
#   1. Run snapDemo.sh to capture multiple frames during the run
#   2. Pick the last snapshot (most settled state)
#   3. Resize to 704x462 (height doubled)
#   4. Save to screenshots/<demo>.png

set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
OUT_DIR="$PROJECT_ROOT/screenshots"

# Demos with visible (or expected-empty-desktop) output.
DEMOS="helloBeep helloText helloWindow qdProbe orcaFrame frame minicad reversi heavyRelocs"

# Per-demo snap frame list.  Most are fine with defaults; reversi/
# minicad/frame can use slightly later frames to capture more drawn
# content.
declare -A FRAMES
FRAMES[helloBeep]="6500"
FRAMES[helloText]="6500"
FRAMES[helloWindow]="6500"
FRAMES[qdProbe]="6500"
FRAMES[orcaFrame]="6500"
FRAMES[frame]="6000"
FRAMES[minicad]="6000"
FRAMES[reversi]="6500,7000,7500"
FRAMES[heavyRelocs]="6500"

for demo in $DEMOS; do
    omf="$PROJECT_ROOT/demos/${demo}.omf"
    [ -f "$omf" ] || { echo "skip $demo (no OMF — run demos/build.sh $demo)"; continue; }

    echo "=== $demo ==="
    frame_list="${FRAMES[$demo]:-6500}"
    snap_out=$(PARK_MOUSE=1 bash "$SCRIPT_DIR/snapDemo.sh" "$demo" "$frame_list" 2>&1) || {
        echo "  snapDemo.sh failed for $demo (exit=$?); skipping"
        continue
    }
    snap_dir=$(echo "$snap_out" | grep "snaps in:" | sed 's|snaps in: ||')
    [ -d "$snap_dir/apple2gs" ] || { echo "  no snaps for $demo (dir: $snap_dir)"; continue; }

    # Pick the LAST .png (most settled state).
    last_png=$(ls -1 "$snap_dir/apple2gs"/*.png 2>/dev/null | sort | tail -1)
    [ -n "$last_png" ] || { echo "  no png files in $snap_dir"; continue; }

    # Resize to 704x462 (double height) and copy.
    convert "$last_png" -resize 704x462\! "$OUT_DIR/${demo}.png"

    # Mask the cursor artifact.  Demos with active event loops (reversi)
    # had their cursor moved off-screen by PARK_MOUSE; demos that stop
    # processing events leave the cursor's XOR pattern frozen at its
    # last position (around x=52-76, y=63-87 in 704x462 coords).  Each
    # demo needs a slightly different mask shape/color to avoid
    # overpainting the menu bar divider line, window borders, or text.
    case "$demo" in
        helloText)
            # white content area, menu bar divider at y=57-60 — skip
            convert "$OUT_DIR/${demo}.png" \
                -fill white -draw "rectangle 52,63 76,87" \
                "$OUT_DIR/${demo}.png" ;;
        minicad|orcaFrame)
            # window top border at y=80 in doubled — mask above it only
            convert "$OUT_DIR/${demo}.png" \
                -fill white -draw "rectangle 52,62 76,79" \
                "$OUT_DIR/${demo}.png" ;;
        frame)
            # frame's window border is lower; full cursor extent visible
            convert "$OUT_DIR/${demo}.png" \
                -fill white -draw "rectangle 52,62 76,86" \
                "$OUT_DIR/${demo}.png" ;;
        helloWindow)
            # cursor sits on the window's black title bar — paint black
            convert "$OUT_DIR/${demo}.png" \
                -fill black -draw "rectangle 52,58 76,88" \
                "$OUT_DIR/${demo}.png" ;;
        reversi)
            # cursor remnant in white area between window border and
            # the title bar's black background; avoid title bar at
            # x>=72 / y>=71.
            convert "$OUT_DIR/${demo}.png" \
                -fill white -draw "rectangle 54,63 71,70" \
                "$OUT_DIR/${demo}.png" ;;
    esac
    echo "  wrote $OUT_DIR/${demo}.png ($(identify -format '%wx%h' "$OUT_DIR/${demo}.png"))"

    # Cleanup
    rm -rf "$snap_dir"
done

echo ""
echo "All screenshots updated:"
identify "$OUT_DIR"/*.png | column -t
