#!/usr/bin/env bash

# Script initialization
# Check if animated wallpapers exist, set env var if so
# Sleep for swww init to finish
sleep 3
script_init() {
    if [ `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | wc -l` -gt 0 ]; then
        echo "true" > /tmp/$USER-desktop-anim
        ANIM_READY=true
    else
        # Download or bail here?
        echo "no animations found, static only!"
    fi
}
# check if on ac or battery
check_if_ac() {
    ON_AC=`cat /sys/class/power_supply/AC/online`
    if [ $ON_AC -eq 1 ] && [ "$ANIM_READY" = true ]; then
        echo "Anim"
        init_anim
    else
        echo "Static"
        init_static
    fi
}

init_static() {
    # On battery or missing wallpapers, just load a random image
    IMG=`find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
    echo $IMG >> /tmp/$USER-desktop-anim
    swww img $IMG
    # This is hacky but sometimes we get into a race condition where the
    # animated background will load anyways, so this will ensure we stay static
    # until we definitely control swww and its state is known
    sleep 60
    swww img $IMG
}

init_anim() {
    # Preload a static image until the animated one is ready
    swww img --transition-step 255 `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
    sleep 2
    IMG=`find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1`
    echo $IMG >> /tmp/$USER-desktop-anim
    swww img $IMG
}

begin_randomizer() {
    while :; do
        echo "Invoke selector"
        sleep 3600
        if [ `cat /sys/class/power_supply/AC/online` -eq 1 ] && [ `cat /tmp/$USER-desktop-anim | head -n1` == "true" ]; then
            switch_check `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1`
        else
            switch_check `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
        fi
    done
}

switch_check() {
    # Simply query what image is displayed and make sure we do not overwrite
    # it. This is more for animated wallpapers since it causes corruption
    CURRENT_FILE=`swww query | awk -F'/' '{print $NF}'`
    SELECTION=`echo $1 | awk -F'/' '{print $NF}'`

    if ! [ "$CURRENT_FILE" == "$SELECTION" ]; then
        # Update the file
        sed -i '2c'"$1" /tmp/$USER-desktop-anim
        # Set the background!
        swww img $1
    fi
}

check_load() {
    # If system is under stress, disable animated wallpaper. If not, allow them
    while :; do 
        sleep 60
        ANIM_ENABLE=`cat /tmp/$USER-desktop-anim`
        LOAD=`cat /proc/loadavg | cut -d' ' -f1`

        if [ "$ANIM_ENABLE" == "true" ] && [ $LOAD -gt 8.00 ] && [ $ANIM_READY = true ]; then
            sed -i '1c\false' /tmp/$USER-desktop-anim
            switch_check `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
        elif [ "$ANIM_ENABLE" == "false" ] && [ $LOAD -lt 8.00 ] && [ $ANIM_READY = true ]; then
            sed -i '1c\true' /tmp/$USER-desktop-anim
            switch_check `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1`
        fi
    done
}

script_init
check_if_ac
begin_randomizer &
check_load &

dbus-monitor --system "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',sender=':1.2',path='/org/freedesktop/UPower/devices/line_power_AC'" 2>/dev/null | stdbuf -o0 awk -F' ' '/variant             boolean/ {print $(NF)}' |
while read -r line; do
    if [ "$line" == "true" ]; then
        echo "switching to ac"
        switch_check `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1`
    elif [ "$line" == "false" ]; then
        echo "switching to bat"
        switch_check `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
    fi
done