#!/bin/bash

LAST_TRACK=""

playerctl --player=mopidy metadata --follow | \
while read -r line; do
    if [[ "$line" == *"xesam:url"* ]]; then
        uri=$(awk '{print $NF}' <<< "$line")

        # Solo si cambia la pista
        if [[ "$uri" != "$LAST_TRACK" ]]; then
            LAST_TRACK="$uri"

            if [[ "$uri" == tidal:* ]]; then
                # Extraer ID de la pista
                track_id=$(echo "$uri" | awk -F: '{print $4}')
                image_url=$(python3 /home/teraflops/.local/bin/get_tidal_image.py "$track_id")

                # Descargar imagen temporalmente
                tmpimg="/tmp/tidal_art.jpg"
                curl -s -L "$image_url" -o "$tmpimg"

                # Mostrar notificación
                title=$(playerctl --player=mopidy metadata xesam:title)
                artist=$(playerctl --player=mopidy metadata xesam:artist)

                notify-send -i "$tmpimg" "$title" "$artist"
            fi
        fi
    fi
done

