#compdef conda
#description:conda package manager

local state line context
local -A opt_args

__conda_envs(){

    local -a envs
    envs=($( conda info -e | sed "1,2d" | cut -f1 -d' '))
    _describe -t envs 'conda environments' envs

}

__conda_packages_installed(){
    local -a packages
    packages=($(conda list | sed 1,2d | cut -f1 -d' '))
    _describe -t packages 'installed packages' packages
}

__conda_packages_search(){
    local -a packages
    packages=($(conda search --use-index-cache --json |
    python -c "
import json, sys
parsed = json.load(sys.stdin)
for k in parsed.keys():
    print(k)
        "))
    _describe -t packages 'available packages' packages
}

__conda_channels(){

    local -a channels
    channels=(default system)
    _describe -t channels 'conda channels' channels

}

__conda_commands(){
    local -a package maint environment help config special
    package=(
        search:'Search for packages and display their information.'
        install:'Install a list of packages into a specified conda environment.'
    )
    maint=(
        update:'Update conda packages.'
        clean:'Remove unused packages and caches.'
    )
    environment=(
        info:'Display information about current conda install.'
        create:'Create a new conda environment from a list of specified packages.'
        list:'List linked packages in a conda environment.'
        remove:'Remove a list of packages from a specified conda environment.'
    )
    help=(
        help:'Displays a list of available conda commands and their help strings.'
    )
    config=(
        config:'Modify configuration values in .condarc.'
    )
    special=(
        run:'Launches an application installed with Conda.'
        init:'Initialize conda into a regular environment. (DEPRECATED)'
        package:'Low-level conda package utility. (EXPERIMENTAL)'
        bundle:'Create or extract a "bundle package" (EXPERIMENTAL)'
    )
    _describe -t package_commands "package commands" package
    _describe -t maint_commands "maint commands" maint
    _describe -t environment_commands "environment commands" environment
    _describe -t help_commands "help commands"  help
    _describe -t config_commands "config commands"  config
    _describe -t special_commands "special commands"  special
}

local -a opts
opts=(
    '(-h --help)'{-h,--help}'[show this help message and exit]'
    '(-v --version)'{-v,--version}'[show program''s version number and exit]'
)

_arguments -C $opts \
           ': :->command' \
           '*:: :->subcmd'



base_opts=(
    '(-h --help)'{-h,--help}'[show the help message and exit]' \
    '(-n --name=)'{-n,--name=}'[name of environment]:environment:__conda_envs' \
    '(-p --prefix)'{-p,--prefix}'[full path to environment prefix]:path:_path_files' \
    '--unknown[use index metadata from the local package cache]' \
    '--use-index-cache[use cache of channel index files]' \
    '(-c --channel)'{-c,--channel}'[additional channel to search for packages]:channel:__conda_channels'\
    '--override-channels [do not search default or .condarc channels]' \
    '--json[report all output as json.]' \
    '--use-local[use locally built packages]' \
    )
install_opts=(
    '(-y --yes)'{-y,--yes}'[do not ask for confirmation]' \
    '--dry-run[only display what would have been done]' \
    '(-f --force)'{-f,--force}'[force install]' \
    '--file[read package versions from file]:file:_path_files' \
    '--no-deps[do not install dependencies]' \
    '(-m --mkdir)'{-m,--mkdir}'[create prefix directory if necessary]' \
    '--offline[offline mode, don''t connect to internet]' \
    '--no-pin[ignore pinned file]' \
    '(-q --quiet)'{-q,--quiet}'[do not display progress bar]'\
    '--copy[Install all packages using copies instead of hard- or soft-linking]' \
    '--alt-hint[Use an alternate algorithm to generate an unsatisfiable hint]' \
    )

case $state in
(command)
    __conda_commands
    ;;
(subcmd)
    case ${line[1]} in
    (info)
        _arguments -C '(-h --help)'{-h,--help}'[show the help message and exit]' \
                      '--json[report all output as json.]' \
                      '(-a --all)'{-a,--all}'[show all information, (environments, license, and system information]' \
                      '(-e --envs)'{-e,--envs}'[list all known conda environments]' \
                      '(-l --license)'{-l,--license}'[display information about local conda licenses list]' \
                      '(-s --system)'{-s,--system}'[list environment variables]' \
                      '--root[display root environment path]'
        ;;
    (help)
        _arguments -C '(-h --help)'{-h,--help}'[show the help message and exit]' \
                      '*:commands:__conda_commands' \
        ;;
    (list)
        _arguments -C '(-h --help)'{-h,--help}'[show the help message and exit]' \
                      '(-n --name=)'{-n,--name=}'[install into given conda environment]:environment:__conda_envs' \
                      '(-p --prefix)'{-p,--prefix}'[full path to environment prefix]:path:_path_files' \
                      '--json[report all output as json.]' \
                      '(-c --canonical)'{-c,--canonical}'[output canonical names of packages only]' \
                      '(-e --export)'{-e,--export}'[output requirement string only]' \
                      '(-r --revisions)'{-r,--revision}'[list the revision history and exit]' \
                      '--no-pip[Do not include pip-only installed packages]' \
                      '*:regex:' \
        ;;
    (search)
        _arguments -C $base_opts \
                      '(-c --canonical)'{-c,--canonical}'[output canonical names of packages only]' \
                      '(-o --outdated)'{-o,--outdated}'[only display installed but outdated packages]' \
                      '(-v --verbose)'{-v,--verbose}'[Show available packages as blocks of data]' \
                      '--platform[Search the given platform.]' \
                      '--spec[Treat regex argument as a package specification]' \
                      '*:regex:' \
        ;;
    (create)
        _arguments -C $base_opts \
                      $install_opts \
                      '--clone[path to (or name of) existing local environment]' \
                      '--no-default-packages[ignore create_default_packages in condarc file]' \
                      '*:packages:__conda_packages_search' \
        ;;
    (install)
        _arguments -C $base_opts \
                      $install_opts \
                      '--revision[revert to the specified revision]:revision' \
                      '*:packages:__conda_packages_search' \
        ;;
    (update)
        _arguments -C $base_opts \
                      $install_opts \
                      '--all[Update all installed packages in the environment]' \
                      '*:packages:__conda_packages_installed' \
        ;;
    (*)
        #_arguments -C $opts
        _path_files
        ;;
    esac
    ;;
esac

