#!/bin/bash


# Controls spotify if it's running, (kill spotify process to switch)


# add different players here (also at the end)

if [[ -n $(pgrep spotify) ]]; then
Control="spotify"
else 
Control="MPD"
fi

# Covers will be saved here (for both the players)
Cover=/tmp/cover.png
# mpd music directory
mpddir=~/Music

############# Spotify ##############

spotcover() {

albumart="$(playerctl --player=spotify metadata mpris:artUrl | sed -e 's/open.spotify.com/i.scdn.co/g')"

if [ $(playerctl --player=spotify metadata mpris:artUrl) ]; then
	curl -s "$albumart" --output $Cover;
else cp ~/.config/eww/images/music.png $Cover;
fi
echo "$Cover"
}

spotstatusicon() {
if [[ $(playerctl --player=spotify status 2>&1) == "Playing" ]];then
	echo ""
elif [[ $(playerctl --player=spotify status 2>&1) == "Paused" ]];then
	echo "喇"
else
	echo "ﭥ"
fi
}

############ MPD #################

###

mpctitle() {
song=`mpc -f %title% current`

	if [[ -n "$song" ]]; then
		echo "$song"
	else
		echo "Play Someting"
	fi	
}

mpcartist() {
artist=`mpc -f %artist% current`
	if [[ -n "$artist" ]]; then
		echo "$artist"
	else
		echo "Artist"
	fi	
}

mpcalbum() {
album=`mpc -f %album% current`
	if [[ -n "$album" ]]; then
		echo "$album"
	else
		echo "Album"
	fi	
}

mpccover() {
	ffmpeg -i "$mpddir"/"$(mpc current -f %file%)" "${Cover}" -y &> /dev/null
	STATUS=$?

	if [ "$STATUS" -eq 0 ];then
		echo "$Cover"
	else
	cp ~/.config/eww/images/music.png $Cover;
	echo "$Cover"
	fi
}

mpctime() {
	time=`mpc status %currenttime%`
	if [[ -n "$time" ]]; then
		echo "$time"
	else
		echo "0:00"
	fi	
}

mpclength() {
	length=`mpc status %totaltime%`
	if [[ -n "$length" ]]; then
		echo "$length"
	else
		echo "0:00"
	fi	
}


mpcicon() {
	if [[ $(mpc status) == *"[playing]"* ]]; then
		echo ""
	else
		echo "喇"
	fi
}


mpcstatus() {
	if [[ $(mpc status) == *"[playing]"* ]]; then
		echo "Playing"
	elif [[ $(mpc status) == *"[paused]"* ]]; then
		echo "Paused"
	else
		echo "Stopped"
	fi
}

mpcperc() {
	perc=`mpc status %percenttime%`
	if [[ -n "$perc" ]]; then
		echo "$perc"
	else
		echo "0"
	fi	
}


mpcsongpos() {
	pos=`mpc status %songpos%`
	length=`mpc status %length%`
	if [[ -n "$pos" ]]; then
		echo "$pos/$length"
	else
		echo "0/0"
	fi
}



case $Control in

	spotify)

	if [[ $1 == "next" ]];then
	playerctl --player=spotify next
	elif [[ $1 == "previous" ]];then
	playerctl --player=spotify previous
	elif [[ $1 == "toggle" ]];then
	playerctl --player=spotify play-pause
	elif [[ $1 == "title" ]];then
	playerctl --player=spotify metadata --format {{title}}
	elif [[ $1 == "artist" ]];then
	playerctl --player=spotify metadata --format {{artist}}
	elif [[ $1 == "album" ]];then
	playerctl --player=spotify metadata --format {{album}}
	elif [[ $1 == "status" ]];then
	playerctl --player=spotify status
	elif [[ $1 == "statusicon" ]];then
	spotstatusicon
	elif [[ $1 == "player" ]];then
	echo " Spotify"
	elif [[ $1 == "coverloc" ]];then
	spotcover
	elif [[ $1 == "showcover" ]];then
	spotcover | xargs feh 
	elif [[ $1 == "time" ]];then
	echo "0:00"
	elif [[ $1 == "length" ]];then
	echo "0:00"
	elif [[ $1 == "percent" ]];then
	echo "0"
	elif [[ $1 == "playlist" ]];then
	playerctl metadata xesam:trackNumber
	fi	

	;;

	MPD)
	if [[ $1 == "next" ]];then
	mpc -q next
	elif [[ $1 == "previous" ]];then
	mpc -q prev
	elif [[ $1 == "toggle" ]];then
	mpc -q toggle
	elif [[ $1 == "title" ]];then
	mpctitle
	elif [[ $1 == "artist" ]];then
	mpcartist
	elif [[ $1 == "album" ]];then
	mpcalbum
	elif [[ $1 == "status" ]];then
	mpcstatus
	elif [[ $1 == "statusicon" ]];then
	mpcicon
	elif [[ $1 == "player" ]];then
	echo " MPD"
	elif [[ $1 == "coverloc" ]];then
	mpccover
	elif [[ $1 == "showcover" ]];then
	mpccover | xargs feh
	elif [[ $1 == "time" ]];then
	mpctime
	elif [[ $1 == "length" ]];then
	mpclength
	elif [[ $1 == "percent" ]];then
	mpcperc
	elif [[ $1 == "playlist" ]];then
	mpcsongpos
	fi	

esac 2>/dev/null


