#!/bin/sh

set -ex

tmpf=$(mktemp)

choose(){
    choice=$(grep "$(cat $tmpf | cut -d\| -f1| wdmenu)" $tmpf)
    choiceId="$(echo $choice| cut -d\| -f2)"
    choiceUrl=http://deezer.com/$sType/$choiceId
    choicePreview=$( curl -s "api.deezer.com/$sType/$choiceId/$preview_suffix" |
        jq -r '.preview, .data[0].preview | select(. != null)' )

    choice=$( printf "Preview\nDownload\nCopy URL" | wdmenu )
    if [ "$choice" = Preview ]
    then
        mpv --quiet "$choicePreview" &
        mpvPid=$!
        choice=$(printf "Download\nAnother" | wdmenu -p 'Download?')
        kill $mpvPid
        if test "$choice" = "Another"
        then
            choose
            return
        fi
    fi
    if [ "$choice" = Download ]
    then
        notify-send "Starting Download"
        deemix "$choiceUrl" &&
            notify-send "Download Successful" ||
            notify-send "Download Failed"
        mpc add /
        mpdDup
    fi
    if [ "$choice" = "Copy URL" ]
    then
        wl-copy "$choiceUrl"
    fi

}

clean(){
    set +e
    kill "$mpvPid"

    rm -f $tmpf
}
trap clean EXIT

sType=$(printf "Track\nAlbum\nArtist" | wdmenu | tr [:upper:] [:lower:] )
query=$(echo -n | wdmenu | sed 's/[^ a-z]//g;s/ /+/g')
case "$sType" in
    track)
        jqFilter='.data[]|.title+" - "+.album.title+" - "+.artist.name+"|"+( .id | tostring ) '
        ;;
    album)
        jqFilter='.data[]| ( .nb_tracks | tostring ) +" - "+.title+" - "+.artist.name+"|"+ ( .id | tostring ) '
        preview_suffix=tracks
        ;;
    artist)
        jqFilter='.data[]| ( .nb_fan | tostring ) +" - "+.name+"|"+ ( .id | tostring ) '
        preview_suffix=top
        ;;
esac

curl -s "api.deezer.com/search/$sType?q=${query}" |
    jq -r "$jqFilter" > $tmpf

choose

