{{- $tags := .chezmoi.config.data.tags -}}
{{- $compositor := .chezmoi.config.data.compositor -}}
{{- $waybar_output := "eDP-1" -}}
{{- range .monitors -}}
  {{- if index . "waybar" -}}
    {{- $waybar_output = .name -}}
  {{- else if index . "primary" -}}
    {{- $waybar_output = .name -}}
  {{- end -}}
{{- end -}}
{{- $workspace_module := "" -}}
{{- if (eq $compositor "hyprland") -}}
  {{- $workspace_module = "hyprland/workspaces" -}}
{{- else if (eq $compositor "niri") -}}
  {{- $workspace_module = "niri/workspaces" -}}
{{- end -}}
{
  "layer": "top",
  "position": "right",
  "output": "{{ $waybar_output }}",
  "width": 54,
  "spacing": 4,
  "margin-top": 0,
  "margin-bottom": 0,

  "modules-left": [
    "clock",
    "custom/swaync",
    "custom/alhp"{{- if $workspace_module }},
    "{{ $workspace_module }}"{{- end }}
  ],

  "modules-center": [
    "cpu",
    "memory",
    "disk#root",
    "network",
    "group/temps"
  ],

  "modules-right": [
    "idle_inhibitor",
    {{- if (eq $compositor "hyprland") }}
    "custom/hyprsunset",
    {{- end }}
    "gamemode",
    "privacy",
    "group/volume",
    "tray",
    {{- if .chezmoi.config.data.tags.bluetooth }}
    "bluetooth",
    {{- end }}
    {{- if .chezmoi.config.data.tags.laptop }}
    "power-profiles-daemon",
    "battery"
    {{- end }}
  ],

  {{- if (eq $compositor "hyprland") }}
  "hyprland/workspaces": {
    "format": "{icon}",
    "all-outputs": true,
    "sort-by": "id",
    "format-icons": {
      "1": "1",
      "2": "2",
      "3": "3",
      "4": "4",
      "5": "5",
      "6": "6",
      "7": "7",
      "8": "8",
      "9": "9",
      "10": "10",
      "comms": "",
      "element": "",
      "mail": "",
      "joplin": "",
      "steam": "",
      "spotify": "",
      "default": "",
      "special": ""
    },
    "on-scroll-up": "hyprctl dispatch workspace e+1",
    "on-scroll-down": "hyprctl dispatch workspace e-1"
  },
  {{- else if (eq $compositor "niri") }}
  "niri/workspaces": {
    "format": "{value}",
    "all-outputs": true
  },
  {{- end }}

  "cpu": {
    "interval": 2,
    "format": "{usage}%",
    "states": {
      "warning": 75,
      "critical": 90
    }
  },

  "memory": {
    "interval": 5,
    "format": " {percentage}%\n {swapPercentage}%",
    "tooltip-format": "RAM: {used:0.1f}G/{total:0.1f}G\nSWAP: {swapUsed:0.1f}G/{swapTotal:0.1f}G",
    "states": {
      "warning": 75,
      "critical": 90
    }
  },

  "disk#root": {
    "interval": 30,
    "format": "/ {percentage_used}%",
    "path": "/",
    "states": {
      "warning": 80,
      "critical": 90
    }
  },

  "network": {
    "interval": 3,
    "format-wifi": " {signalStrength}%",
    "format-ethernet": "",
    "tooltip-format-wifi": "{signalStrength}\n {bandwidthUpBytes}\n {bandwidthDownBytes}",
    "tooltip-format-ethernet": " {bandwidthUpBytes}\n {bandwidthDownBytes}",
    "format-disconnected": "",
    "tooltip-format-disconnected": "Disconnected"
  },

  "idle_inhibitor": {
    "format": "{icon}",
    "format-icons": {
      "activated": "",
      "deactivated": ""
    },
    "tooltip-format-activated": "Idle inhibited",
    "tooltip-format-deactivated": "Idle allowed"
  },

  "gamemode": {
    "format": ""
  },

  "privacy": {
    "icon-size": 16
  },

  "custom/mic-usb": {
    "exec": "mic-usb-status",
    "return-type": "json",
    "interval": 2,
    "format": " {text}",
    "tooltip": true,
    "on-click": "mic-toggle-usb"
  },

  "wireplumber#source": {
    "node-type": "Audio/Source",
    "format": " {volume}%",
    "format-muted": "",
    "tooltip-format": "{node_name} {volume}%",
    "scroll-step": 5,
    "on-click": "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle",
    "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SOURCE@ 5%+",
    "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SOURCE@ 5%-"
  },

  "wireplumber#sink": {
    "format": "{icon} {volume}%",
    "format-muted": "",
    "format-icons": ["", "", "󰕾", ""],
    "scroll-step": 2,
    "max-volume": 140,
    "tooltip-format": "{node_name} {volume}%",
    "on-click": "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle",
    "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 2%+",
    "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 2%-"
  },

  "tray": {
    "icon-size": 16,
    "spacing": 6
  },

  "clock": {
    "format": "{:%H:%M}",
    "tooltip-format": "<tt><small>{calendar}</small></tt>",
    "calendar": {
      "mode": "month",
      "weeks-pos": "right",
      "on-scroll": 1,
      "format": {
        "months": "<span color='#ffb700'><b>{}</b></span>",
        "weekdays": "<span color='#404040'><b>{}</b></span>",
        "days": "<span>{}</span>",
        "today": "<span color='#ff0044'><b><u>{}</u></b></span>"
      }
    },
    "actions": {
      "on-click-right": "mode",
      "on-scroll-up": "shift_up",
      "on-scroll-down": "shift_down"
    }
  },

  "power-profiles-daemon": {
    "format": "{icon} {profile}",
    "tooltip-format": "Power profile: {profile}\nDriver: {driver}",
    "tooltip": true,
    "format-icons": {
      "performance": "",
      "balanced": "",
      "power-saver": "",
      "default": ""
    }
  },

  "battery": {
    "interval": 30,
    "states": { "warning": 30, "critical": 15 },
    "format": "{capacity}% {icon}",
    "format-time": "{H}h {M}m",
    "format-icons": ["", "", "", "", ""],
    "tooltip": true,
    "events": {
      "on-discharging-warning": "notify-send -u normal 'Battery low'",
      "on-discharging-critical": "notify-send -u critical 'Battery very low'",
      "on-charging-100": "notify-send -u normal 'Battery full'"
    }
  },

  "bluetooth": {
    "format": "",
    "format-disabled": "",
    "format-off": "",
    "format-on": "",
    "tooltip": true,
    "on-click": "blueman-manager"
  },


  "group/temps": {
    "orientation": "vertical",
    "drawer": {
      "transition-duration": 300,
      "transition-left-to-right": true,
      "click-to-reveal": true
    },
    "modules": [
      "custom/temps-cycle",        // leader, always visible
      "temperature#cpu",
      "temperature#gpu_hotspot",
      "temperature#nvme"
    ]
  },

  "custom/temps-cycle": {
    "exec": "$HOME/.config/waybar/scripts/temps-cycle.sh",
    "return-type": "json",
    "interval": 3,
    "format": "{text}",
    "tooltip": true
  },

  "temperature#cpu": {
    "hwmon-path-abs": "/sys/devices/pci0000:00/0000:00:18.3/hwmon",
    "input-filename": "temp1_input",
    "interval": 5,
    "warning-threshold": 80,
    "critical-threshold": 85,
    "format": " {temperatureC}°C",
    "format-critical": " {temperatureC}°C ",
    "tooltip-format": "CPU Tctl: {temperatureC}°C"
  },

  "temperature#gpu_edge": {
    "hwmon-path-abs": "/sys/devices/pci0000:00/0000:00:01.1/0000:10:00.0/0000:11:00.0/0000:12:00.0/hwmon",
    "input-filename": "temp1_input",
    "interval": 5,
    "warning-threshold": 90,
    "critical-threshold": 100,
    "format": "GPU {temperatureC}°C",
    "format-critical": "GPU {temperatureC}°C ",
    "tooltip-format": "GPU edge: {temperatureC}°C"
  },

  "temperature#gpu_hotspot": {
    "hwmon-path-abs": "/sys/devices/pci0000:00/0000:00:01.1/0000:10:00.0/0000:11:00.0/0000:12:00.0/hwmon",
    "input-filename": "temp2_input",
    "interval": 5,
    "warning-threshold": 100,
    "critical-threshold": 110,
    "format": " {temperatureC}°C",
    "format-critical": " {temperatureC}°C ",
    "tooltip-format": "GPU junction: {temperatureC}°C"
  },

  "temperature#gpu_mem": {
    "hwmon-path-abs": "/sys/devices/pci0000:00/0000:00:01.1/0000:10:00.0/0000:11:00.0/0000:12:00.0/hwmon",
    "input-filename": "temp3_input",
    "interval": 5,
    "warning-threshold": 90,
    "critical-threshold": 100,
    "format": " {temperatureC}°C",
    "format-critical": " {temperatureC}°C ",
    "tooltip-format": "GPU memory: {temperatureC}°C"
  },

  "temperature#nvme": {
    "hwmon-path-abs": "/sys/devices/pci0000:00/0000:00:02.2/0000:23:00.0/nvme/nvme0/hwmon",
    "input-filename": "temp1_input",
    "interval": 10,
    "warning-threshold": 75,
    "critical-threshold": 85,
    "format": " {temperatureC}°C",
    "format-critical": " {temperatureC}°C ",
    "tooltip-format": "NVMe Composite: {temperatureC}°C"
  },

  "custom/swaync": {
    "exec-if": "which swaync-client >/dev/null 2>&1 && pgrep -x swaync >/dev/null 2>&1",
    "exec": "$HOME/.config/waybar/scripts/swaync-count.sh",
    "return-type": "json",
    "interval": 2,
    "format": "{text}",
    "on-click": "swaync-client -t -sw",
    "on-click-right": "swaync-client -d",
    "on-click-middle": "swaync-client -C"
  },

  {{- if (eq $compositor "hyprland") }}
  "custom/hyprsunset": {
    "format": "SUN",
    "tooltip": true,
    "tooltip-format": "Hyprsunset controls: Left=Enable, Right=Disable, Scroll=Gamma ±10%",

    "on-click": "hyprctl hyprsunset temperature 2500", // enable warm tint
    "on-click-right": "hyprctl hyprsunset identity",   // disable (neutral)
    "on-scroll-up": "hyprctl hyprsunset gamma +10",    // brighter
    "on-scroll-down": "hyprctl hyprsunset gamma -10",  // dimmer

    "interval": 0
  },
  {{- end }}

  "custom/alhp": {
    "id": "custom-alhp",
    "exec": "$HOME/.config/waybar/scripts/alhp.sh",
    "return-type": "json",
    "interval": 60,
    "tooltip": true,
    "format": "{text}",
    "on-click": "kitty --hold alhp.utils -j"
  },

  "group/volume": {
    "orientation": "vertical",
    "modules": [
      "custom/mic-usb",
      "wireplumber#source",
      "wireplumber#sink"
    ]
  }
}
