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

FPS=24
SECONDS_PER_EFFECT=5
WIDTH=0
HEIGHT=0

STORY_TEXT=$(cat <<'__KNOTT_TEXT__'
Knott Dynamics is dedicated to advancing
the field of humanoid robotics through the
principles of nature: tendons, leverage,
and movement.

We build machines that move
with purpose, adapt with intelligence,
and interact with the world
as extensions of life.

This compendium documents our
understanding of the mechanisms
that make it possible.

Knott Dynamics

A humanoid machine is not assembled
from parts alone.

It is composed of relationships:
force and resistance,
weight and balance,
signal and response.

Every joint must serve motion.
Every actuator must serve intention.
Every mechanism must disappear
into the grace of the whole.

Nature rarely pushes where it can pull.

The tendon is a line of force,
a messenger between motor and limb,
a cord that allows strength to travel
through distance, angle, and constraint.

In the humanoid form,
tendons allow the machine
to become less rigid,
less mechanical,
and more alive in motion.

Leverage is the quiet multiplication
of strength.

A small force, correctly placed,
may move a great weight.
A short rotation, correctly guided,
may command an entire limb.

The study of leverage is the study
of where power wishes to enter
the body.
__KNOTT_TEXT__
)

TEXT="${STORY_TEXT}"

usage() {
  printf 'Usage: ./text_effects.sh [--seconds N] [--fps N] [--width N] [--height N]\n'
  printf '  --seconds    seconds to play each effect (default: %s)\n' "$SECONDS_PER_EFFECT"
  printf '  --fps        frame rate for the demo (default: %s)\n' "$FPS"
  printf '  --width      override canvas width\n'
  printf '  --height     override canvas height\n'
  printf '  --text       override story text with a single quoted string\n'
  printf '  --help       show this message\n'
}

while [ "$#" -gt 0 ]; do
  case "$1" in
    --fps)
      FPS="$2"
      shift
      ;;
    --seconds)
      SECONDS_PER_EFFECT="$2"
      shift
      ;;
    --width)
      WIDTH="$2"
      shift
      ;;
    --height)
      HEIGHT="$2"
      shift
      ;;
    --text)
      TEXT="$2"
      shift
      ;;
    --help|-h)
      usage
      exit 0
      ;;
    *)
      printf 'Unknown argument: %s\n' "$1"
      usage
      exit 1
      ;;
  esac
  shift
done

WIDTH=${WIDTH:-$(tput cols 2>/dev/null || printf '120')}
HEIGHT=${HEIGHT:-$(tput lines 2>/dev/null || printf '36')}
if [ "$WIDTH" -le 0 ]; then
  WIDTH=120
fi
if [ "$HEIGHT" -le 0 ]; then
  HEIGHT=36
fi

DURATION=$((SECONDS_PER_EFFECT * FPS))
if [ "$DURATION" -lt 1 ]; then
  DURATION=90
fi

printf '\033[2J\033[?25l'
trap 'printf "\033[0m\033[?25h"; printf "\n"' EXIT

for effect in "${EFFECTS[@]}"; do
  printf '\033[2J\033[H'
  printf 'Knott Dynamics Text Effects • %s\n' "$effect"
  cargo run --quiet --example demo -- "$effect" --text "$TEXT" --width "$WIDTH" --height "$HEIGHT" --fps "$FPS" --duration "$DURATION"
  sleep 0.25
done
