#!/usr/bin/env bash
#
# demo.sh — interactive tour of every fed* field editor.
#
# Each editor is a subprocess component: it draws into a region of the
# terminal, writes the edited value to stdout, and writes the exit-reason
# mnemonic to stderr. This script shows the standard calling pattern —
# capture stdout into a variable, capture stderr separately, and branch on
# the exit code (0 = noop, 1-9 = exit keys; see each tool's --help).
#
#   Usage:  bash examples/demo.sh
#
# Requires a real terminal (the editors use curses). Run it directly, not
# through a pipe.

set -u
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
TMP="$(mktemp)"
trap 'rm -f "$TMP"' EXIT

# Dormant fallback (pairs with the commented restore in run_editor below): if a
# terminal still shows wrong newline handling after a field exits — bare LF with
# no CR, so lines "stairstep" diagonally down the screen — uncomment BOTH this
# save and that restore. With the restore-on-exit default (proper endwin) this
# should not be needed; it stays commented so it can't mask a real regression.
#OLD_STTY="$(stty -g)"

# run_editor <title> <tool.py> [args...]
# Runs an editor, captures its value (stdout) and exit reason (stderr),
# and prints both. Returns the editor's exit code.
run_editor() {
    local title="$1"; shift
    clear
    echo "=== $title ==="
    echo "(edit the field below; Tab/Enter/Esc/F10 exit — see --help)"
    echo
    local value
    value="$(python3 "$ROOT/$1" "${@:2}" 2>"$TMP")"
    local code=$?
    local reason; reason="$(cat "$TMP")"
    # uncomment (together with the OLD_STTY save at the top) only if the
    # terminal misbehaves after a field exits — see the note there.
    #stty "$OLD_STTY"
    echo
    echo "  value : '$value'"
    echo "  reason: $reason   (exit code $code)"
    echo
    read -r -p "Press Enter for the next field..."
    return $code
}

clear
cat <<'INTRO'
fed* field editor demo
======================
You'll step through one of each field type. For every field, the demo
prints the value it captured and the exit-reason mnemonic — exactly what
a real shell script would inspect.

Press Enter to begin.
INTRO
read -r

run_editor "fedline — single-line text"       fedline.py  2 0 40 120 -v "edit me"
run_editor "fedarea — multi-line text"        fedarea.py  2 0 50 6 2000 1 1 -v "wraps across lines as you type"
run_editor "fedmask — masked phone number"    fedmask.py  2 0 phone:classic
run_editor "fedlist — scrollable select"      fedlist.py  2 0 24 6 -i "Apple|Banana|Cherry|Date|Fig|Grape|Kiwi"
run_editor "fedcheck — multi-select"          fedcheck.py 2 0 24 -i "Email|SMS|Push|Phone"
run_editor "fedradio — single-select"         fedradio.py 2 0 24 -i "Small|Medium|Large" -v "Medium"

# --- Full form ---
clear
echo "=== fedform — the survey form (all field types in one process) ==="
echo "Fill it in, then press F10 to submit (or ESC to cancel)."
echo
read -r -p "Press Enter to launch the form..."
form_out="$(python3 "$ROOT/fedform.py" "$ROOT/examples/survey.def" -o kv 2>"$TMP")"
code=$?
clear
echo "Form exited with code $code ($(cat "$TMP"))."
echo
echo "Submitted values (kv format):"
echo "----------------------------------------"
echo "$form_out"
echo "----------------------------------------"
echo
echo "Tip: try other output formats and pre-filled values —"
echo "  python3 fedform.py examples/survey.def -o shell"
echo "  python3 fedform.py examples/survey.def -o json"
echo "  python3 fedform.py examples/survey.def --values examples/survey-values.json"
