#!/usr/bin/env bash
set -euo pipefail

CONF="${XDG_CONFIG_HOME:-$HOME/.config}/hyprsunset/config"
STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/hyprsunset"
AUTO_FILE="$STATE_DIR/auto"     # "1"=auto on, "0"=auto off
TEMP_FILE="$STATE_DIR/temp"     # last set temp (manual/night)
MODE_FILE="$STATE_DIR/mode"     # "day"/"night" (best effort)
mkdir -p "$STATE_DIR"

# Defaults (overridden by CONF)
DEFAULT_TEMP=5500
TEMP_DAY=6500
TEMP_NIGHT=2500
[ -r "$CONF" ] && source "$CONF"
[ -r "$TEMP_FILE" ] || echo -n "${TEMP_NIGHT:-$DEFAULT_TEMP}" > "$TEMP_FILE"
[ -r "$AUTO_FILE" ] || echo -n "1" > "$AUTO_FILE"
[ -r "$MODE_FILE" ] || echo -n "day" > "$MODE_FILE"

is_running() { pgrep -x hyprsunset >/dev/null 2>&1; }
cur_temp() {
  if is_running; then
    pid="$(pgrep -x hyprsunset | head -n1 || true)"
    if [ -n "${pid:-}" ] && [ -r "/proc/$pid/cmdline" ]; then
      t="$(tr '\0' ' ' < "/proc/$pid/cmdline" | sed -n 's/.*-t \([0-9]\+\).*/\1/p')"
      [ -n "$t" ] && { echo "$t"; return; }
    fi
  fi
  cat "$TEMP_FILE"
}
set_temp() {
  local t="$1"
  echo -n "$t" > "$TEMP_FILE"
  hyprctl dispatch exec "hyprsunset -t $t" >/dev/null 2>&1 || true
}
disable() {
  hyprctl dispatch exec "hyprsunset -x" >/dev/null 2>&1 || true
  pkill -x hyprsunset >/dev/null 2>&1 || true
}
print_json() {
  local auto="$(cat "$AUTO_FILE")"
  local t="$(cur_temp)"
  local mode="$(cat "$MODE_FILE" 2>/dev/null || echo "day")"
  if [ "$auto" = "1" ]; then
    printf '{"text":" %sK","class":"auto %s","tooltip":"Hyprsunset Auto (%s) — %sK\\nLeft-click: Toggle auto OFF\\nRight-click: Toggle manual on/off\\nScroll: +/- 200K (manual)"}\n' "$t" "$mode" "$mode" "$t"
  else
    if is_running; then
      printf '{"text":" %sK","class":"manual on","tooltip":"Hyprsunset Manual ON — %sK\\nLeft-click: Toggle auto ON\\nRight-click: Turn OFF\\nScroll: +/- 200K"}\n' "$t" "$t"
    else
      local last="$(cat "$TEMP_FILE")"
      printf '{"text":" Off","class":"manual off","tooltip":"Hyprsunset Manual OFF — last %sK\\nLeft-click: Toggle auto ON\\nRight-click: Turn ON (%sK)\\nScroll: +/- 200K"}\n' "$last" "$last"
    fi
  fi
}

case "${1:---print}" in
  --print)        print_json ;;
  --auto-on)      echo -n "1" > "$AUTO_FILE" ; print_json ;;
  --auto-off)     echo -n "0" > "$AUTO_FILE" ; print_json ;;
  --toggle-auto)
    if [ "$(cat "$AUTO_FILE")" = "1" ]; then
      # Turning auto OFF → restore last manual temp and keep running
      echo -n "0" > "$AUTO_FILE"
      set_temp "$(cat "$TEMP_FILE")"
    else
      # Turning auto ON → let daemon steer; ensure hyprsunset is running at current temp (keeps smoothness)
      echo -n "1" > "$AUTO_FILE"
      set_temp "$(cur_temp)"
    fi
    print_json
    ;;
  --toggle)
    # Manual on/off (does not change auto flag)
    if is_running; then
      disable
    else
      set_temp "$(cat "$TEMP_FILE")"
    fi
    print_json
    ;;
  --set)
    t="${2:-$DEFAULT_TEMP}"
    echo -n "$t" > "$TEMP_FILE"
    set_temp "$t"
    print_json
    ;;
  *) echo "Usage: $0 [--print|--toggle-auto|--auto-on|--auto-off|--toggle|--set TEMP]" >&2; exit 1 ;;
esac

