#!/bin/sh

# Description:
#   Toggle the visibility of lyrics widgets in eww.
#   - Ensure only one of the two widgets (lyrics or lyrics-single) is open at a time.
#   - Cycle through the following states:
#     1. Both widgets closed -> Open 'lyrics'
#     2. 'lyrics' open -> Close 'lyrics' and open 'lyrics-single' (if waybar is running)
#     3. 'lyrics-single' open -> Close 'lyrics-single'
#     4. Both widgets open -> Close both

LYRICS=$(eww active-windows | grep "lyrics:")
LYRICS_SINGLE=$(eww active-windows | grep "lyrics-single:")

# both are closed
if [ -z "$LYRICS" ] && [ -z "$LYRICS_SINGLE" ]; then
    eww open lyrics

# only lyrics is open
elif [ -n "$LYRICS" ] && [ -z "$LYRICS_SINGLE" ]; then
    eww close lyrics
    # if waybar is running, open lyrics-single
    if pgrep -x "waybar" -u "$USER" > /dev/null; then
        sleep 0.5
        eww open lyrics-single
    fi

# only lyrics-single is open
elif [ -z "$LYRICS" ] && [ -n "$LYRICS_SINGLE" ]; then
    eww close lyrics-single

# both are open
elif [ -n "$LYRICS" ] && [ -n "$LYRICS_SINGLE" ]; then
    eww close lyrics
    eww close lyrics-single
fi