#compdef screenflow

_screenflow() {
  local state line
  typeset -A opt_args

  _arguments -C \
    '(-v --version)'{-v,--version}'[output the version number]' \
    '(-h --help)'{-h,--help}'[display help]' \
    '1: :->cmd_or_file' \
    '*:: :->args'

  case $state in
    cmd_or_file)
      local -a subcommands
      subcommands=(
        'video:Create a 9-second animated marketing video'
        'appstore:Generate a ready-to-upload App Store screenshot (1242×2688)'
        'mcp:Manage the MCP server for AI agents (install/uninstall)'
        'devices:List all available devices and their colors'
        'config:Show saved defaults'
        'set-default:Set a default device and color interactively'
        'author:About the author'
      )
      _alternative \
        'subcommands:subcommand:_describe "command" subcommands' \
        'files:screenshot:_files -g "*.png *.jpg *.jpeg *.heic *.heif *.webp *.mp4 *.mov *.m4v"'
      ;;
    args)
      case $line[1] in
        video)       _screenflow_video ;;
        appstore)    _screenflow_appstore ;;
        mcp)         _values 'mcp subcommand' 'install[Register with detected AI agents]' 'uninstall[Remove the registration]' ;;
        devices|config|set-default|author) ;;
        *)           _screenflow_frame ;;
      esac
      ;;
  esac
}

_screenflow_colors() {
  local device=${1:-iphone-17-pro}
  local -a colors
  case $device in
    iphone-17-pro)  colors=(cosmic-orange deep-blue silver) ;;
    iphone-17)      colors=(black lavender mist-blue sage white) ;;
    iphone-16-pro)  colors=(black natural white) ;;
    iphone-16)      colors=(black pink teal ultramarine white) ;;
    iphone-air)     colors=(black blue gold white) ;;
    ipad-pro-11)    colors=(silver silver-with-apple-pencil space-gray space-gray-with-apple-pencil) ;;
    ipad-pro-13)    colors=(silver space-gray) ;;
    imac)           colors=(blue green orange pink purple silver yellow) ;;
    *)              colors=(cosmic-orange) ;;
  esac
  echo "${colors[@]}"
}

_screenflow_device_arg() {
  local device='iphone-17-pro'
  for ((i = 2; i < ${#words[@]}; i++)); do
    if [[ ${words[$i]} == (-d=*|--device=*) ]]; then
      device=${words[$i]#*=}; break
    elif [[ (${words[$i]} == '-d' || ${words[$i]} == '--device') && -n ${words[$((i+1))]} ]]; then
      device=${words[$((i+1))]}; break
    fi
  done
  echo $device
}

_screenflow_frame() {
  local device=$(_screenflow_device_arg)
  local colors=($(_screenflow_colors $device))

  _arguments \
    '(-o --output)'{-o+,--output=}'[output file path]:output file:_files' \
    '--png[output as PNG instead of SVG (still images only)]' \
    '--jpeg[output as JPEG instead of SVG (still images only)]' \
    '--mute[drop the audio track (screen recordings only)]' \
    '(-d --device)'{-d+,--device=}'[device frame]:device:(iphone-17-pro iphone-17 iphone-16-pro iphone-16 iphone-air ipad-pro-11 ipad-pro-13 imac)' \
    '(-c --color)'{-c+,--color=}'[frame color]:color:('"${colors[*]}"')' \
    ':screenshot:_files -g "*.png *.jpg *.jpeg *.heic *.heif *.webp *.mp4 *.mov *.m4v"'
}

_screenflow_video() {
  local device=$(_screenflow_device_arg)
  local colors=($(_screenflow_colors $device))

  _arguments \
    '(-o --output)'{-o+,--output=}'[output file path]:output file:_files' \
    '(-d --device)'{-d+,--device=}'[device frame]:device:(iphone-17-pro iphone-17 iphone-16-pro iphone-16 iphone-air ipad-pro-11 ipad-pro-13 imac)' \
    '(-c --color)'{-c+,--color=}'[frame color]:color:('"${colors[*]}"')' \
    '(-s --style)'{-s+,--style=}'[animation style]:style:(zoom-in zoom-out pan-down pan-left pan-right)' \
    '(-t --tilt)'{-t+,--tilt=}'[perspective tilt in degrees (0–45)]:degrees:' \
    '--fps=[frame rate]:fps:(24 30 60 120)' \
    '--mute[drop the audio track (screen recordings only)]' \
    ':screenshot:_files -g "*.png *.jpg *.jpeg *.heic *.heif *.webp *.mp4 *.mov *.m4v"'
}

_screenflow_appstore() {
  local device=$(_screenflow_device_arg)
  local colors=($(_screenflow_colors $device))

  _arguments \
    '(-o --output)'{-o+,--output=}'[output file path]:output file:_files' \
    '--caption=[headline text rendered above the device]:caption:' \
    '--align=[caption alignment]:align:(left center right)' \
    '--bg=[background color (hex, e.g. #0A84FF)]:color:' \
    '--jpeg[output as JPEG instead of PNG]' \
    '(-d --device)'{-d+,--device=}'[device frame]:device:(iphone-17-pro iphone-17 iphone-16-pro iphone-16 iphone-air ipad-pro-11 ipad-pro-13 imac)' \
    '(-c --color)'{-c+,--color=}'[frame color]:color:('"${colors[*]}"')' \
    ':screenshot:_files -g "*.png *.jpg *.jpeg *.heic *.heif *.webp"'
}

_screenflow "$@"
