#!/bin/bash

## Get data
# STATUS="$(mpc status)"
# COVER="/tmp/.music_cover.jpg"
# MUSIC_DIR="$HOME/Music"

# DATA = `dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata`

# Playing, Paused or Stopped
# album = `dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata | sed -n '/album/{n;p}' | cut -d '"' -f 2 | head -n 1`

## Get status
get_status() {
	status=`dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'PlaybackStatus'|egrep -A 1 "string"|cut -b 26-|cut -d '"' -f 1|egrep -v ^$`
	if [[ "$(pidof spotify || echo 1)" != "1" ]]; then
		if [[ "$status" == "Playing" ]]; then
			echo ""
		else
			echo "喇"
		fi
	else
		echo ""
	fi
}

## Get song
get_song() {
	title=`dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata | sed -n '/title/{n;p}' | cut -d '"' -f 2`
	if [[ "$(pidof spotify || echo 1)" != "1" ]]; then	
		echo "$title"
	else
		echo "Offline"
	fi
}

## Get artist
get_artist() {
	artist=`dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata | sed -n '/artist/{n;n;p}' | cut -d '"' -f 2`
	if [[ "$(pidof spotify || echo 1)" != "1" ]]; then
        	echo "$artist"
        else
                echo "Offline"
        fi
}

## Get time
get_time() {
	time=`mpc status | grep "%)" | awk '{print $4}' | tr -d '(%)'`
	if [[ -z "$time" ]]; then
		echo "0"
	else
		echo "$time"
	fi	
}
get_ctime() {
	ctime=`mpc status | grep "#" | awk '{print $3}' | sed 's|/.*||g'`
	if [[ -z "$ctime" ]]; then
		echo "0:00"
	else
		echo "$ctime"
	fi	
}
get_ttime() {
	ttime=`mpc -f %time% current`
	if [[ -z "$ttime" ]]; then
		echo "0:00"
	else
		echo "$ttime"
	fi	
}

## Get cover
get_cover() {
	art=`dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata | sed -n '/artUrl/{n;p}' | cut -d '"' -f 2`
	if [[ "$(pidof spotify || echo 1)" != "1" ]]; then
		echo "$art"
	else
		echo "images/spotify.jpg"
	fi
}

# Toggle
toggle() {
	if [[ "$(pidof spotify || echo 1)" != "1" ]]; then	
		dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
	fi
}

## Execute accordingly
if [[ "$1" == "--song" ]]; then
	get_song
elif [[ "$1" == "--artist" ]]; then
	get_artist
elif [[ "$1" == "--status" ]]; then
	get_status
elif [[ "$1" == "--time" ]]; then
	get_time
elif [[ "$1" == "--ctime" ]]; then
	get_ctime
elif [[ "$1" == "--ttime" ]]; then
	get_ttime
elif [[ "$1" == "--cover" ]]; then
	get_cover
elif [[ "$1" == "--toggle" ]]; then
	toggle
elif [[ "$1" == "--next" ]]; then
	{ dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next; get_cover; }
elif [[ "$1" == "--prev" ]]; then
	{ dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous; get_cover; }
fi
