# dwb completion support.

__script() {
    [[ -d ~/.config/dwb/userscripts/ ]] || return 0
    files=( ~/.config/dwb/userscripts/*.js )
    COMPREPLY=( $( compgen -W "$files" -- $cur ) )
}

__profile() {
    COMPREPLY=( $( compgen -W "$( while read line; do
        [[ "$line" == \[* ]] && {
            line="${line#\[}"
            printf "${line%\]} "
        } done < "${HOME}/.config/dwb/settings" )" -- $cur ) )
}


__session() {
    local s p_found profile
    s=${#COMP_WORDS[*]}
    p_found=false

    for (( i=1; i <= s; i++ )); do
        $p_found && { profile=${COMP_WORDS[$i]}; break;}
        [[ ${COMP_WORDS[$i]} == -p ]] ||\
        [[ ${COMP_WORDS[$i]} == --profile ]] &&\
            p_found=true
    done
    $p_found || profile=default

    COMPREPLY=( $( compgen -W "$(
        while read line; do
            [[ "$line" == 'g:'* ]] && {
                line="${line#g\:}"
                printf "${line#\*} "
            }
        done < "${HOME}/.config/dwb/${profile}/session" )" -- $cur ) )
}

__execute() {
    local cmds=""
    COMPREPLY=( $( compgen -W "${cmds}" -- $cur ) )
}

__extension() {
    COMPREPLY=( $( compgen -W "$( while read line; do
        printf "${line% *} "
    done < "${HOME}/.local/share/dwb/extensions/.metadata" )" -- $cur ) )
}

__get_installed() {
    while read line; do
        printf "${line% *} "
    done < "${HOME}/.local/share/dwb/extensions/.installed"
}

__get_disabled() {
    if [ -f "${HOME}/.config/dwb/userscripts/extension_loader.js" ]; then
        while read line; do
            if [[ "$line" == '/*<'*'__DISABLED' ]]; then
                line="${line#/\*<}"
                printf "${line%___DISABLED} "
            fi
        done < ${HOME}/.config/dwb/userscripts/extension_loader.js
    fi
}

__installed_extension() {
    COMPREPLY=( $( compgen -W "$(__get_installed)" -- $cur ) )
}

__disabled_extension() {
    COMPREPLY=( $( compgen -W "$(__get_disabled)" -- $cur ) )
}
__enabled_extension() {
    local disabled="$(__get_disabled)"
    COMPREPLY=( $( compgen -W "$( for installed in $(__get_installed); do 
        [[  " ${disabled} " != *" ${installed} "* ]] && printf "${installed} "
    done)" -- $cur ) )
}
__archive() {
    COMPREPLY=( $( compgen -W "$( case $prev in 
        --archive) printf 'a h d e l p s u ';;
        *) _filedir;;
    esac)"  -- $cur ) )
}

#__dwbremote_commands() {
#    local pprev=${COMP_WORDS[COMP_CWORD-2]}
#    case "$pprev" in 
#        -p|-i|-c|-name)
#            printf "execute get prompt pwd_prompt hook bind";;
#        *)
#            printf "";;
#    esac
#}
#__dwbremote_arguments() {
#    local opts
#    local prev=${COMP_WORDS[COMP_CWORD-1]}
#    local cur=${COMP_WORDS[COMP_CWORD]}
#    case "${prev}" in 
#        get) 
#            opts="uri title host domain all_titles all_domains all_hosts
#            all_uris ntabs current_tab" ;;
#        hook) 
#            opts="navigation load_finished hook load_committed" ;;
#        #execute)
#        #    opts=$(awk -F '=' 'BEGIN { a=0 } /\[\w+\]/ { a=a+1 } /^\s*$/ { next } a==1 { printf " "$1 }' $HOME/.config/dwb/keys)
#            #;;
#        *) 
#            opts="$(__dwbremote_commands)"
#    esac
#    COMPREPLY=( $( compgen -W "$opts" -- $cur ) )
#
#
#}
#
#_dwbremote()
#{
#    local cur prev opts
#    _init_completion || return 
#
#    opts="-id -pid -class -name"
#
#    case "${cur}" in
#        -*) COMPREPLY=( $( compgen -W "$opts" -- $cur ) )
#            return 0;;
#        *) __dwbremote_arguments
#            return 0;;
#    esac
#} && 
#complete -F _dwbremote dwbremote

_dwb() {
    local cur prev opts lopts
    _init_completion || return 

    opts="-h -c -d -e -f -l -n -r -R -p -x -v -S -V -t"
    lopts="--help --check-script --delete-profile --embed --force --list-sessions --new-instance
    --restore --override-restore --profile --execute --version --enable-scripts
    --set-as-default --fifo --version-full --temp-profile"

    case "${prev}" in
        -c|--check-script)
            __script
            return 0;;
        -r|--restore)
            __session
            return 0;;
        -p|--profile)
            __profile
            return 0;;
        -d|--delete-profile)
            __profile
            return 0;;
        -x|--execute)
            __execute
            return 0;;
    esac

    case "${cur}" in
        --*)
            COMPREPLY=( $( compgen -W "${lopts}" -- $cur ) )
            return 0;;
        -*)
            COMPREPLY=( $( compgen -W "${opts} ${lopts}" -- $cur ) )
            return 0;;
        *)
            _filedir
            return 0;;
    esac
} &&
complete -F _dwb dwb

_dwbem() {
    local cur prev opts lopts
    _init_completion || return 

    opts="-h -a -b -B -c -d -e -E -i -I -l -L -n -N -r -p -u -U"
    lopts="--help --list-all --bind --setbind --config --disable --enable --edit
    --install --info --list-installed --setload --no-config --no-confirm
    --remove --proxy --upgrade --update --archive"

    extinstops=" -B --setbind -c --config -d --disable -e --enable -E --edit
    -L --setload -r --remove -U --update "
    extallops=" -i --install -I --info "

    [[ " -d --disable " == *" ${prev} "* ]] && {
        __enabled_extension
        return 0
    }
    [[ " -e --enable " == *" ${prev} "* ]] && {
        __disabled_extension
        return 0
    }
    [[ " --archive " == *" ${prev} "* ]] && {
        __archive
        return 0
    }
    [[ "${extinstops}" == *" ${prev} "* ]] && {
        __installed_extension
        return 0
    }
    [[ "${extinstops}${extallops}" == *" ${prev} "* ]] && {
        __extension
        return 0
    }

    case "${cur}" in
        --*)
            COMPREPLY=( $( compgen -W "${lopts}" -- $cur ) )
            return 0;;
        -*)
            COMPREPLY=( $( compgen -W "${opts} ${lopts}" -- $cur ) )
            return 0;;
        *)
            _filedir
            return 0;;
    esac
} && 
complete -F _dwbem dwbem

# vim: set ft=sh:
