(defpoll time :interval "1s" "date '+%Y/%m/%d %H:%M:%S'")
(defpoll inhibit_status :interval "1s" "[ -f /tmp/eww-inhibit.pid ] && echo 'on' || echo 'off'")
(defpoll vol :interval "1s" :initial "0" "bash ~/.config/eww/scripts/volume.sh get")
(defpoll vol_icon :interval "1s" :initial "🔉" "bash ~/.config/eww/scripts/volume.sh icon")
(defpoll br :interval "1s" :initial "50" "bash ~/.config/eww/scripts/brightness.sh get")
(defpoll br_icon :interval "1s" :initial "☀️" "bash ~/.config/eww/scripts/brightness.sh icon")
(defpoll battery :interval "10s" :initial "100" "cat /sys/class/power_supply/BAT*/capacity 2>/dev/null || echo 100")
(defpoll battery_status :interval "10s" :initial "Unknown" "cat /sys/class/power_supply/BAT*/status 2>/dev/null || echo Unknown")
(defpoll power_profile :interval "5s" :initial "balanced" "bash ~/.config/eww/scripts/power-profile.sh get")
(defpoll power_profile_icon :interval "5s" :initial "⚖️" "bash ~/.config/eww/scripts/power-profile.sh icon")

(deflisten workspaces :initial "[]" "bash ~/.config/eww/scripts/get-workspaces.sh")

(defwidget workspaces_widget []
  (box :class "workspaces" :orientation "h" :space-evenly false :spacing 5
    (for ws in workspaces
      (button :onclick "swaymsg workspace ${ws.name}"
              :class {ws.focused ? "ws-focused" : "ws-inactive"}
        "${ws.name}"))))

(defwidget date_widget []
  (box :class "date-time"
    (button :onclick "bash ~/.config/eww/scripts/toggle-window.sh calendar"
      time)))

(defwidget volume_widget []
  (box :class "volume-widget"
    (button :onclick "bash ~/.config/eww/scripts/toggle-window.sh volume_popup"
            :onrightclick "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle"
            :class "tray-button"
      "${vol_icon} ${vol}%")))

(defwidget brightness_widget []
  (box :class "brightness-widget"
    (button :onclick "bash ~/.config/eww/scripts/toggle-window.sh brightness_popup"
            :class "tray-button"
      "${br_icon} ${br}%")))

(defwidget caffeine_widget []
  (box :class "caffeine"
    (button :onclick "bash ~/.config/eww/scripts/toggle-inhibit.sh"
            :class "tray-button ${inhibit_status == 'on' ? 'caffeine-on' : 'caffeine-off'}"
      {inhibit_status == "on" ? "☕" : "🌙"})))

(defwidget battery_widget []
  (box :class "battery-widget"
    (button :onclick "bash ~/.config/eww/scripts/toggle-window.sh power_profile_popup"
            :tooltip "Battery: ${battery}% (${battery_status})\nPower Profile: ${power_profile}"
            :class "tray-button"
      "${battery_status == 'Charging' ? '🔌' : 
         battery >= 80 ? '🔋' : 
         battery >= 50 ? '🔋' : 
         battery >= 20 ? '🪫' : 
         '🪫'} ${battery}% ${power_profile_icon}")))

(defwidget tray_widget []
  (box :halign "end" :class "tray-container" :space-evenly false :spacing 10
    (battery_widget)
    (brightness_widget)
    (volume_widget)
    (caffeine_widget)
    (systray :orientation "h" :spacing 5 :icon-size 20 :prepend-new true)))

(defwidget bar_layout []
  (box :class "main-container"
    (centerbox :orientation "h"
      (workspaces_widget)
      (date_widget)
      (tray_widget))))

(defwindow bar
  :monitor "eDP-1"
  :geometry (geometry :x "0%" :y "0%" :width "99%" :height "30px" :anchor "top center")
  :stacking "fg"
  :exclusive true
  :focusable false
  (bar_layout))

(defwindow calendar
  :monitor "eDP-1"
  :geometry (geometry :x "0%" :y "15px" :width "250px" :height "250px" :anchor "top center")
  :stacking "fg"
  :focusable false
  (box :class "calendar-box"
    (calendar)))

(defwindow volume_popup
  :monitor "eDP-1"
  :geometry (geometry :x "15px" :y "15px" :width "300px" :height "60px" :anchor "top right")
  :stacking "fg"
  :focusable false
  (box :class "popup-box" :space-evenly false :spacing 15
    (button :onclick "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle" vol_icon)
    (scale :class "vol-bar" :min 0 :max 101 :value {vol == "" ? 0 : vol} :width 150 :onchange "wpctl set-volume @DEFAULT_AUDIO_SINK@ {}%")
    (label :text "${vol}%")))

(defwindow brightness_popup
  :monitor "eDP-1"
  :geometry (geometry :x "15px" :y "15px" :width "300px" :height "60px" :anchor "top right")
  :stacking "fg"
  :focusable false
  (box :class "popup-box" :space-evenly false :spacing 15
    (label :text br_icon)
    (scale :class "br-bar" :min 0 :max 101 :value {br == "" ? 0 : br} :width 150 :onchange "brightnessctl s {}%")
    (label :text "${br}%")))

(defwindow power_profile_popup
  :monitor "eDP-1"
  :geometry (geometry :x "15px" :y "15px" :width "300px" :height "180px" :anchor "top right")
  :stacking "fg"
  :focusable false
  (box :class "popup-box" :orientation "v" :space-evenly false :spacing 10
    (label :class "popup-title" :text "Power Profile")
    (box :orientation "v" :spacing 8
      (button :class {power_profile == "power-saver" ? "profile-active" : "profile-inactive"}
              :onclick "bash ~/.config/eww/scripts/power-profile.sh set power-saver && bash ~/.config/eww/scripts/toggle-window.sh power_profile_popup"
        "🐢 Power Saver")
      (button :class {power_profile == "balanced" ? "profile-active" : "profile-inactive"}
              :onclick "bash ~/.config/eww/scripts/power-profile.sh set balanced && bash ~/.config/eww/scripts/toggle-window.sh power_profile_popup"
        "⚖️ Balanced")
      (button :class {power_profile == "performance" ? "profile-active" : "profile-inactive"}
              :onclick "bash ~/.config/eww/scripts/power-profile.sh set performance && bash ~/.config/eww/scripts/toggle-window.sh power_profile_popup"
        "🚀 Performance"))
    (label :class "popup-subtitle" :text "Current: ${power_profile}")))
