# This file is part of sfdk.bash-completion                -*- shell-script -*-

__sfdk_complete_device_name()
{
    local objects=$(_sfdk misc inspect devices)
    IFS=$'\n' read -a COMPREPLY -d '' \
        <<<$(IFS=$'\n' compgen -W "$objects" -- "$cur")
    COMPREPLY=("${COMPREPLY[@]@Q}")
}

_sfdk_command_device()
{
    local commands=(list exec)

    if ((cword == _sfdk_command_idx + 1)); then
        COMPREPLY=($(compgen -W "${commands[*]}" -- "$cur"))
        return
    fi

    local objects=
    case ${words[_sfdk_command_idx + 1]} in
        list)
            _sfdk_no_defaults
            ;;
        exec)
            local -A options=(
                [-t]=
            )
            # Skip possible device name at start
            local non_option_idx=$(_sfdk_find_non_option --skip-at-start \
                $((_sfdk_command_idx + 2)) "$cword")
            if [[ $non_option_idx ]]; then
                # TODO Complete device command?
                return
            fi

            if [[ $cur == -* ]]; then
                COMPREPLY=($(compgen -W "${!options[*]} --" -- "$cur") "${COMPREPLY[@]}")
            else
                ((cword == _sfdk_command_idx + 2)) && __sfdk_complete_device_name
            fi
            ;;
    esac
}

__sfdk_complete_emulator_property_name()
{
    local properties=$(_sfdk emulator show |cut -d: -f1)
    COMPREPLY=($(compgen -W "$properties" -- "$cur"))
}

__sfdk_complete_emulator_property_value()
{
    case $prev in
        device-model)
            __sfdk_complete_device_model
            ;;
        orientation)
            COMPREPLY=($(compgen -W "portrait landscape" -- "$cur"))
            ;;
        downscale)
            _sfdk_complete_boolean_property
            ;;
        *)
            compopt +o default
            ;;
    esac
}

__sfdk_complete_emulator_property_assignment()
{
    if [[ $cur != *=* ]]; then
        compopt -o nospace
        __sfdk_complete_emulator_property_name
        COMPREPLY=("${COMPREPLY[@]/%/=}")
    else
        local prev=${cur%%=*} cur=${cur#*=}
        __sfdk_complete_emulator_property_value
    fi
}

__sfdk_complete_emulator_name()
{
    local objects=$(_sfdk misc inspect emulators)
    IFS=$'\n' read -a COMPREPLY -d '' \
        <<<$(IFS=$'\n' compgen -W "$objects" -- "$cur")
    COMPREPLY=("${COMPREPLY[@]@Q}")
}

__sfdk_complete_available_emulator_name()
{
    local objects=
    _sfdk_cache objects _sfdk emulator list --available
    objects=$(awk '($2 ~ /available/) { print $1 }' <<<$objects)
    IFS=$'\n' read -a COMPREPLY -d '' \
        <<<$(IFS=$'\n' compgen -W "$objects" -- "$cur")
    COMPREPLY=("${COMPREPLY[@]@Q}")
}

__sfdk_complete_device_model()
{
    local objects=$(_sfdk emulator device-model-list)
    IFS=$'\n' read -a COMPREPLY -d '' \
        <<<$(IFS=$'\n' compgen -W "$objects" -- "$cur")
    COMPREPLY=("${COMPREPLY[@]@Q}")
}

_sfdk_command_emulator()
{
    local commands=(list start stop status set show exec install remove
        device-model-{list,show})

    if ((cword == _sfdk_command_idx + 1)); then
        _sfdk_complete_with_shortcuts "${commands[@]}"
        return
    fi

    case ${words[_sfdk_command_idx + 1]} in
        list)
            _sfdk_no_defaults
            ;;
        start|stop|status|show|remove)
            ((cword == _sfdk_command_idx + 2)) && __sfdk_complete_emulator_name
            ;;
        set)
            compopt -o nosort
            local properties=() names=()

            __sfdk_complete_emulator_property_assignment
            properties=("${COMPREPLY[@]}")

            if ((cword == _sfdk_command_idx + 2)); then
                __sfdk_complete_emulator_name
                # __sfdk_complete_emulator_property_assignment turns 'nospace' on temporarily, so
                # here we need to maintain spaces manually
                names=("${COMPREPLY[@]/%/ }")
            fi

            COMPREPLY=("${names[@]}" "${properties[@]}")
            ;;
        exec)
            local -A options=(
                [-t]=
            )
            # Skip possible emulator name at start
            local non_option_idx=$(_sfdk_find_non_option --skip-at-start \
                $((_sfdk_command_idx + 2)) "$cword")
            if [[ $non_option_idx ]]; then
                # TODO Complete device command?
                return
            fi

            if [[ $cur == -* ]]; then
                COMPREPLY=($(compgen -W "${!options[*]} --" -- "$cur") "${COMPREPLY[@]}")
            else
                ((cword == _sfdk_command_idx + 2)) && __sfdk_complete_emulator_name
            fi
            ;;
        install)
            ((cword == _sfdk_command_idx + 2)) && __sfdk_complete_available_emulator_name
            ;;
        device-model-list)
            _sfdk_no_defaults
            ;;
        device-model-show)
            ((cword == _sfdk_command_idx + 2)) && __sfdk_complete_device_model
            ;;
    esac
}

_sfdk_config_option_device()
{
    _sfdk_no_defaults
    local devices=$(_sfdk misc inspect devices)
    IFS=$'\n' read -a COMPREPLY -d '' \
        <<<$(IFS=$'\n' compgen -W "$devices" -- "$cur")
    COMPREPLY=("${COMPREPLY[@]@Q}")
}

# ex: filetype=sh
