(defwidget bar []
  (box :class "bar-widget" :orientation "v" :space-evenly true :halign "center"
    (start)
    (center)
    (apps)
    (and)
  ))


(defwidget start []
  (box :class "start" :orientation "v" :halign "center" :valign "start"
    (launcher)
  )
)


(defwidget center []
  (box :class "center" :halign "center"  :orientation "v"
    (workspaces)
  )
)

(defwidget apps []
  (box :class "apps" :spacing 10 :space-evenly false :valign "end" :halign "center" :orientation "v"
    (volume)
    (spotify)
    (firefox)
  )
)

(defwidget and []
  (box :class "" :valign "end":orientation "v" :space-evenly false :spacing 15
    (bell)
    (central-icon)
    (clock)
  )
)

(defwidget launcher []
  (eventbox
    :cursor "pointer"
    :halign "center"
    (button
      :class "launcher"
      :valign "start"
      :onclick "actions --launcher"
      :width 25
    "" )))

(defwidget spotify []
  (eventbox
    :cursor "pointer"
    :halign "center"
    (button
      :class "launcher"
      :valign "start"
      :onclick "env LD_PRELOAD=/usr/lib/spotify-adblock.so spotify --uri=%U &"
      :width 25
    "" )))

(defwidget firefox []
  (eventbox
    :cursor "pointer"
    :halign "center"
    (button
      :class "firefox"
      :valign "start"
      :onclick "firefox &"
      :width 25
    "󰾔" )))

(defwidget central-icon []
  (eventbox
    :cursor "pointer"
    :halign "center"
    (button
      :class "central-icon"
      :valign "start"
      :onclick "eww open --toggle central"
      :width 20
    "󰙪" )))


(defwidget bell []
  (eventbox
    :cursor "pointer"
    :halign "center"
    (button
      :class "bell"
      :valign "start"
      :onclick "eww open --toggle notifications"
      :width 20
    "󰂚" )))

(defwidget workspaces []
  (box
    :class "workspaces"
    :halign "start"
    :spacing 10
    :orientation "v"
    :space-evenly false
    (for workspace in hyprland
      (eventbox
        :cursor "pointer"
        :onscroll "echo {} | sed -e 's/up/-1/g' -e 's/down/+1/g' | xargs hyprctl dispatch workspace"
        :halign "center"
        (button
          :class "${workspace.current ? "current" : ""} ${workspace.windows > 0 ? "occupied" : "empty"}"
          :onclick "hyprctl dispatch workspace ${workspace.id}"
          (button
            :width 25
            :class "icon ${workspace.current ? "current" : ""} ${workspace.windows > 0 ? "occupied" : "empty"}"
            {workspace.current ? "" : ""}
          )
        )
      )
    )
  )
)

(defwidget music []
  (eventbox
    :onhover "eww update music-rev=true"
    :onhoverlost "eww update music-rev=false"
    (box
      :space-evenly false
      :spacing 10
      :orientation "v"
      (eventbox
        :cursor "pointer"
        (button
          :class "musicbar-art"
          :orientation "v"
          :onclick "eww open-many --toggle player-closer player"
          :style "background-image: url('${music-art}');"
        )
      )
      (label
        :class "musicbar-title"
        :orientation "v"
        :limit-height 10
        :limit-width 35
        :text music-title
      )
      (revealer
        :transition "slideright"
        :reveal music-rev
        :duration "500ms"
        (box
          :spacing 10
          :orientation "h"
          (eventbox
            :cursor "pointer"
            (button
              :class "musicbar-previous-icon"
              :onclick "./scripts/mediacontrol --previous"
              ""
            )
          )
          (eventbox
            :cursor "pointer"
            (button
              :class "musicbar-toggle-icon"
              :onclick "./scripts/mediacontrol --toggle"
              music-toggle
            )
          )
          (eventbox
            :cursor "pointer"
            (button
              :class "musicbar-next-icon"
              :onclick "./scripts/mediacontrol --next"
              ""
            )
          )
        )
      )
    )
  )
)

(defwidget volume []
  (eventbox
    :onhover "eww update volume_rev=true"
    :onhoverlost "eww update volume_rev=false"
    (box
      :class "volume"
      :space-evenly false
      :orientation "v"
      (revealer
        :class "volume-bar"
        :transition "slideup"
        :reveal volume_rev
        (eventbox
          :cursor "pointer"
          :halign "center"
          (scale
            :orientation "v"
            :tooltip {volume == "muted" ? "Muted" : "${volume}%"}
            :value {volume == "muted" ? "0" : volume}
            :min 0
            :max 101
            :onchange "pamixer -u && pamixer --set-volume {}"
            :flipped true
          )
        )
      )
      (eventbox
        :cursor "pointer"
        :onscroll "echo {} | sed -e 's/up/--up/g' -e 's/down/--down/g' | xargs volume"
        (button
          :class "volume icon"
          :style {volume == 0 || volume == "muted" ? "color: #dd6777;" : "color: #cacaca;"}
          :tooltip {volume == "muted" ? "Muted" : "${volume}%" }
          :onclick "volume --toggle"
          {volume == 0 || volume == "muted" ? "󰝟" :
          volume < 30 ? "" :
          volume < 75 ? "" : ""}
        )
      )
    )
  )
)



(defwidget clock []
  (box
    :class "clock"
    :orientation "v"
    :space-evenly false
    :spacing 4
    :halign "center"
    :valign "end"
    (label
      :class "clock hour"
      :text hour
    )
    (box :class "timebox" :spacing 4
    )
    (label
      :class "clock minute"
    :text minute)
  )
)


(defwindow bar
  :monitor 0
  :exclusive true
  :geometry (geometry :x "0%"
    :y "0%"
    :height "99%"
    :width "4%"
  :anchor "center left")
  :reserve (struts :side "top" :distance "4%")
  (bar))
