import time
from roonapi import RoonApi

appinfo = {
    "extension_id": "python_roon_waybar",
    "display_name": "Waybar Roon Extension",
    "display_version": "1.0.0",
    "publisher": "gregd",
    "email": "mygreat@emailaddress.com",
}

core_id = open("/home/teraflops/apikeys/roon_core_id").read()
token = open("/home/teraflops/apikeys/roon_core_token").read()

roonapi = RoonApi(appinfo, token, "192.168.1.37", "9330", True)


def send_album_cover_notification(zone):
    """Envía una notificación con la carátula del álbum."""
    album_cover_url = zone['now_playing']['image_key']
    response = requests.get(
        f"http://{roonapi.host}:{roonapi.port}/image/{album_cover_url}")

    if response.status_code == 200:
        cover_path = "/tmp/roon_album_cover.jpg"
        with open(cover_path, "wb") as f:
            f.write(response.content)

        # Envía la notificación usando notify-send
        subprocess.run([
            "notify-send", "-i", cover_path, "Now Playing",
            f"{zone['now_playing']['three_line']['line1']
               } - {zone['now_playing']['three_line']['line2']}"
        ])


def get_current_track():
    """Obtiene la canción actual que está sonando en Roon."""
    zones = roonapi.zones
    for zone in zones.values():
        if zone['state'] == 'playing':
            track_name = zone['now_playing']['three_line']['line1']
            artist_name = zone['now_playing']['three_line']['line2']
            return f"{track_name} - {artist_name}"
    return "No track playing"


if __name__ == "__main__":
    print(get_current_track())
