#!/usr/bin/env bash
# txdialog — show a dialog and return the choice.
# Usage: txdialog WIDGET [-t TITLE] [options] [--dry-run]
#   Wrapper around termux-dialog. Widgets: confirm, text, radio,
#   checkbox, sheet, spinner, date, time, counter. Prints the JSON
#   result termux-dialog returns, pretty-printed when python3 exists.
set -u
prog=txdialog

usage() {
  cat <<'USAGE'
Usage: txdialog WIDGET [-t TITLE] [options] [--dry-run]
  Show an Android dialog via termux-dialog and print the result.
  WIDGETS
    confirm [-i HINT]                 yes/no dialog
    text [-i HINT] [--password] [--number]
                                      free-text input
    radio -v "a,b,c"                  single choice
    checkbox -v "a,b,c"               multiple choice
    sheet -v "a,b,c"                  bottom-sheet choice
    spinner -v "a,b,c"                drop-down choice
    date                              date picker
    time                              time picker
    counter [-r "MIN,MAX,STEP"]       number counter (default 0,100,1)
  COMMON
    -t TITLE      dialog title
    --dry-run     print the termux-dialog command, do not show it
    --selftest    run built-in checks (dry-run + validation + guard)
    --help        show this help
  Requires termux-dialog (Termux:API). No Linux equivalent exists, so
  on plain Linux this prints a clear message and exits non-zero.
USAGE
}

pretty_json() {
  if command -v python3 >/dev/null 2>&1; then
    python3 -m json.tool 2>/dev/null || cat
  else cat; fi
}

selftest() {
  local tmp fails=0 out rc
  tmp=$(mktemp -d); trap "rm -rf '$tmp'" EXIT
  local self="${BASH_SOURCE[0]}"
  out=$("$self" --dry-run confirm -t "Sure?" 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"termux-dialog confirm"* && $out == *"-t Sure\\?"* ]]; then echo "PASS: --dry-run confirm"; else echo "FAIL: confirm rc=$rc out=$out"; fails=1; fi
  out=$("$self" --dry-run radio -t "Pick one" -v "a,b,c" 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"termux-dialog radio"* && $out == *"-v a\\,b\\,c"* ]]; then echo "PASS: --dry-run radio"; else echo "FAIL: radio rc=$rc out=$out"; fails=1; fi
  out=$("$self" --dry-run text -t "Name" -i "hint" --password 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"termux-dialog text"* && $out == *"--password"* ]]; then echo "PASS: --dry-run text --password"; else echo "FAIL: text rc=$rc out=$out"; fails=1; fi
  out=$("$self" --dry-run radio -t "Pick" 2>&1); rc=$?
  if (( rc != 0 )) && [[ $out == *"-v"* ]]; then echo "PASS: radio without -v rejected"; else echo "FAIL: radio no -v rc=$rc out=$out"; fails=1; fi
  out=$("$self" --dry-run bogus 2>&1); rc=$?
  if (( rc != 0 )); then echo "PASS: bad widget rejected"; else echo "FAIL: bad widget should fail"; fails=1; fi
  out=$("$self" --dry-run 2>&1); rc=$?
  if (( rc != 0 )); then echo "PASS: no widget rejected"; else echo "FAIL: no widget should fail"; fails=1; fi
  out=$(PATH="/usr/bin:/bin" "$self" confirm -t "Sure?" 2>&1); rc=$?
  if (( rc != 0 )) && [[ $out == *"termux-api not available"* ]]; then echo "PASS: graceful degradation message"; else echo "FAIL: guard rc=$rc out=$out"; fails=1; fi
  out=$("$self" --help 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"Usage:"* ]]; then echo "PASS: --help"; else echo "FAIL: --help rc=$rc"; fails=1; fi
  (( fails == 0 )) && echo "txdialog selftest: all PASS" || echo "txdialog selftest: $fails FAILURES"
  return $fails
}

widget="" title="" hint="" values="" range="" password=0 number=0 dryrun=0
while (( $# )); do
  case $1 in
    --help|-h) usage; exit 0;;
    --selftest) selftest; exit $?;;
    --dry-run) dryrun=1;;
    -t|--title) shift; title=${1:?-t needs a value};;
    -i|--hint) shift; hint=${1:?-i needs a value};;
    -v|--values) shift; values=${1:?-v needs a value};;
    -r|--range) shift; range=${1:?-r needs a value};;
    --password) password=1;;
    --number) number=1;;
    -*) echo "$prog: unknown option: $1" >&2; usage >&2; exit 1;;
    *) [[ -z $widget ]] && widget=$1 || { echo "$prog: unexpected argument: $1" >&2; exit 1; };;
  esac
  shift
done
case $widget in
  "" ) echo "$prog: need a widget (confirm|text|radio|checkbox|sheet|spinner|date|time|counter)" >&2; usage >&2; exit 1;;
  confirm|text|radio|checkbox|sheet|spinner|date|time|counter) ;;
  *) echo "$prog: unknown widget '$widget' (confirm|text|radio|checkbox|sheet|spinner|date|time|counter)" >&2; exit 1;;
esac
case $widget in radio|checkbox|sheet|spinner)
  [[ -n $values ]] || { echo "$prog: $widget needs -v \"a,b,c\"" >&2; exit 1; };;
esac
[[ -z $range || $range =~ ^[0-9]+,[0-9]+,[0-9]+$ ]] || { echo "$prog: -r must be MIN,MAX,STEP numbers, got '$range'" >&2; exit 1; }
args=("$widget")
[[ -n $title ]] && args+=(-t "$title")
[[ -n $hint ]] && args+=(-i "$hint")
[[ -n $values ]] && args+=(-v "$values")
[[ -n $range ]] && args+=(-r "$range")
(( password )) && args+=(--password)
(( number )) && args+=(--number)
if (( dryrun )); then printf 'termux-dialog'; printf ' %q' "${args[@]}"; printf '\n'; exit 0; fi
command -v termux-dialog >/dev/null 2>&1 || {
  printf '%s: termux-api not available (termux-dialog not found)\n' "$prog" >&2
  printf '%s: run this on Android with Termux:API installed. There is no Linux equivalent for Android dialogs.\n' "$prog" >&2
  exit 1; }
termux-dialog "${args[@]}" | pretty_json
