#!/bin/bash

runtime="${XDG_RUNTIME_DIR}/i3blocks"
[[ ! -d "$runtime" ]] && mkdir -p "$runtime"

if ! systemctl --user is-active mpd &>/dev/null
then
	origstatus='off'
else
	origstatus="$(mpc status | sed -n '2p' | grep -Eo '^\[.*\]' | tr -d '[]')"
	[[ -z "$origstatus" ]] && origstatus='stop'
fi

LONG=40
SHORT=20

case $BLOCK_BUTTON in
	1) mpc -q prev ;;
	2) mpc -q toggle ;;
	3) mpc -q next ;;
	#4) mpc -q volume +2 ;;
	#5) mpc -q volume -2 ;;
	#?*) notify-send "Test" "$BLOCK_BUTTON" ;;
esac

human_time() {
	local seconds="$1"
	if [[ "$seconds" -lt 0 ]]
	then
		echo "--:--"
		return
	fi
	local minutes=$(( $seconds / 60 ))
	seconds=$(( $seconds % 60 ))
	if [[ "$minutes" -ge 60 ]]
	then
		local hours=$(( $minutes / 60 ))
		minutes=$(( $minutes % 60 ))
		printf "%02dh%02d\n" "$hours" "$minutes"
	else
		printf "%02d:%02d\n" "$minutes" "$seconds"
	fi
	return
}

	
status="<span foreground=\"#6699CC\">"
case $origstatus in
	stop ) status="<span foreground=\"#F2777A\">" ;;
	paused ) status="<span foreground=\"#FFCC66\">" ;;
	playing ) status="<span foreground=\"#99CC99\">" ;;
	off ) status="<span foreground=\"#515151\">" ;;
esac

if [[ "$origstatus" != "stop" ]] && [[ "$origstatus" != "off" ]]
then
	if [[ ! -f $runtime/mpd ]]
	then
		mpc -f '[[%artist%§|%name%§]%title%|[%file%]' current > ${runtime}/mpd
	fi

	song=$(<${runtime}/mpd)
	
	if [[ "${#song}" -gt $LONG ]]
	then
		scrolling=long
	elif [[ "${#song}" -gt $SHORT ]]
	then
		scrolling=short
	else
		scrolling=none
	fi

	if [[ "$scrolling" != "none" ]]
	then
		scroll="$(<${runtime}/mpd-scroll)"
	
		if [[ -z "${scroll// /}" ]]
		then
			printf "%-${LONG}s" "$(<${runtime}/mpd)" > ${runtime}/mpd-scroll
			song="$(<${runtime}/mpd-scroll)"
		else
			song="$(<${runtime}/mpd-scroll)"
			printf "%-${LONG}s" "${song:1}" > ${runtime}/mpd-scroll
		fi
	fi

	time="$(echo -e 'status\nclose' | nc -U $MPD_HOST | grep time \
		| cut -d' ' -f2)"
	
	time_elapsed=$(human_time "${time%:*}")
 	time_total=$(human_time "${time#*:}")
	time_left=$(human_time "$(( ${time#*:} - ${time%:*} ))")

	longtime="[$time_elapsed $time_left ${time_total/00:00/--:--}]"
	shorttime="$time_elapsed $time_left"

	case $scrolling in
		long)
			longsong="${song}"
			shortsong="${song}"
		;;
		short)
			longsong="$(<${runtime}/mpd)"
			shortsong="${song}"
		;;
		none)
			longsong="$(<${runtime}/mpd)"
			shortsong="$longsong"
		;;
	esac

	longsong="${longsong:0:$LONG}"
	shortsong="${shortsong:0:$SHORT}"
	longsong="${longsong//&/&amp;}"
	shortsong="${shortsong//&/&amp;}"

	if [[ "$longsong" =~ § ]]
	then
		longsong="<b>${longsong/§/</b> }"
	elif [[ "$song" =~ § ]]
	then
		longsong="<b>${longsong}</b>"
	fi
	if [[ "$shortsong" =~ § ]]
	then
		shortsong="<b>${shortsong/§/</b> }"
	elif [[ "$song" =~ § ]]
	then
		shortsong="<b>${shortsong}</b>"
	fi

	echo "${longsong} $status$longtime</span>"
	echo "${shortsong} $status$shorttime</span>"
else
	[[ -f ${runtime}/mpd ]] && rm ${runtime}/mpd
	[[ -f ${runtime}/mpd-scroll ]] && rm ${runtime}/mpd-scroll
	printf "$status[--:-- --:-- --:--]</span>\n" " "
	printf "$status--:-- --:--</span>\n" " "
fi