(defwidget label_circle [text value]
  (box :class "label_circle" :space-evenly false
    (circular-progress :value {value} :clockwise true :start-at 25 :thickness 3 :class "value"
      (label :text text :class "label" :justify "center"))))


(defwidget label_value [text value ?text_class ?value_class]
  (box :class "label_value" :space-evenly false
    (label :text text :class "label ${text_class}")
    (label :text value :class "value ${value_class}")))


(defwidget sep [?class]
  (label :text "|" :class "sep ${class}"))
(defwidget space []
  (label :text " " :class "space"))


(deflisten workspaces :initial "[]" "~/.config/eww/scripts/get-workspaces")
(defwidget workspaces [monitor ?all]
  (box :space-evenly false :class "workspaces"
    (for workspace in workspaces
      (eventbox :onclick "hyprctl dispatch workspace ${workspace.id}" :visible {(all ?: false) || (workspace.monitor == monitor)} :class "${workspace.active ? "current" : ""} ${workspace.first ? "first" : ""}" :cursor "pointer"
        (label :text {workspace.name})))))


(deflisten window :initial "..." "~/.config/eww/scripts/get-window-title")
(defwidget window []
  (box
    (label :text {window ?: ""})))

(deflisten audio :initial "--" "~/.config/eww/scripts/get-audio")
(defwidget volume []
  (eventbox :cursor {audio.muted ? "pointer" : "row-resize"} :onscroll "~/.config/eww/scripts/adjust-volume {}" :onclick "pactl set-sink-mute @DEFAULT_SINK@ toggle"
    (label_value :text {audio.muted ? "󰝟" : "󰕾"} :value "${audio.volume}%" :value_class {audio.muted ? "muted" : ""})))
(defwidget audio_source []
  (eventbox :onclick "~/.config/eww/scripts/swap-audio-source" :cursor "pointer"
    (label :text {audio.source} :class "audio-info")))
(defwidget audio_sink []
  (eventbox :onclick "~/.config/eww/scripts/swap-audio-sink" :cursor "pointer"
    (label :text {audio.sink} :class "audio-info")))


(defwidget mountpoint [label mount]
  (label_circle :text label :value {EWW_DISK[mount].used_perc}))


(defwidget ram []
  (label_circle :text "" :value {EWW_RAM.used_mem_perc}))


(defwidget cpu []
  (label_circle :text "" :value {EWW_CPU.avg}))

(defvar combined_interface true)
(deflisten interfaces :initial '{}' "~/.config/eww/scripts/network-statistics-shell 5")
(defwidget network_speed [label interface]
  (eventbox :onclick "${EWW_CMD} update combined_interface=${!combined_interface}" :cursor {interfaces?.[interface]?.up ? "pointer" : ""}
    (tooltip
      (graph :value {interfaces?.[interface]?.combined_raw ?: 0} :thickness 2 :time-range 60000 :dynamic true :line-style "round" :width 60 :height 20)
      (label_value :text label :value {(interfaces?.[interface]?.up ?: false) ? (combined_interface ? interfaces?.[interface]?.combined : "󰶡${interfaces?.[interface]?.rx} 󰶣${interfaces?.[interface]?.tx}") : "dc"} :value_class {(interfaces?.[interface]?.up ?: false) ? "" : "disabled"}))))
(defwidget eth []
  (network_speed :label "󰈀" :interface "enp6s0"))
(defwidget wlan []
  (network_speed :label "" :interface "wlo1"))


(defwidget date []
  (tooltip
    (box :orientation "vertical" :space-evenly false
      (label :text {formattime(EWW_TIME, "%A, %B %-d")})
      (calendar :show-heading false :show-day-names false))
    (label :text "${formattime(EWW_TIME, "%Y-%m-%d")}" :class "label-date")))
(defwidget time [?highlighted]
  (label :text "${formattime(EWW_TIME, "%H:%M:%S")}" :class {(highlighted ?: true) ? "label-time" : ""} :tooltip {formattime(EWW_TIME, "%-I:%M %p")}))
(defwidget datetime []
  (box :space-evenly false
    (date)
    (label :text "/" :class "disabled")
    (time)))


(defvar reveal_power false)
(defwidget power []
  (box :space-evenly false :class "power"
    (revealer :transition "slideleft" :reveal reveal_power
      (box :space-evenly true :spacing 2
;        (eventbox :onclick "swaylock" :cursor "pointer"
;          (label :text "" :class "lock"))
        (eventbox :onclick "systemctl suspend" :cursor "pointer"
          (label :text "鈴" :class "sleep"))
        (eventbox :onclick "reboot" :cursor "pointer"
          (label :text "ﰇ" :class "reboot"))
        (eventbox :onclick "shutdown now" :cursor "pointer"
          (label :text "" :class "shutdown"))))
    (eventbox :onclick "${EWW_CMD} update reveal_power=${!reveal_power}" :cursor "pointer"
      (stack :selected {reveal_power ? 1 : 0} :transition "crossfade" :same-size true
        (label :text "" :class "control")
        (label :text "×" :class "control")))))
; Unused entries from my Polybar version of this that may end up being useful later
;menu-0-2 = %{F#7FFF00}
;menu-0-2-exec = systemctl hibernate
;menu-0-3 = %{F#00A2ED}
;menu-0-3-exec = grub-reboot "$(rg -i windows /boot/grub/grub.cfg|head -n 1|cut -d "'" -f 2)" && reboot


(defvar notifications true)
(defwidget notifications []
  (eventbox :onclick "${EWW_CMD} update notifications=${!notifications} ; dunstctl set-paused ${notifications}" :cursor "pointer"
    (stack :selected {notifications ? 0 : 1} :transition "crossfade" :same-size true
      (label :text "" :class "primary")
      (label :text "" :class "primary"))))


(defwidget clicker [text command]
  (eventbox :onclick command :cursor "pointer" :timeout "3600s"
    (label :text text :class "primary")))
(defwidget colour_selector []
  (clicker :text "" :command "uwsm-app -- hyprpicker -a"))
(defwidget wallpaper []
  (clicker :text "󰸉" :command "~/scripts/swww_change.py"))


(defpoll brightness :interval 60 "~/.config/eww/scripts/backlight get")
(defwidget brightness []
  (eventbox :onscroll "~/.config/eww/scripts/backlight {}" :cursor "row-resize"
    (label_value :text "󰌵" :value "${brightness}%")))


(defpoll battery :interval 30 "~/.config/eww/scripts/get-battery")
(defwidget battery []
  (label_value :text {battery.charging ? "󰂄" : battery.icon} :value "${battery.charge}%" :value_class {battery.charge < 30 ? "alert" : ""}))


(defpoll weather :interval 300 "~/scripts/get-weather.sh")
(defpoll weather_icons :interval 0 :run-while false "cat ~/.config/eww/scripts/weather-icons.json")
(defwidget weather []
  (tooltip
    (label :text "${round(weather.current.feels_like, 0)}°C")
    (label_value :text {weather_icons[weather.current.weather[0].icon]} :value "${round(weather.current.temp, 0)}°C")))


(defpoll gametime :interval 10 "~/scripts/gametime.sh status")
(defwidget gametime []
  (eventbox :onclick "~/scripts/gametime.sh toggle" :cursor "pointer"
    (label :text {gametime ? "󰁜" : "󰺷"} :class "primary")))


(defwidget left_bar []
  (centerbox :class "bar" :orientation "h"
    (box :class "left" :orientation "h" :space-evenly false :halign "begin"
      (workspaces :monitor "DP-2")(sep :class "no-left-pad")
      (window))
    (box)
    (box :class "right" :orientation "h" :space-evenly false :halign "end"
      (volume)(sep)
      (audio_source)(space)
      (audio_sink))))
(defwidget right_bar []
  (centerbox :class "bar" :orientation "h"
    (box :class "left" :orientation "h" :space-evenly false :halign "begin"
      (workspaces :monitor "DP-1"))
    (systray :pack_direction "ltr" :icon-size 20)
    (box :class "right" :orientation "h" :space-evenly false :halign "end"
      (gametime)(space)
      (colour_selector)(space)
      (wallpaper)(space)
      (mountpoint :label "/" :mount "/")(space)
      (mountpoint :label "󰋊" :mount "/home/mbradley/hdd")(sep)
      (ram)(space)
      (cpu)(sep)
      (eth)(sep)
      (datetime)(sep)
      (power))))

(defwidget laptop_bar []
  (centerbox :class "bar" :orientation "h"
    (box :class "left" :orientation "h" :space-evenly false :halign "begin"
      (workspaces :monitor "eDP-1" :all true)(sep)
      (window))
    (systray :pack_direction "ltr" :icon-size 20)
    (box :class "right" :orientation "h" :space-evenly false :halign "end"
      (weather)(sep)
      (colour_selector)(space)
      (wallpaper)(sep)
      (ram)(space)
      (cpu)(sep)
      (wlan)(space)
      (battery)(sep)
      (brightness)(space)
      (volume)(sep)
      (datetime)(sep)
      (power))))


(defwindow left
  :monitor 1
  :geometry (geometry :x "0px"
                      :y "4px"
                      :width "1912px"
                      :height "24px"
                      :anchor "top center")
  :stacking "bg"
  :exclusive true
  :focusable false
  (left_bar))
(defwindow right
  :monitor 0
  :geometry (geometry :x "0px"
                      :y "4px"
                      :width "1912px"
                      :height "24px"
                      :anchor "top center")
  :stacking "fg"
  :exclusive true
  :focusable false
  (right_bar))

(defwindow laptop
  :monitor "0x1502"
  :geometry (geometry :x "0px"
                      :y "4px"
                      :width "1912px"
                      :height "24px"
                      :anchor "top center")
  :stacking "fg"
  :exclusive true
  :focusable false
  (laptop_bar))

