#compdef wpg

_profiles() {
    local -a profiles
    profiles=(`wpg -l`)
    _describe 'profiles' profiles
}

_templates() {
    local -a templates
    templates=(`wpg -xl`)
    _describe 'templates' templates
}

_backends() {
    local -a backends
    backends=(`wpg --backend`)
    _describe 'backends' backends
}

_themes() {
    local -a themes
    themes=(`wpg --pywal`)
    _describe 'themes' themes
}

_wpg() {
    local state optforprofiles
    # themes
    optforprofiles=(-s -e -d -z -m -A -LA --brt --sat) 
    # themes, filenames
    optvariable=(-i -o)
    # themes, pywal themes
    optvariabletheme=(-Ti)
    # filenames
    optforgeneric=(-y -a -xa)
    # templates
    optfortemplates=(-xd -dx)
    # pywal backends
    optforbackends=(--backend)
    # pywal themes
    optforthemes=(--pywal)

    _arguments \
        '1: :->generic' \
        '*: :->listprofiles'

    case $state in
        (generic)
            _gnu_generic
            ;;
        (listprofiles)
            if [[ $CURRENT < 5 || ${words[2]} != "-s" ]]; then
                for opt in $optforprofiles; do
                    if [[ $opt = ${words[2]} ]]; then
                        _profiles
                    fi
                done
                for opt in $optvariable; do
                    if [[ $opt = ${words[2]} && $CURRENT < 4 ]]; then
                        _profiles
                    elif [[ $opt = ${words[2]} ]]; then
                        _gnu_generic
                    fi
                done
                for opt in $optvariabletheme; do
                    if [[ $opt = ${words[2]} && $CURRENT < 4 ]]; then
                        _profiles
                    elif [[ $opt = ${words[2]} ]]; then
                        _themes
                    fi
                done
                for opt in $optforgeneric; do
                    if [[ $opt = ${words[2]} ]]; then
                        _gnu_generic
                    fi
                done
                for opt in $optfortemplates; do
                    if [[ $opt = ${words[2]} ]]; then
                        _templates
                    fi
                done
                for opt in $optforbackends; do
                    if [[ $opt = ${words[2]} ]]; then
                        _backends
                    fi
                done
                for opt in $optforthemes; do
                    if [[ $opt = ${words[2]} ]]; then
                        _themes
                    fi
                done
            fi 
            ;;
    esac
}

_wpg "$@"
