(defwindow bar [monitor]
  :monitor monitor
  :class "ewwbar"
  :windowtype "dock"
  :geometry (geometry :x "0%"
                      :y "1%"
                      :width "99%"
                      :height "5%"
                      :anchor "top center")
                      :exclusive true
  (bar :monitor_ monitor))

(defpoll battery :interval "2s"
  "~/.config/scripts/batteryperc")

(defwidget bar [monitor_]
  (centerbox :orientation "h"
    (winsworks :monitor monitor_)
    (screenrotate)
    (sidestuff)))

(defwidget winsworks [monitor]
  (box :orientation "h" :space-evenly false :halign "start"
    (osk)
    (button :onclick "~/.config/scripts/drawer.sh" :class "music" {"󱂬"})
    (metric :label "󰓃"
            :value volume
            :onchange "pactl set-sink-volume @DEFAULT_SINK@ {}%"
            :onclick "killall pavucontrol || pavucontrol")
  )
)

(defwidget screenrotate []
  (box :orientation "h" :space-evenly false :halign "center"
    (button :class "music" :onclick "~/.config/scripts/screenrotationwcw.sh" "↻")
    (workspaceWidget)
    (button :class "music" :onclick "~/.config/scripts/screenrotationacw.sh" "↺")
  )
)

(defwidget osk []
  (button :class "music" :onclick "~/.config/scripts/onscreenkb.sh" "⌨"))

(defwidget sidestuff []
  (box :class "sidestuff" :orientation "h" :space-evenly false :halign "end"
    (box :class "music" {"${battery}"})
    (box :class "music" {"󰛳 ${IP}"})
    (box
      :tooltip {disks}
      (metric :label ""
              :value {round((1 - (EWW_DISK["/"].free / EWW_DISK["/"].total)) * 100, 0)}
              :onchange ""
              :onclick ""))
    (clock)
    (systray :class "music" :orientation "h" :spacing 2 :space-evenly true)
  ))

(defwidget music []
  (button :class "music"
          :orientation "h"
          :space-evenly false
          :halign "center"
          :onclick "~/.config/scripts/playpause.sh"
    {music != "" ? " ${music}" : " None"}))

(defwidget metric [label value onchange onclick]
  (box :orientation "h"
       :class "metric"
       :space-evenly false
    (button :class "label" :onclick onclick label)
    (scale :min 0
           :max 101
           :active {onchange != ""}
           :value value
           :onchange onchange)))

(deflisten workspace-niri "~/.config/scripts/workspace")
(defwidget workspaceWidget []
  (eventbox
    :onscroll "niri msg action $(echo {} | sed 's/up/focus-workspace-up/;s/down/focus-workspace-down/')"
    (literal :content workspace-niri)))

(defpoll music :interval "0.5s"
  "~/.config/scripts/playerget")

(defpoll IP :interval "5s"
  "~/.config/scripts/ip")

(defpoll volume :interval "0.5s"
  "~/.config/scripts/getvol")

(defpoll time :interval "1s"
  "date '+%H:%M:%S|%d.%m.%Y'")

(defwidget clock []
  (box :class "clock"
       :tooltip {calender}
    (label :text {"${time}"})))

(defpoll calender :interval "600s"
  "~/.config/scripts/calender-fix.sh")

(defpoll disks :interval "600s"
  "~/.config/scripts/dysk-phydisks.sh")
