#!/usr/bin/env bash


DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source "$DIR/common"
export DISPLAY=:0.0

usage(){
	echo "checkCall [options]"
	echo "Options:"
	echo "  -h|--help       Display this help text"
	echo "  -m|--modem      Specify a modem"
	echo "  --dry-run       Don't actually send the message"
}


# Stolen from sxmo
lookupnumberfromcallid() {
	local id=$1
	mmcli -m "$modem" --voice-list-calls -o "$id" -K |
		grep call.properties.number |
		cut -d ':' -f 2 |
		tr -d ' '
}

# Stolen from sxmo
toggleflag() {
	TOGGLEFLAG=$1
	shift
	FLAGS="$*"

	echo -- "$FLAGS" | grep -- "$TOGGLEFLAG" >&2 &&
		NEWFLAGS="$(echo -- "$FLAGS" | sed "s/$TOGGLEFLAG//g")" ||
		NEWFLAGS="$(echo -- "$FLAGS $TOGGLEFLAG")"

	NEWFLAGS="$(echo -- "$NEWFLAGS" | sed "s/--//g; s/  / /g")"

	# shellcheck disable=SC2086
	megiaudioroute $NEWFLAGS
	echo -- "$NEWFLAGS"
}

# Stolen from sxmo
toggleflagset() {
	FLAGS="$(toggleflag "$1" "$FLAGS")"
}


lookupcontact(){
	local contact="$(contact-numbers "$1" | cut -d '	' -f 1)"
	if [ -n "$contact" ]; then
		echo "$contact"
	else
		echo "$1" |
			# Remove the +44 and replace with 0
			sed 's/^\+44/0/'
	fi
}

dtmf(){
	local id="$1"
	svkbd-numbers &
	local choice="$( (
		echo "Exit"
		) | rofi -dmenu -i -me-accept-entry '!MousePrimary' -p "Tones" -normal-window -font 'Iosevka 20' )"
	if [ "$choice" = "Exit" ]; then
		return
	fi
	echo "$choice" | grep -o '.' | while read -r tone; do
	sleep 0.5
		mmcli -m "$modem" -o "$id" --send-dtmf="$tone"
	done
	pkill svkbd-numbers

}

ongoingCall(){
	local id="$1"

	# Stolen from sxmo
	FLAGS=" "
	toggleflagset "-e"
	toggleflagset "-m"
	toggleflagset "-2"
	toggleflagset "-2"
	toggleflagset "-2"

	notify-send "I get here"

	while : ; do
		choice="$( (
			echo "Hang Up"
			echo "DTMF Tones"
			echo "Volume Up"
			echo "Volume Down"
			) | trofi)"

		case "$choice" in
			"Hang Up") rejectCall "$1"; break ;;
			"DTMF Tones") dtmf "$1" ;;
			"Volume"*) notify-send "Still need to implement" ;;
		esac

	done
}

answerCall(){

	pkill mpv
	sleep 0.2

	echo "answer call $1" > /dev/tty
	mmcli -m "$modem" -o "$1" --accept

	ongoingCall "$1"
}

rejectCall(){
	pkill mpv
	echo "reject call $1" > /dev/tty
	mmcli -m "$modem" -o "$1" --hangup
	mmcli -m "$modem" --voice-delete-call="$1"

	mmcli -m "$modem" --voice-hangup-all
	for CALLID in $( mmcli -m "$modem" --voice-list-calls | grep -oE "Call\/[0-9]+" | cut -d'/' -f2); do
		echo mmcli -m "$modem" --voice-delete-call "$CALLID" > ~/.hangup.log
	done

	# Not sure why but sometimes the modem changes
	# To be sure, re-check the modem and delete all calls
	local tmpmodem="$(mmcli -L | grep -oE 'Modem\/[0-9]+' | head -n 1 | cut -d'/' -f2)"

	mmcli -m "$tmpmodem" --voice-hangup-all
	for CALLID in $( mmcli -m "$tmpmodem" --voice-list-calls | grep -oE "Call\/[0-9]+" | cut -d'/' -f2); do
		echo mmcli -m "$tmpmodem" --voice-delete-call "$CALLID" > ~/.hangup.log
	done

	alsactl --file "$ALSA_CONF_DIR/default_alsa_sound.conf" restore
}

promptIncoming(){
	export DISPLAY=:0.0

	local answer=""
	local reject=""

	local choice=''
	local contact="$1"

	local prompt="$(echo -en "Incoming Call\n$contact")"

	choice="$(echo -e "$answer\n$reject" | rofi -dmenu -i \
		-theme themes/incoming-call.rasi -a 0 -u 1 \
		-p "$prompt" \
		-window-title "call-from-$contact" \
		-me-select-entry '' -me-accept-entry MousePrimary)"

	case "$choice" in
		"$answer") echo "accept" ;;
		"$reject") echo "reject" ;;
	esac
}

checkOutgoing(){
	export DISPLAY=:0.0
	if [ "$dryrun" = "true" ]; then
		return
	fi
	notify-send "outgoing call" "$1"
	mmcli -m "$modem" -o "$1" --start

	ongoingCall "$1"
}

checkIncoming(){
	export DISPLAY=:0.0
	if [ "$dryrun" = "true" ]; then
		promptIncoming "+441234567890"
		return
	fi
	local id="$( mmcli -m "$modem" --voice-list-calls |
		grep -Eo '[0-9]+ incoming \(ringing-in\)' | grep -Eo '[0-9]+' )"

	local count="$(echo "$id" | deleteEmptyLines | wc -l)"

	[ "$count" -eq 0 ] && return

	local number="$(lookupnumberfromcallid "$id")"
	local contact="$(lookupcontact "$number")"

	local action=""
	while mmcli -m "$modem" --voice-list-calls | grep -Eoq "$id"' incoming \(ringing-in\)' && [ -z "$action" ]; do
		mpv "$HOME/.local/share/soundeffects/ringtone" --loop &
		action="$(promptIncoming "$contact")"
	done
	case "$action" in
		"accept") answerCall "$id"; ;;
		"reject") rejectCall "$id"; ;;
	esac

}

checkFinished(){
	local ids="$( mmcli -m "$modem" --voice-list-calls |
		grep -Eo '[0-9]+ incoming \(terminated\)' | grep -Eo '[0-9]+' )"

	local count="$(echo "$ids" | deleteEmptyLines | wc -l)"
	echo "Finished Count: $count"

	[ "$count" -eq 0 ] && return

	echo "$ids" | while read -r id; do
		# Delete from the modem
		local number="$(lookupnumberfromcallid "$id")"
		local contact="$(lookupcontact "$number")"

		echo "I get here"

		# If there is a rofi process with the title of "call-from-number", then
		# it hasn't been answerd yet.
		# Treat as a missed call
		if ps aux | grep -E '\Wrofi' | grep -q "call-from-$contact"; then
			echo -e "$(date '+%s')\t$contact\t$number" >> "$CALL_DIR/missed-calls"
			pkill mpv
			mmcli -m "$modem" --voice-delete-call "$id"
			ps aux | grep -E '\Wrofi' | grep "call-from-$contact" |
				awk '{print $2}' | xargs kill
		fi


			
	done
}

mkdir -p "$CALL_DIR"

dryrun="false"

# Assume we want the first modem
# can be overwritten by the -m option
modem="$(mmcli -L | grep -oE 'Modem\/[0-9]+' | head -n 1 | cut -d'/' -f2)"



# Read the options and set stuff
while [[ $1 = -?* ]]; do
	case $1 in
		-h|--help) usage; exit;;
		-m|--modem) modem="$2"; shift ;;
		--dry-run) dryrun="true" ;;
		--) shift; break ;;
		*) die "invalid option: '$1'." ;;
	esac
	shift
done

if [ -n "$1" ]; then
	checkOutgoing "$1" &
else
	checkIncoming &
	checkFinished &
fi