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

(deflisten current_workspace :initial "1" "bash ~/.config/eww/scripts/get-active-workspace")

(defwidget workspaces [monitor]
  (eventbox :onscroll "bash ~/.config/eww/scripts/change-active-workspace {} ${current_workspace}" :class "workspaces-widget"
    (box :spacing 10 :orientation "h" :halign "start" :space-evenly false
      (label :text "${workspaces}${current_workspace}" :visible false)
      (for workspace in workspaces
        (box
          :visible {workspace.output == monitor}
          (eventbox :onclick "swaymsg workspace number ${workspace.id}"
            (box :class "workspace-entry ${workspace.focused ? 'current' : ''} ${workspace.urgent ? 'urgent' : ''}"
              (label :text "${workspace.id}"))))))))


(defpoll time :interval "1s"
  "date '+%H:%M'")

(defpoll time_tooltip :interval "1s"
  "date '+%A, %B %d, %Y'")


(defwidget time-widget []
  (label
    :text {time}
    :tooltip time_tooltip
    :class "time-label"))

(defwidget notification-widget []
  (eventbox :class "notification-eventbox" :onclick "bash ~/.config/eww/scripts/notification-history-toggle"
            :tooltip "Toggle notification history"
    (label :text "" :class "notification-icon")))

(deflisten BATPERC :initial "" "bash ~/.config/eww/scripts/battery-perc-monitor")
(deflisten BATICON :initial "" "bash ~/.config/eww/scripts/battery-icon-monitor")
(deflisten BATTIME :initial "" "bash ~/.config/eww/scripts/battery-time-monitor")

(defwidget battery-widget []
  (box :orientation "horizontal" :spacing 0 :tooltip "Time remaining: ${BATTIME}"
    (label :text {BATICON} :class "battery-icon")
    (label :text {BATPERC} :class "battery-percent")))

(defwidget internet []
  (label :text wifi-icon
         :tooltip wifi-name
         :class "wifi-icon"))
(deflisten wifi-icon :initial "󰪎" "bash ~/.config/eww/scripts/wifi-monitor")
(deflisten wifi-name :initial "" "bash ~/.config/eww/scripts/wifi-name-monitor")

(deflisten brightness :initial "50" "bash ~/.config/eww/scripts/brightness")

(defwidget brightness-widget []
  (eventbox :onscroll "[ {} = up ] && brightnessctl set 10%+ || brightnessctl set 10%-"
            :tooltip "Brightness: ${brightness}%"
    (box :orientation "h" :spacing 0
      (label :text "󰌵" :class "brightness-icon")
      (scale :min 0
             :max 100
             :value brightness
             :class "brightness-bar"
             :onchange "brightnessctl set {}%"))))

(deflisten volume :initial "50" "bash ~/.config/eww/scripts/volume")
(deflisten volume-icon :initial "" "bash ~/.config/eww/scripts/volume-icon-monitor")

(defwidget volume-widget []
    (eventbox :onscroll "[ {} = up ] && pactl set-sink-volume @DEFAULT_SINK@ +10% || pactl set-sink-volume @DEFAULT_SINK@ -10%"
              :tooltip "Volume: ${volume}%"
    (box :orientation "h" :spacing 0
      (label :text volume-icon :class "volume-icon")
      (scale :min 0
             :max 100
             :value volume
             :class "volume-bar"
           :onchange "pactl set-sink-volume @DEFAULT_SINK@ {}%"))))

(defwidget system []
  (box :orientation "h" :halign "end" :spacing 0
    (internet)
    (brightness-widget)
    (volume-widget)
    (battery-widget)
    (time-widget)
    (notification-widget)))


(defwidget bar [monitor]
  (box :orientation "h" :space-evenly false
    (workspaces :monitor monitor)
    (box :hexpand true)
    (system)))

(defwindow bar0
           :monitor 0
           :geometry (geometry :x "0%"
                               :y "0px"
                               :width "100%"
                               :height "30px"
                               :anchor "top center")
           :stacking "fg"
           :exclusive true
           :reserve (struts :distance "40px" :side "top")
           :windowtype "dock"
           :wm-ignore false
           (bar :monitor "HDMI-A-1"))

(defwindow bar1
           :monitor 1
           :geometry (geometry :x "0%"
                               :y "0px"
                               :width "100%"
                               :height "30px"
                               :anchor "top center")
           :stacking "fg"
           :exclusive true
           :reserve (struts :distance "40px" :side "top")
           :windowtype "dock"
           :wm-ignore false
           (bar :monitor "eDP-1"))


