#!/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
)

LOADERS=(
  bar
  tqdm
  blocks
  meter
  battery
  gauge
  dots
  pulse
  line
  bounce
  scanner
  snake
  pong
  marquee
  steps
  download
  upload
  equalizer
  wave
  matrix
  spark
  orbit
  brackets
  crawl
  braided
  burn
  decrypt
  rain
  thunderstorm
  laseretch
  vhstape
  blackhole
  fireworks
  pour
  synthgrid
  beams
  smoke
  binarypath
  bouncyballs
  bubbles
  colorshift
  crumble
  errorcorrect
  expand
  highlight
  middleout
  orbittingvolley
  overflow
  print
  randomsequence
  rings
  scattered
  slice
  slide
  spotlights
  spray
  swarm
  sweep
  unstable
  waves
  wipe
  ember
  cipher
  glitch
  vortex
  stormflicker
  lasersweep
  cascade
  gridpulse
)

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 --rainbow\n'
  printf '  ./run.sh showcase --theme industrial\n'
  printf '  ./run.sh showcase --seconds 60\n'
  printf '  ./run.sh story --seconds 90\n'
  printf '  ./run.sh loader-lab --seconds 120\n'
  printf '  ./run.sh new-loaders --seconds 24\n'
  printf '  ./run.sh loader tqdm --label download\n'
  printf '  ./run.sh loaders --seconds 20\n'
  printf '  ./run.sh list-loaders\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"
}

run_loader() {
  cargo run --quiet --example loaders -- "$@"
}

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

case "$command" in
  -h|--help|help)
    usage
    ;;
  list)
    printf '%s\n' "${EFFECTS[@]}"
    ;;
  list-loaders)
    printf '%s\n' "${LOADERS[@]}"
    ;;
  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 -- "$@"
    ;;
  story)
    shift || true
    cargo run --quiet --example story -- "$@"
    ;;
  loader-lab|loaders-lab)
    shift || true
    cargo run --quiet --example loader_lab -- "$@"
    ;;
  new-loaders|new_loaders)
    shift || true
    cargo run --quiet --example new_loaders -- "$@"
    ;;
  loader)
    shift || true
    run_loader "$@"
    ;;
  loaders)
    shift || true
    run_loader all "$@"
    ;;
  *)
    if contains_effect "$command"; then
      run_effect "$command" "$text"
    else
      printf 'Unknown effect: %s\n\n' "$command" >&2
      usage >&2
      exit 1
    fi
    ;;
esac
