#!/usr/bin/env bash
# M4 theme test: drives the themed demo under quibble and asserts
#   1. idle renders zero frames (unchanged under theming),
#   2. hovering a button repaints (engine-level :hover restyle),
#   3. clicking "Switch theme" swaps to dark and the pixels change,
#   4. quiescent again afterwards.
set -euo pipefail
cd "$(dirname "$0")/.."

cargo build -p guiduck --example m4_themed
cargo build -p guiduck-render-tinyskia --bin png-diff

workdir=$(mktemp -d)
sock="$workdir/quibble.sock"
cleanup() {
    quibblectl --socket "$sock" quit >/dev/null 2>&1 || true
    rm -rf "$workdir"
}
trap cleanup EXIT

PLUS_X=76;  PLUS_Y=100     # center of the "+1" button at 480x320
SWITCH_X=228; SWITCH_Y=100 # center of the "Switch theme" button
AWAY_X=400; AWAY_Y=260

GUIDUCK_BACKEND=software GUIDUCK_TRACE_FRAMES=1 \
    quibble --socket "$sock" --width 480 --height 320 \
    target/debug/examples/m4_themed \
    >"$workdir/out.log" 2>"$workdir/err.log" &
for _ in $(seq 1 50); do
    quibblectl --socket "$sock" ping >/dev/null 2>&1 && break
    sleep 0.2
done
quibblectl --socket "$sock" wait-window --timeout-ms 30000 >/dev/null
sleep 1

frames() { grep -c '^guiduck-frame' "$workdir/err.log" || true; }
fail=0

# 1: idle.
f0=$(frames); sleep 3; f1=$(frames)
echo "idle frames: $f0 -> $f1"
[ "$f0" = "$f1" ] || { echo "FAIL: idle rendered frames" >&2; fail=1; }

quibblectl --socket "$sock" screenshot -o "$workdir/light.png" >/dev/null

# 2: hover restyle.
h0=$(frames)
quibblectl --socket "$sock" pointer-move $PLUS_X $PLUS_Y >/dev/null
sleep 0.5
quibblectl --socket "$sock" screenshot -o "$workdir/hover.png" >/dev/null
h1=$(frames)
if [ "$h1" -le "$h0" ]; then
    echo "FAIL: hover did not repaint" >&2; fail=1
fi
if target/debug/png-diff "$workdir/light.png" "$workdir/hover.png" >/dev/null; then
    echo "FAIL: hover produced identical pixels" >&2; fail=1
else
    echo "hover restyle: pixels changed"
fi
quibblectl --socket "$sock" pointer-move $AWAY_X $AWAY_Y >/dev/null
sleep 0.5

# 3: live theme swap.
quibblectl --socket "$sock" pointer-move $SWITCH_X $SWITCH_Y >/dev/null
quibblectl --socket "$sock" click >/dev/null
quibblectl --socket "$sock" pointer-move $AWAY_X $AWAY_Y >/dev/null
sleep 0.7
quibblectl --socket "$sock" screenshot -o "$workdir/dark.png" >/dev/null
if grep -q '^theme=Dark$' "$workdir/out.log"; then
    echo "switch clicked: theme=Dark"
else
    echo "FAIL: switch click did not change theme; out: $(tail -2 "$workdir/out.log" | tr '\n' ' ')" >&2
    fail=1
fi
if target/debug/png-diff "$workdir/light.png" "$workdir/dark.png" >/dev/null; then
    echo "FAIL: dark theme renders identically to light" >&2; fail=1
else
    echo "theme swap: pixels changed"
fi

# 4: quiescent again.
q0=$(frames); sleep 2; q1=$(frames)
echo "post-interaction idle frames: $q0 -> $q1"
[ "$q0" = "$q1" ] || { echo "FAIL: still rendering after interaction" >&2; fail=1; }

cp "$workdir/light.png" "$workdir/dark.png" target/ 2>/dev/null || true
[ "$fail" -eq 0 ] && echo "M4 theme test PASSED (screenshots: target/light.png target/dark.png)"
exit $fail
