#!/bin/bash

is_running() {
    pgrep -x "hyprsunset" >/dev/null 2>&1 || systemctl --user is-active --quiet hyprsunset 2>/dev/null
}

if [ "$1" = "toggle" ]; then
    if is_running; then
        systemctl --user stop hyprsunset 2>/dev/null
        killall -9 hyprsunset 2>/dev/null
        sleep 0.1
    else
        setsid -f hyprsunset -t 4500 >/dev/null 2>&1
        sleep 0.2
    fi
    pkill -RTMIN+8 waybar
    exit 0
fi

if is_running; then
    echo '{"text": "ON 🌙", "class": "on", "tooltip": "Night Light: ON "}'
else
    echo '{"text": "OFF ☀️", "class": "off", "tooltip": "Night Light: OFF "}'
fi
