#!/bin/bash

_voxl_logger(){

	local OPTS=('--armed --benchtest_vio --cam --directory --pose_6dof --help --imu --skip --mavlink --note --preset_odometry --tof --qvio --snapshot_rate_hz --samples --time --vio --raw --debug')

	COMPREPLY=()

	if [ "$COMP_CWORD" -eq 1 ]; then
		COMPREPLY=( $(compgen -W '${OPTS[@]}' -- ${COMP_WORDS[COMP_CWORD]}) )
		return 0
	fi

	case ${COMP_WORDS[COMP_CWORD-1]} in
		"-c" | "--cam" )
			#safety check in case the user has not installed voxl-utils >= 0.6.2, which
			# contains the voxl tab complete function
			if type -t _voxl_tab_complete | grep -q "function" ; then
				_voxl_tab_complete "camera_image_metadata_t"
			fi
			return 0
			;;

		"-d" | "--directory" )
			COMPREPLY=( $(compgen -d -- ${COMP_WORDS[COMP_CWORD]}) )
			return 0
			;;

		"-i" | "--imu" )
			#safety check in case the user has not installed voxl-utils >= 0.6.2, which
			# contains the voxl tab complete function
			if type -t _voxl_tab_complete | grep -q "function" ; then
				_voxl_tab_complete "imu_data_t"
			fi
			return 0
			;;

		"-f" | "--pose_6dof" )
			#safety check in case the user has not installed voxl-utils >= 0.6.2, which
			# contains the voxl tab complete function
			if type -t _voxl_tab_complete | grep -q "function" ; then
				_voxl_tab_complete "pose_vel_6dof_t"
			fi
			return 0
			;;

		"-k" | "--skip" )
			echo ""
			echo "Enter the number of samples to skip between logged samples"
			echo -ne "${ECHO_PS1}${SET_BOLD}${CLR_BLU}$(dirs)${RESET_ALL}${CLR_LIT_YLW}$(__git_ps1 '(%s)')${RESET_ALL}\$"

			for i in $(seq 0 $COMP_CWORD); do
				echo -n " ${COMP_WORDS[i]}"
			done
			;;
		
		"-n" | "--note" )
			echo ""
			echo "Enter a very short note to the end of the log, i.e. \"flight_with_new_motors\""
			echo -ne "${ECHO_PS1}${SET_BOLD}${CLR_BLU}$(dirs)${RESET_ALL}${CLR_LIT_YLW}$(__git_ps1 '(%s)')${RESET_ALL}\$"

			for i in $(seq 0 $COMP_CWORD); do
				echo -n " ${COMP_WORDS[i]}"
			done
			;;

		"-p" | "--tof" )
			#safety check in case the user has not installed voxl-utils >= 0.6.2, which
			# contains the voxl tab complete function
			if type -t _voxl_tab_complete | grep -q "function" ; then
				_voxl_tab_complete "tof"
			fi
			return 0
			;;

		"-s" | "--samples" )
			echo ""
			echo "Enter the maximum samples to save for this specific channel"
			echo -ne "${ECHO_PS1}${SET_BOLD}${CLR_BLU}$(dirs)${RESET_ALL}${CLR_LIT_YLW}$(__git_ps1 '(%s)')${RESET_ALL}\$"

			for i in $(seq 0 $COMP_CWORD); do
				echo -n " ${COMP_WORDS[i]}"
			done
			;;

		"-t" | "--time" )
			echo ""
			echo "Enter the time to log for in seconds"
			echo -ne "${ECHO_PS1}${SET_BOLD}${CLR_BLU}$(dirs)${RESET_ALL}${CLR_LIT_YLW}$(__git_ps1 '(%s)')${RESET_ALL}\$"

			for i in $(seq 0 $COMP_CWORD); do
				echo -n " ${COMP_WORDS[i]}"
			done
			;;

		"-v" | "--vio" )
			#safety check in case the user has not installed voxl-utils >= 0.6.2, which
			# contains the voxl tab complete function
			if type -t _voxl_tab_complete | grep -q "function" ; then
				_voxl_tab_complete "vio_data_t"
			fi
			return 0
			;;
		
		*)
			local availableOpts=("${OPTS[@]}")

			for i in $(seq 1 $COMP_CWORD);
			do	
				case "${COMP_WORDS[i]}" in 
					"-o"|"--preset_odometry") # preset_odometry
						availableOpts=("${availableOpts[@]/--preset_odometry}")
						;;
					"-p"|"--qvio") # qvio
						availableOpts=("${availableOpts[@]/--qvio}")
						;;
					"-z"|"--debug") # debug
						availableOpts=("${availableOpts[@]/--debug}")
						;;
					"-h"|"--help") # help
						COMPREPLY=()
						return 0
						;;

			   	esac
			done

			COMPREPLY=( $(compgen -W '${availableOpts}' -- ${COMP_WORDS[COMP_CWORD]}) )
			return 0
	esac
	return 0

}

complete -o filenames -F _voxl_logger voxl-logger
