#compdef fvm
# ------------------------------------------------------------------------------
# Copyright (c) 2022 Github zsh-users - https://github.com/zsh-users
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
#  Completion script for fvm. (https://github.com/fluttertools/fvm)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Shohei Yoshida (https://github.com/syohex) <syohex@gmail.com>
#
# ------------------------------------------------------------------------------

_fvm() {
  typeset -A opt_args
  local context state line
  local curcontext="$curcontext"
  local ret=1

  _arguments -C -A "-*" \
    '(- *)'{-h,--help}'[Print this usage information]' \
    '--verbose[Print verbose output]' \
    '(- *)--version[current version]' \
    '1: :_fvm_subcommands' \
    '*::arg:->args' \
    && ret=0

  case "$state" in
    (args)
      case $words[1] in
        (help)
          _arguments \
           '(- *)'{-h,--help}'[Print this usage information]' \
           '1: :_fvm_subcommands' \
            && ret=0
          ;;
        (config)
          _arguments \
            '(- *)'{-h,--help}'[Print this usage information]' \
            '(-c --cache-path)'{-c,--cache-path}'[Set the path which FVM will cache the version.Priority over FVM_HOME]::_path_files -/' \
            '(-s --skip-setup --no-skip-setup)'{-s,--skip-setup}'[Skip setup after a version install]' \
            '(-s --skip-setup --no-skip-setup)--no-skip-setup[No skip setup after a version install]' \
            '(-g --git-cache --no-git-cache)'{-g,--git-cache}'[Will cache a local version of Flutter repo for faster version install]' \
            '(-g --git-cache --no-git-cache)--no-git-cache[Will not cache a local version of Flutter repo for faster version install]' \
            && ret=0
          ;;
        (destroy|doctor|flavor|list|releases)
          _arguments -C \
           '(- *)'{-h,--help}'[Print this usage information]' \
            && ret=0
          ;;
        (dart)
          _arguments \
           '1: :_fvm_run_dart' \
           '*: :_normal' \
            && ret=0
          ;;
        (exec)
          _arguments \
           '*::args:_normal' \
            && ret=0
          ;;
        (flutter)
          _arguments \
           '1: :_fvm_run_flutter' \
           '*: :_normal' \
            && ret=0
          ;;
        (global)
          _arguments \
           '(- *)'{-h,--help}'[Print this usage information]' \
           '*::args:_fvm_installed_versions' \
            && ret=0
          ;;
        (install)
          _arguments \
           '(- *)'{-h,--help}'[Print this usage information]' \
           '(-s --skip-setup)'{-s,--skip-setup}'[Skips Flutter setup after install]' \
           '1: :_fvm_versions' \
            && ret=0
          ;;
        (remove)
          _arguments \
           '(- *)'{-h,--help}'[Print this usage information]' \
           '--force[Skips version global check]' \
           '1: :_fvm_installed_versions' \
            && ret=0
          ;;
        (spawn)
          _arguments \
            '1: :_fvm_installed_versions' \
            '2: :_fvm_run_flutter' \
            '*: :_normal' \
            && ret=0
          ;;
        (use)
          _arguments \
           '(- *)'{-h,--help}'[Print this usage information]' \
           '(-f --force)'{-f,--force}'[Skips command guards that does Flutter project checks]' \
           '(-p --pin)'{-p,--pin}'[If version provided is a channel. Will pin the latest release of the channel]' \
           '--flavor[Sets version for a project flavor]' \
           '(-s --skip-setup)'{-s,--skip-setup}'[Skips Flutter setup after install]' \
           '1: :_fvm_installed_versions' \
            && ret=0
          ;;
      esac
      ;;
  esac

  return ret
}

(( $+functions[_fvm_subcommands] )) ||
_fvm_subcommands() {
  local -a commands=(
    'config:Set configuration for FVM'
    'dart:Proxies Dart Commands'
    'destroy:Destroy FVM cache by deleting FVM directory'
    'doctor:Shows information about environment, and project configuration'
    'exec:Executes scripts with the configured Flutter SDK'
    'flavor:Switches between different project flavors'
    'flutter:Proxies Flutter Commands'
    'global:Sets Flutter SDK Version as a global'
    'install:Installs Flutter SDK version'
    'list:Lists installed Flutter SDK Versions'
    'releases:View all Flutter SDK releases available for install'
    'remove:Removes Flutter SDK Version'
    'spawn:Spawns a command on a Flutter version'
    'use:Sets Flutter SDK Version you would like to use in a project'
  )
  _describe -t commands 'command' commands "$@"
}

(( $+functions[_fvm_versions] )) ||
_fvm_versions() {
  local -a versions
  versions=($(fvm releases | awk '/[0-9]+\.[0-9]+\.[0-9]+/{ sub(/^[^│]*│ /, ""); print $1}'))
  versions=(master beta stable $versions)
  _describe 'versions' versions
}

(( $+functions[_fvm_installed_versions] )) ||
_fvm_installed_versions() {
  local -a versions
  versions=($(fvm list | sed -e '1,2d'))
  _describe 'installed_versions' versions
}

(( $+functions[_fvm_run_flutter] )) ||
_fvm_run_flutter() {
  local begin=$(($CURRENT - 1))
  if (( $+functions[_flutter] )); then
    compset -n $begin
    _flutter "$@"
  fi
}

(( $+functions[_fvm_run_dart] )) ||
_fvm_run_dart() {
  local begin=$(($CURRENT - 1))
  if (( $+functions[_dart] )); then
    compset -n $begin
    _dart "$@"
  fi
}

_fvm "$@"

# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et
