_stne_mprisctl() {
	local i comps cmd
	local cur prev
	_get_comp_words_by_ref cur prev

	local -A flags=(
		[-h]=1
		[--help]=1
		[--version]=1
	)

	local -A options=(
		[--all]=1
		[--ignore]=1
		[--match]=1
	)

	# https://specifications.freedesktop.org/mpris-spec/2.2/Player_Interface.html
	local -A commands=(
		[Next]=1
		[Pause]=1
		[Play]=1
		[PlayPause]=1
		[Previous]=1
		[Stop]=1
	)

	if [[ ${options[$prev]} ]]; then
		case $prev in
			# --ignore)
			# 	comps=( "org.mpris.spotify" "org.mpris.chrome" )
			# 	;;
			# --match)
			# 	comps=( "org.mpris.spotify" "org.mpris.chrome" )
			# 	;;
		esac

		COMPREPLY=( $(compgen -W '${comps[@]}' -- "$cur") )
		return 0
	fi

	if [[ "$cur" = -* ]]; then
		comps=( ${!flags[@]} ${!options[@]} )
		COMPREPLY=( $(compgen -W '${comps[@]}' -- "$cur") )
		return 0
	fi

	for i in "${!COMP_WORDS[@]}"; do
		local word="${COMP_WORDS[i]}"
		if [[ -z $word ]]; then
			continue
		fi

		if [[ ${commands[$word]} && !${options[COMP_WORDS[i-1]]} ]]; then
			cmd=${COMP_WORDS[i]}
			break
		fi
	done

	n=$(($COMP_CWORD - $i))

	if [[ -z $cmd || $n == 0 ]]; then
		comps=( ${!commands[@]} )
	fi

	COMPREPLY=( $(compgen -W '${comps[@]}' -- "$cur") )
	return 0
}

complete -F _stne_mprisctl stne-mprisctl
