#!/bin/bash

# Waybar Hyprsunset Module Script
# Place this in ~/.config/waybar/scripts/hyprsunset.sh
# Make executable: chmod +x ~/.config/waybar/scripts/hyprsunset.sh

TEMP=3500  # Default temperature when enabled
STATE_FILE="/tmp/hyprsunset_state"

# Initialize state file if it doesn't exist
if [ ! -f "$STATE_FILE" ]; then
    echo "disabled" > "$STATE_FILE"
fi

get_status() {
    # Check if hyprsunset is currently active
    if [ -f "$STATE_FILE" ]; then
        state=$(cat "$STATE_FILE" 2>/dev/null)
        if [ "$state" = "enabled" ]; then
            echo "enabled"
        else
            echo "disabled"
        fi
    else
        echo "disabled"
    fi
}

toggle_hyprsunset() {
    current_status=$(get_status)
    
    if [ "$current_status" = "enabled" ]; then
        # Disable hyprsunset
        hyprctl hyprsunset identity
        echo "disabled" > "$STATE_FILE"
    else
        # Enable hyprsunset
        hyprctl hyprsunset temperature "$TEMP"
        echo "enabled" > "$STATE_FILE"
    fi
    
    # Signal Waybar to update the module
    pkill -SIGRTMIN+8 waybar
}

output_json() {
    status=$(get_status)
    
    if [ "$status" = "enabled" ]; then
        printf '{"text":"","tooltip":"Sunset: ON (%sK)","class":"enabled"}\n' "$TEMP"
    else
        printf '{"text":"","tooltip":"Sunset: OFF","class":"disabled"}\n'
    fi
}

case "$1" in
    "toggle")
        toggle_hyprsunset
        ;;
    *)
        output_json
        ;;
esac
