#!/bin/bash

_plask()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local prev=${COMP_WORDS[COMP_CWORD-1]}

    local IFS=$'\n'

    local i=1
    for w in ${COMP_WORDS[@]}; do
        if [ $i == ${#COMP_WORDS[@]} ]; then break; fi
        if [ "$w" == "-c" -o "$w" == "-i" -o $w == "-m" ]; then
            COMPREPLY=($(compgen -f -- "$cur"))
            return 0
        fi
        i=$(($i+1))
    done

    case "$prev" in
        -l)
            COMPREPLY=($(compgen -W $'critical_error\ncritical\nerror\nerror_detail\nwarning\ninfo\nresult\ndata\ndetail\ndebug\nansi\nmono' -- "$cur"))
            return 0
        ;;
        -V|-s|-h|--help|--version)
            return 0
        ;;
        *)
            if [[ "$cur" == -l* ]]; then
                COMPREPLY=($(compgen -W $'-lcritical_error\n-lcritical\n-lerror\n-lerror_detail\n-lwarning\n-linfo\n-lresult\n-ldata\n-ldetail\n-ldebug\n-lansi\n-lmono' -- "$cur"))
            elif [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W $'--help\n--version\n-c\n-h\n-i\n-l\n-m\n-p\n-s\n-u\n-V\n-x' -- "$cur"))
            elif [[ "$prev" == *= ]]; then
                if [[ "$cur" != "None" && "$cur" != "True" && "$cur" != "False" &&
                     ! "$cur" =~ ^[+-]?([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][+-]?[0-9]+)?([+-]([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][+-]?[0-9]+)?j)?$ ]]; then
                    COMPREPLY=("'${cur#*=}'")
                else
                    COMPREPLY=("$cur")
                fi
            else
                COMPREPLY=($(compgen -f -X "!+(*.+(xpl|py)|*tests/python/+(main|xpl))" -o plusdirs -- "$cur"))
            fi
            return 0
        ;;
    esac
}
complete -o filenames -F _plask plask
