#compdef zplugin zpl zplg

local context state state_descr line ret=1
typeset -A opt_args

typeset -a commands
commands=(
    zstatus:'overall Zplugin status'
    self-update:'updates Zplugin'
    help:'usage information'
    load:'load plugin'
    unload:'unload plugin'
    update:'update plugin (Git)'
    update-all:'update all plugins (Git)'
    status:'status for plugin (Git)'
    status-all:'status for all plugins (Git)'
    report:'show plugin'"'"'s report'
    all-reports:'show all plugin reports'
    loaded:'show what plugins are loaded'
    list:'show what plugins are loaded'
    clist:'list completions in use'
    completions:'list completions in use'
    cdisable:'disable completion'
    cenable:'enable completion'
    creinstall:'install completions for plugin'
    cuninstall:'uninstall completions for plugin'
    csearch:'search for available completions from any plugin'
    compinit:'refresh installed completions'
)

_arguments \
    '1: :->command'\
    '*: :->argument' && ret=0

case $state in
    command)
        _describe -t commands "Zplugin command" commands && ret=0
        ;;
    argument)
        case $words[2] in
            help)
                _message "Hit enter to get usage information" && ret=0
                ;;
            zstatus)
                _message "Hit enter to get overall status information" && ret=0
                ;;
            load|light|update|status)
                typeset -a plugins
                plugins=( "$ZPLG_PLUGINS_DIR"/*(N:t) )
                plugins=( "${plugins[@]/---//}" )
                plugins=( "${plugins[@]:#_local/zplugin}" )
                plugins=( "${plugins[@]:#custom}" )

                _wanted plugins expl "Plugins" \
                    compadd "$@" -a - plugins && ret=0
                ;;
            unload)
                typeset -a plugins
                plugins=( "${ZPLG_REGISTERED_PLUGINS[@]:#_local/zplugin}" )
                _wanted plugins expl "Plugins" \
                    compadd "$@" -a - plugins && ret=0
                ;;
            report)
                typeset -a plugins
                plugins=( "${ZPLG_REGISTERED_PLUGINS[@]:#_local/zplugin}" )
                _wanted plugins expl "Plugins" \
                    compadd "$@" -a - plugins && ret=0
                ;;
            all-reports)
                _message "Hit enter to get all reports (for all loaded plugins)" && ret=0
                ;;
            loaded|list)
                _message "Hit enter or give part of plugin name" && ret=0
                ;;
            clist|completions)
                _message "Hit enter to get list of completions" && ret=0
                ;;
            cdisable)
                # Find enabled completions
                typeset -a completions
                completions=( "$ZPLG_COMPLETIONS_DIR"/_*(N:t) )
                completions=( "${completions[@]#_}" )
                compadd "$@" -a - completions && ret=0
                ;;
            cenable)
                # Find disabled
                typeset -a completions
                completions=( "$ZPLG_COMPLETIONS_DIR"/[^_]*(N:t) )
                compadd "$@" -a - completions && ret=0
                ;;
            creinstall)
                # Complete only plugins that have any completions
                # We must iterate each plugin to check
                # for completions that can be installed
                typeset -a plugins completions
                local p c user plugin
                for p in "${ZPLG_PLUGINS_DIR[@]}"/*; do
                    completions=( "$p"/_*(N) )
                    for c in "${completions[@]}"; do
                        p="${p:t}"
                        user="${p%%---*}"
                        plugin="${p#*---}"
                        plugins+=( "$user/$plugin" )
                        break
                    done
                done
                _wanted plugins expl "Plugins" \
                    compadd "$@" -a - plugins && ret=0
                ;;
            cuninstall)
                # We must iterate each plugin and check if
                # it has completions that are installed
                typeset -a plugins completions
                local p c user plugin
                for p in "${ZPLG_PLUGINS_DIR[@]}"/*; do
                    completions=( "$p"/_*(N) )
                    for c in "${completions[@]}"; do
                        cfile="${c:t}"
                        bkpfile="${cfile#_}"
                        # Completion installed, either enabled or disabled?
                        if [[ -e "$ZPLG_COMPLETIONS_DIR"/"$cfile" || -e "$ZPLG_COMPLETIONS_DIR"/"$bkpfile" ]]; then
                            p="${p:t}"
                            user="${p%%---*}"
                            plugin="${p#*---}"
                            plugins+=( "$user/$plugin" )
                            break
                        fi
                    done
                done
                _wanted plugins expl "Plugins" \
                    compadd "$@" -a - plugins && ret=0
                ;;
            compinit)
                ret=0
                ;;
            *)
                ret=1
                ;;
        esac
esac

return "$ret"
