#!/usr/bin/env bash
set -euo pipefail

EFFECTS=(
  beams
  binarypath
  blackhole
  bouncyballs
  bubbles
  burn
  colorshift
  crumble
  decrypt
  errorcorrect
  expand
  fireworks
  highlight
  laseretch
  matrix
  middleout
  orbittingvolley
  overflow
  pour
  print
  rain
  randomsequence
  rings
  scattered
  slice
  slide
  smoke
  spotlights
  spray
  swarm
  sweep
  synthgrid
  thunderstorm
  unstable
  vhstape
  waves
  wipe
)

usage() {
  printf 'Usage:\n'
  printf '  ./run.sh [effect|all|list] [text]\n\n'
  printf 'Examples:\n'
  printf '  ./run.sh wipe "Hello from Rust"\n'
  printf '  ./run.sh matrix "opencode style"\n'
  printf '  ./run.sh all "Aisling"\n'
  printf '  ./run.sh showcase\n'
  printf '  ./run.sh showcase --theme industrial\n'
  printf '  ./run.sh showcase --seconds 60\n'
  printf '  ./run.sh list\n'
}

contains_effect() {
  local wanted="$1"
  local effect
  for effect in "${EFFECTS[@]}"; do
    if [[ "$effect" == "$wanted" ]]; then
      return 0
    fi
  done
  return 1
}

run_effect() {
  local effect="$1"
  local text="$2"
  cargo run --quiet --example demo -- "$effect" "$text"
}

command="${1:-wipe}"
text="${2:-Aisling}"

case "$command" in
  -h|--help|help)
    usage
    ;;
  list)
    printf '%s\n' "${EFFECTS[@]}"
    ;;
  all)
    for effect in "${EFFECTS[@]}"; do
      printf '\033[2J\033[H'
      printf 'Effect: %s\n\n' "$effect"
      sleep 0.7
      run_effect "$effect" "$text"
      sleep 0.5
    done
    ;;
  showcase)
    shift || true
    cargo run --quiet --example showcase -- "$@"
    ;;
  *)
    if contains_effect "$command"; then
      run_effect "$command" "$text"
    else
      printf 'Unknown effect: %s\n\n' "$command" >&2
      usage >&2
      exit 1
    fi
    ;;
esac
