;; VARIABLE DEFINITIONS


;; Audio
(deflisten vol  "cat ~/.cache/eww/vol && inotifywait -q -m -e close_write ~/.cache/eww/vol | while read -r filename event; do  cat  ~/.cache/eww/vol; done")
(defpoll micvol :interval "1m" "wpctl get-volume @DEFAULT_AUDIO_SOURCE@ | awk '{print $2}'")

;; Workspace Stuff
(deflisten workspaces :initial "[]" "bash ~/.config/eww/scripts/get-workspaces")
(deflisten current_workspace :initial "..." "bash ~/.config/eww/scripts/get-active-workspace")
(deflisten urgent_workspace :initial "..." "bash ~/.config/eww/scripts/get-urgent-workspaces")
(defwidget _workspaces []
  (eventbox :onscroll "bash ~/.config/eww/scripts/change-active-workspace {} ${current_workspace}" :class "workspaces-widget"
    (box :space-evenly false :orientation "vertical"
      (label :text "${workspaces}${current_workspace}${urgent_workspace}" :visible false)
      (for workspace in workspaces
        (eventbox :onclick "hyprctl dispatch workspace ${workspace.id}"
          (box :class "workspace-entry ${workspace.id == current_workspace ? "current" : ""} ${workspace.id == urgent_workspace ? "urgent" : ""} ${workspace.windows > 0 ? "occupied" : "empty" }"
            (label :text "${workspace.id}")
          ))))))

;; Screen
(deflisten backlight "brightnessctl g && inotifywait -q -m -e modify /sys/class/backlight/intel_backlight/actual_brightness | while read -r filename event; do  cat /sys/class/backlight/intel_backlight/actual_brightness; done")


;; Widgets

(defwidget clock []
  (eventbox
    :onhover "eww open cal"
    (box :orientation "vertical"
         :halign "center"
         :valign "end"
         (box :class "clock" {formattime(EWW_TIME, "%H")})
         (box :class "clock" {formattime(EWW_TIME, "%M")})
         )))

(defwidget circle [val prog sty char]
  (box :halign "center"
    :orientation "vertical"
    :valign "end"
    (circular-progress :value prog
      :start-at 10
      :clockwise false
      :thickness 3
      :width 30
      :height 30
      :class "bat"
      :halign "center"
      val )
      (box :class "circle" :style sty char)))

(defwidget battery []
  (circle :prog {EWW_BATTERY.BAT1.capacity * (77778 /100000)}
    :sty "color: ${EWW_BATTERY.BAT1.status == 'Charging' ? '#1a1': EWW_BATTERY.BAT1.capacity < 20 ? '#a11': '#cac'};"
    :val {EWW_BATTERY.BAT1.capacity}
    :char "󱐋"))

(defwidget volume []
  (eventbox
    :onclick "hyprctl dispatch exec 'pavucontrol -t 3'"
    :onscroll "if [ {} == 'up' ]; then /etc/nixos/config/dunst/scripts/volumeNotify.sh 1%+; else /etc/nixos/config/dunst/scripts/volumeNotify.sh 1%-; fi"
    (circle :prog {vol * 7/10}
      :sty "font-size: 12; margin-right: 2px;"
      :val {vol + 1 - 1}
      :char "")))

(defwidget mic []
  (eventbox
    :onclick "hyprctl dispatch exec 'pavucontrol -t 4'"
    :onscroll "if [ {} == 'up' ]; then wpctl set-volume -l 1 @DEFAULT_SOURCE@ 1%+; else wpctl set-volume @DEFAULT_SOURCE@ 1%-; fi"
    (circle :prog {micvol * 70}
      :sty "margin-left: 1px;"
      :val {micvol *100}
      :char "󰍬")))

(defwidget brightness []
  (eventbox
    :onscroll "if [ {} == 'up' ]; then brightnessctl -d intel_backlight set 1%+; else brightnessctl -d intel_backlight set 1%-; fi"
    (circle :prog {(backlight / 960) * 7/10}
      :sty "font-size: 12; margin-right: 3px;"
      :val {round( backlight / 960, 0) - 1}
      :char "󰃠")))

(defwidget power []
  (box :class "power" :valign "end" "⏻"))

(defwidget tray []
  (systray :orientation "v" :icon-size 20))


;; Upper Level Widgets


(defwidget bar []
  (centerbox :orientation "vertical"
    :halign "center"
    :space-evenly false
    (_workspaces)
    (clock)
    (box :orientation "vertical" :spacing -25 :valign "end"
      (brightness)
      (mic)
      (volume)
      (battery)
      (tray)
      (power))))

(defwidget cal []
  (box
    :class "cal"
    (eventbox
      :onhoverlost "eww close cal"
      (calendar))))


;; Windows

(defwindow bar
  :monitor 0
  :geometry (geometry :x "4px"
                      :height "98%"
                      :anchor "left center")
  :stacking "fg"
  :exclusive true
  :class "bar"
  (bar))

(defwindow cal
  :monitor 0
  :geometry (geometry :x "4px"
                      :anchor "left center")
  :stacking "fg"
  :exclusive false
  :class "bar"
  (cal))