#compdef apy

__model() {
  _arguments $opts_help '*:: :->subcmds' && return 0

  local -a subcmds_model
  subcmds_model=( \
    'edit-css:Edit CSS template for specified model' \
    'rename:Rename model' \
    )

  if (( CURRENT == 1 )); then
    _describe -t commands 'apy commands / project' subcmds_model
    return
  fi

  case "$words[1]" in
    edit-css)
      opts=( \
        '(-m --model)'{-m,--model}'[Specify model]:model:' \
        '(-s --sync-after)'{-s,--sync-after}'[Synchronize after edit]' \
        $opts_help \
        );;
    rename)
      opts=( \
        '::old-name' \
        '::new-name' \
        $opts_help \
        );;
  esac

  _arguments $opts
}

_apy() {
  zstyle ":completion:*:*:apy:*" sort false

  local -a opts opts_help subcmds

  opts_help=( '(-h --help)'{-h,--help}'[Show help]' )

  opts=( \
    $opts_help
    '(-b --base)'{-b,--base}'[Set base directory]:Anki base directory:_files -/' \
    )

  subcmds=( \
    'add:Add notes interactively from terminal' \
    'add-single:Add a single note from command line arguments' \
    'add-from-file:Add notes from Markdown file (alias for update-from-file)' \
    'update-from-file:Update existing or add new notes from Markdown file' \
    'check-media:Check media' \
    'info:Print some basic statistics' \
    'model:Interact with the models' \
    'list:Print cards that match query' \
    'review:Review/Edit notes that match query [default: marked/flagged]' \
    'reposition:Reposition new card with given CID' \
    'sync:Synchronize collection with AnkiWeb' \
    'tag:Add or remove tags from notes that match query' \
    )

  _arguments $opts '*:: :->subcmds' && return 0

  if (( CURRENT == 1 )); then
    _describe -t commands 'apy commands' subcmds
    return
  fi

  case "$words[1]" in
    add)
      opts=( \
        '(-t --tags)'{-t,--tags}'[Specify tags]:tags:' \
        '(-m --model)'{-m,--model}'[Specify model]:model:' \
        '(-d --deck)'{-d,--deck}'[Specify deck]:deck:' \
        $opts_help \
        );;
    add-single)
      opts=( \
        '(-s --preset)'{-s,--preset}'[Specify a preset]:preset:' \
        '(-t --tags)'{-t,--tags}'[Specify tags]:tags:' \
        '(-m --model)'{-m,--model}'[Specify model]:model:' \
        '(-d --deck)'{-d,--deck}'[Specify deck]:deck:' \
        '::Fields' \
        $opts_help \
        );;
    add-from-file|update-from-file)
      opts=( \
        '::Markdown input file:_files -g "*.md"' \
        '(-t --tags)'{-t,--tags}'[Specify tags]:tags:' \
        '(-d --deck)'{-d,--deck}'[Specify deck]:deck:' \
        '(-u --update-file)'{-u,--update-file}'[Update original file with note IDs]' \
        $opts_help \
        );;
    info)
      opts=( $opts_help );;
    model) __model; return;;
    list)
      opts=( \
        '::Query' \
        '(-w --wrap)'{-w,--wrap}'[Wrap the question and answer on multiple lines]' \
        '(-a --show-answer)'{-a,--show-answer}'[Display answer]' \
        '(-m --show-model)'{-m,--show-model}'[Display model]' \
        '(-c --show-cid)'{-c,--show-cid}'[Display card ids]' \
        '(-d --show-due)'{-d,--show-due}'[Display card due time in days]' \
        '(-t --show-type)'{-t,--show-type}'[Display card type]' \
        '(-e --show-ease)'{-e,--show-ease}'[Display card ease]' \
        '(-l --show-lapses)'{-l,--show-lapses}'[Display card number of lapses]' \
        $opts_help \
        );;
    review)
      opts=( \
        '(-q --query)'{-q,--query}'[Query string]:query:' \
        $opts_help \
        );;
    reposition)
      opts=( \
        '::New position for CID' \
        '::Query' \
        $opts_help \
        );;
    tag)
      opts=( \
        '(-a --add-tags)'{-a,--add-tags}'[Add specified tags]:tags:' \
        '(-r --remove-tags)'{-r,--remove-tags}'[Remove specified tags]:tags:' \
        $opts_help \
        '::Query' \
        );;
    *)
      opts=( $opts_help );;
  esac

  _arguments $opts
}

_apy "$@"
