#autoload

__girar_command()
{
    local cmd tag remote idx

    remote=()
    idx=${words[(I)-R|-remote|--remote=*]}
    (( idx )) && case ${words[$idx]} in
	(-R|--remote) remote=(-R "${words[((idx+1))]}");;
	(--remote=*) remote=(-R "${${words[$idx]}#--remote=}");;
    esac

    if zstyle -s ":completion:${curcontext}:${1}" command cmd ||
	cmd="$2"

    "$cmd" "$remote[@]" "${@[3,-1]}"
}

_girar_info-directories()
{
    local expl
    local -a remfiles

    if compset -P '?*/'; then
	remfiles=(${${${${${${${(M)${${(f)"$(__girar_command package-repository girar-remote ls $1${words[CURRENT]%/*})"}:#total *}:#d*}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##})
    elif compset -P /; then
	remfiles=(people archive gears)
    else
	remfiles=(${${${${${${${(M)${${(f)"$(__girar_command package-repository girar-remote ls $1)"}:#total *}:#d*}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##})
    fi

    _wanted files expl 'girar directories' \
	compadd -S/ -q -- "$remfiles[@]"
}

_girar_info-tasks()
{
    local -a tasks

    tasks=(${${${${(f)"$(__girar_command task-id girar-remote task ls --repo=ALL --state=ALL)"}#\#}//:/\\:}/ /:})
    _describe -t task-id 'task id' tasks -V tasks
}

_girar_info-all-tasks()
{
    local -a tasks

    tasks=(${${${${(f)"$(__girar_command any-task-id girar-remote task ls --repo=ALL --state=ALL --user=ALL)"}#\#}//:/\\:}/ /:})
    _describe -t any-task-id 'task id' tasks -V tasks
}

_girar_info-subtasks()
{
    local -a subtasks

    # Assumes that task id comes right before subtask id
    subtasks=(${(M)${${(f)"$(__girar_command subtask-id girar-remote task show --brief "$@")"}# }:#<->*})
    _describe -t subtask-id 'subtask id' subtasks -V subtasks
}

_girar_info-binary-repositories()
{
    local -a repos

    repos=( ${(f)"$(__girar_command binary-repository girar-remote acl --list)"} )
    _describe -t binary-repository 'binary repository' repos -V repos
}

_girar_info-package-repositories()
{
    local -a repos

    repos=( ${${${${${${${${(M)${${(f)"$(__girar_command package-repository girar-remote ls packages)"}:#total *}:#d*}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}%.git} )
    _describe -t package-repository 'package repository' repos
}

_girar_info-package-tags()
{
    local uri pattern
    local -a tags

    if zstyle -s ":completion:${curcontext}:package-tag" tag-pattern pattern ||
	pattern="*-alt*"

    uri="$(__girar_command uri girar-remote-uri "$@")"
    tags=( ${(M)${${${${(f)"$(_call_program package-tag git ls-remote -t "$uri")"}:#*\^\{\}}##*	}#refs/tags/}:#$~pattern} )
    _describe -t package-tag 'package tag' tags -V tags
}

_girar_info-repository-branches()
{
    local uri
    local -a tags

    uri="$(__girar_command uri girar-remote-uri "$@")"
    branches=( ${${${(f)"$(_call_program package-tag git ls-remote -h "$uri")"}##*	}#refs/heads/} )
    _describe -t repository-branch 'repository branch' branches
}

_girar_info-srpms()
{
    local -a srpms

    srpms=( ${${${${${${${${(M)${${(f)"$(__girar_command source-rpm girar-remote ls incoming)"}:#total *}:#*.src.rpm}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}%.git} )
    _describe -t source-rpm 'source rpm' srpms
}

_girar_info-user()
{
    local expl
    local -a remusers

    remusers=(${${${${${${${(M)${${(f)"$(__girar_command users girar-remote ls /people)"}:#total *}:#d*}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##})

    _wanted users expl 'girar users' \
	compadd -- "$remusers[@]"
}

_girar_info-users()
{
    local expl
    local -a remusers

    remusers=(${${${${${${${(M)${${(f)"$(__girar_command users girar-remote ls /people)"}:#total *}:#d*}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##}##[^ ]## ##})

    _wanted users expl 'girar users' \
	compadd -S, -q -- "$remusers[@]"
}

_girar_info()
{
    local what ret
    local -a expl

    zparseopts -D -a expl q n 1 2 P: S: r: R: W: X+: M+: F: J+: V+:

    what="$1"
    [[ "$what" = (directories|tasks|all-tasks|subtasks|binary-repositories|package-repositories|package-tags|repository-branches|srpms|user|users) ]] || {
	_message "unknown command: $what"
	return
    }

    _call_function ret _girar_info-$what "${@[2,-1]}"

    return ret
}

_girar_info "$@"
