#/usr/bin/env bash

VMT_ARGS="--account= --config= --help --version"
VMT_CMDS="att cat help ls mime pick show sync"

VMT_PICK_ARGS="--attachments --attachments-only --date --empty --help --multiple"

VMT_LS_ARGS="--all --help --long --real-name"

VMT_ATT_ARGS=""
VMT_ATT_CMDS="cat help ls save"

VMT_ATT_LS_ARGS="--color --help --long"
VMT_ATT_CAT_ARGS="--help"
VMT_ATT_SAVE_ARGS="--all --help"

_contains()
{
	[[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]] && true || false
}

_prefix()
{
	local WORD=$1; shift
	local PREFIXES=("$@")

	for PREFIX in "${PREFIXES[@]}"; do
		if [[ "$WORD" == "$PREFIX"* ]]; then
			echo "$PREFIX"
			return
		fi
	done
}

_vmt_completions_pick()
{
	if [ "${#COMP_WORDS[@]}" == "3" ] || [[ "${COMP_WORDS[-2]}" = -* ]]; then
		if [[ "${COMP_WORDS[-1]}" = -* ]]; then
			COMPREPLY=($(compgen -W "${VMT_PICK_ARGS}" -- "${COMP_WORDS[-1]}"))
		else
			COMPREPLY=($(compgen -W "$(vmt ls)" -- "${COMP_WORDS[-1]}"))
		fi
	fi
}

_vmt_completions_cat()
{
	# TODO this one triggers `vmt pick` itself, kind of interesting, but might be annoing?
	if [ "${#COMP_WORDS[@]}" == "3" ] || [[ "${COMP_WORDS[-2]}" = -* ]]; then
		if [[ "${COMP_WORDS[-1]}" = -* ]]; then
			COMPREPLY=($(compgen -W "${VMT_LS_ARGS}" -- "${COMP_WORDS[-1]}"))
		elif [[ "${COMP_WORDS[-1]}" = "" ]]; then
			COMPREPLY=($(vmt pick))
		fi
	fi
}

_vmt_completions_ls()
{
	if [ "${#COMP_WORDS[@]}" == "3" ] || [[ "${COMP_WORDS[-2]}" = -* ]]; then
		if [[ "${COMP_WORDS[-1]}" = -* ]]; then
			COMPREPLY=($(compgen -W "${VMT_LS_ARGS}" -- "${COMP_WORDS[-1]}"))
		else
			local IFS=$'\n'
			COMPREPLY=($(IFS=$'\n' compgen -W "$(vmt ls | sed 's/ /\\\\ /g')" -- "${COMP_WORDS[-1]}"))
		fi
	fi
}

_vmt_completions_att()
{
	if [ "${#COMP_WORDS[@]}" == "3" ] || [[ "${COMP_WORDS[-2]}" = -* ]]; then
		if [[ "${COMP_WORDS[-1]}" = -* ]]; then
			COMPREPLY=($(compgen -W "${VMT_ATT_ARGS}" -- "${COMP_WORDS[-1]}"))
		else
			COMPREPLY=($(compgen -W "${VMT_ATT_CMDS}" -- "${COMP_WORDS[-1]}"))
		fi
	else
		case "${COMP_WORDS[-2]}" in
			cat)
				_vmt_completions_att_cat
				;;
			ls)
				_vmt_completions_att_ls
				;;
			save)
				_vmt_completions_att_save
				;;
			*)
				:
				;;
		esac
	fi
}

_vmt_completions_att_cat()
{
	if [ "${#COMP_WORDS[@]}" == "4" ] || [[ "${COMP_WORDS[-2]}" = -* ]]; then
		if [[ "${COMP_WORDS[-1]}" = -* ]]; then
			COMPREPLY=($(compgen -W "${VMT_ATT_LS_ARGS}" -- "${COMP_WORDS[-1]}"))
		else
			# For now, only complete up to mailbox name. Completing
			# to emails or even attachments will be quite tricky to
			# get right...
			local IFS=$'\n'
			COMPREPLY=($(IFS=$'\n' compgen -W "$(vmt ls | sed 's/ /\\\\ /g')" -- "${COMP_WORDS[-1]}"))

			# TODO mailbox name can have spaces
			#MBS=$(vmt ls | sed 's|.*|&/|')
			#MBA=($MBS)
			#CURMB=$(_prefix "${COMP_WORDS[-1]}" "${MBA[@]}")
			##echo found prefix $CURMB >&2
			#if [ -n "$CURMB" ]; then
			#	#local COMPS=( $(compgen -W "$(vmt ls -l "${COMP_WORDS[-1]}")" -- "${COMP_WORDS[1]}") )
			#	local IFS=$'\n'
			#	COMPREPLY=($(IFS=$'\n' compgen -W "$(vmt ls -l "${CURMB}" | awk '{print $1}')" -- "${COMP_WORDS[-1]#"$CURMB"}"))
			#else
			#	compopt -o nospace
			#	COMPREPLY=($(compgen -W "${MBS}" -- "${COMP_WORDS[-1]}"))
			#fi
		fi
	fi
}

_vmt_completions_att_ls()
{
	if [ "${#COMP_WORDS[@]}" == "4" ] || [[ "${COMP_WORDS[-2]}" = -* ]]; then
		if [[ "${COMP_WORDS[-1]}" = -* ]]; then
			COMPREPLY=($(compgen -W "${VMT_ATT_LS_ARGS}" -- "${COMP_WORDS[-1]}"))
		else
			# For now, only complete up to mailbox name. Completing
			# to emails or even attachments will be quite tricky to
			# get right...
			local IFS=$'\n'
			COMPREPLY=($(IFS=$'\n' compgen -W "$(vmt ls | sed 's/ /\\\\ /g')" -- "${COMP_WORDS[-1]}"))
		fi
	fi
}

_vmt_completions_att_save()
{
	if [ "${#COMP_WORDS[@]}" == "4" ] || [[ "${COMP_WORDS[-2]}" = -* ]]; then
		if [[ "${COMP_WORDS[-1]}" = -* ]]; then
			COMPREPLY=($(compgen -W "${VMT_ATT_SAVE_ARGS}" -- "${COMP_WORDS[-1]}"))
		else
			# For now, only complete up to mailbox name. Completing
			# to emails or even attachments will be quite tricky to
			# get right...
			local IFS=$'\n'
			COMPREPLY=($(IFS=$'\n' compgen -W "$(vmt ls | sed 's/ /\\\\ /g')" -- "${COMP_WORDS[-1]}"))
		fi
	fi
}

_vmt_completions()
{
	if [ "${#COMP_WORDS[@]}" == "2" ] || [[ "${COMP_WORDS[-2]}" = -* ]]; then
		if [[ "${COMP_WORDS[-1]}" = -* ]]; then
			COMPREPLY=($(compgen -W "${VMT_ARGS}" -- "${COMP_WORDS[-1]}"))
		else
			COMPREPLY=($(compgen -W "${VMT_CMDS}" -- "${COMP_WORDS[-1]}"))
		fi
	else
		for WORD in "${COMP_WORDS[@]}"; do
			if [[ $WORD == -* ]]; then
				continue
			fi
			case "${WORD}" in
				pick)
					_vmt_completions_pick
					break
					;;
				cat)
					_vmt_completions_cat
					break
					;;
				ls)
					_vmt_completions_ls
					break
					;;
				att)
					_vmt_completions_att
					break
					;;
				*)
					:
					;;
			esac
		done
	fi
}

complete -F _vmt_completions vmt
