#!/bin/bash

IP="192.168.1.140"
ENDPOINT="http://$IP:9529/ZidooMusicControl/v2/getState"

data=$(curl -s --max-time 2 "$ENDPOINT")

if [[ -n "$data" ]]; then
    artist=$(echo "$data" | jq -r '.playingMusic.artist')
    title=$(echo "$data" | jq -r '.playingMusic.title')
    status=$(echo "$data" | jq -r '.everSoloPlayInfo.playStatus')

    # Detalles extra para el tooltip
    bitrate=$(echo "$data" | jq -r '.playingMusic.bitrate')
    samplerate=$(echo "$data" | jq -r '.playingMusic.sampleRate')
    quality=$(echo "$data" | jq -r '.playingMusic.audioQuality')

    # Escapar caracteres especiales
    artist="${artist//&/&amp;}"
    title="${title//&/&amp;}"
    bitrate="${bitrate//&/&amp;}"
    samplerate="${samplerate//&/&amp;}"
    quality="${quality//&/&amp;}"

    tooltip="<b>${artist} - ${title}</b>\n${quality} | ${samplerate} | ${bitrate}"

    if [[ "$status" == "1" && "$artist" != "null" && "$title" != "null" ]]; then
        echo "{\"text\": \"🎵 $artist - $title\", \"tooltip\": \"$tooltip\"}"
    else
        echo "{\"text\": \"⏸️ Pausado o sin música\", \"tooltip\": \"No se está reproduciendo música.\"}"
    fi
else
    echo "{\"text\": \"⚠️ DMP-A6 desconectado\", \"tooltip\": \"No se pudo conectar al Eversolo.\"}"
fi

