__systemctl() {
        local mode=$1; shift 1
        systemctl $mode --full --no-legend "$@"
}

__systemd_properties() {
        local mode=$1
        { __systemctl $mode show --all;
         /lib/systemd/systemd --dump-configuration-items; } |
        while IFS='=' read -r key value; do
            [[ $value ]] && echo "$key"
        done
}

__contains_word () {
        local w word=$1; shift
        for w in "$@"; do
                [[ $w = "$word" ]] && return
        done
}

_modal_systemctl () {

    COMPREPLY=()
    local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
    local i verb comps mode

    local -A OPTS=(
           [STANDALONE]='--all -a --reverse --after --before --defaults --failed --force -f --full -l --global
                         --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall
                         --quiet -q --privileged -P --system --user --version --runtime --recursive -r --firmware-setup'
                  [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root'
    )

    if __contains_word "--user" ${COMP_WORDS[*]}; then
        mode=--user
    else
        mode=--system
    fi

    if __contains_word "$prev" ${OPTS[ARG]}; then
            case $prev in
                    --signal|-s)
                            comps=$(compgen -A signal)
                    ;;
                    --type|-t)
                            comps=$(__systemctl $mode -t help)
                    ;;
                    --state)
                            comps='loaded not-found stub
                                   active inactive
                                   dead elapsed exited listening mounted plugged running waiting'
                    ;;
                    --job-mode)
                            comps='fail replace replace-irreversibly isolate
                                   ignore-dependencies ignore-requirements flush'
                    ;;
                    --kill-who)
                            comps='all control main'
                    ;;
                    --root)
                            comps=$(compgen -A directory -- "$cur" )
                            compopt -o filenames
                    ;;
                    --host|-H)
                            comps=$(compgen -A hostname)
                    ;;
                    --property|-p)
                            comps=$(__systemd_properties $mode)
                    ;;
            esac
            COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
            return 0
    fi

    if [[ "$cur" = -* ]]; then
            COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
            return 0
    fi


    local -A VERBS=(
          [MODAL_UNITS]='is-active is-failed is-enabled status show cat mask unmask 
                         preset help list-dependencies edit disable enable reenable 
                         reset-failed start stop condstop kill try-restart condrestart 
                         isolate reload condreload reload-or-try-restart force-reload 
                         restart reload-or-restart'
                 [JOBS]='cancel'
            [SNAPSHOTS]='delete'
                 [ENVS]='set-environment unset-environment'
           [STANDALONE]='daemon-reexec daemon-reload default
                         emergency exit halt hibernate hybrid-sleep kexec list-jobs
                         list-sockets list-timers list-units list-unit-files poweroff
                         reboot rescue show-environment suspend get-default
                         is-system-running'
                 [NAME]='snapshot'
                 [FILE]='link switch-root'
              [TARGETS]='set-default'
    )
    for ((i=0; i < COMP_CWORD; i++)); do
            if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
             ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
                    verb=${COMP_WORDS[i]}
                    break
            fi
    done

    if [[ -z $verb ]]; then
            comps="${VERBS[*]}"

    elif __contains_word "$verb" ${VERBS[MODAL_UNITS]}; then

            comps=$(ls /etc/systemd/system/ | egrep "voxl|modal|docker" | sed "s/.service//g" )
            COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
            return 0

    elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
            comps=''

    elif __contains_word "$verb" ${VERBS[JOBS]}; then
            comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )

    elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
            comps=$( __systemctl $mode list-units --type snapshot --full --all \
                    | { while read -r a b; do echo " $a"; done; } )

    elif __contains_word "$verb" ${VERBS[ENVS]}; then
            comps=$( __systemctl $mode show-environment \
                | while read -r line; do echo " ${line%%=*}=";done )
            compopt -o nospace

    elif __contains_word "$verb" ${VERBS[FILE]}; then
            comps=$( compgen -A file -- "$cur" )
            compopt -o filenames
    elif __contains_word "$verb" ${VERBS[TARGETS]}; then
            comps=$( __systemctl $mode list-unit-files --type target --full --all \
                    | { while read -r a b; do echo " $a"; done; } )
    fi


    COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
    return 0
}

complete -F _modal_systemctl systemctl
