;; Variables
(defvar power false)
(defvar show-date false)
(defvar show-volume false)
(defvar volume-muted false)

(defvar ha-now-playing "ha-now-playing --host home.datarift.nl --token-file /run/secrets/ha_now_playing_token --entity media_player.sonos_woonkamer")
;; Polls
(defpoll volume-current
         :initial 0
         :interval "1s"
         `pamedia get -b`)

;; Listen
(deflisten sonos-now-playing
           :initial "{\"muted\":false,\"text\":\"\"}"
           `ha-now-playing --host home.datarift.nl --token-file /run/secrets/ha_now_playing_token --entity media_player.sonos_woonkamer`)

(deflisten wm-info
           :initial "{\"workspaces\":[],\"mode\":\"default\",\"title\":\"\"}"
           `./scripts/workspaces.nu`)

;; Widgets
(defwidget mode []
           (box :space-evenly false
                :spacing 2
                :class "mode"
                (label :class "icon"
                       :visible {wm-info.mode != "default"}
                       :text "󰁌")
                (label :visible {wm-info.mode != "default"}
                       :text {wm-info.mode})))

(defwidget workspaces []
           (box :space-evenly false
                :spacing 5
                :class "workspaces"
                (box :space-evenly false
                     :spacing 0
                     (for ws in {wm-info.workspaces}
                          (button :onclick "swaymsg workspace ${ws.name}"
                                  :class "ws${ws.visible ? ' visible' : ''}${ws.focused ? ' focused' : ''}${ws.urgent ? ' urgent' : ''}"
                                  (box :space-evenly false
                                       :spacing 2
                                       (label :class "icon small"
                                              :visible {!ws.focused && !ws.urgent}
                                              :text "")
                                       (label :class "icon small"
                                              :visible {ws.focused}
                                              :text "")
                                       (label :class "icon small"
                                              :visible {ws.urgent}
                                              :text "")
                                       (label :text "${ws.name}"))))
                (mode))))

;; (defwidget workspaces []
;;            (box :space-evenly false
;;                 :spacing 5
;;                 :class "workspaces"
;;                 (label :text {wm-info.title})))

(defwidget ha-now-playing [visible]
           (eventbox :onclick "${ha-now-playing} play-pause"
                     :onrightclick "${ha-now-playing} ${sonos-now-playing.muted ? 'unmute' : 'mute'}"
                     :onscroll "${ha-now-playing} volume-{}"
                     :active true
                     :halign "start"
                     :visible visible
                     (box :class "ha-now-playing"
                          :space-evenly false
                          :spacing 2
                          (label :class "icon"
                                 :text "󰐹")
                          (label :text {sonos-now-playing.text}))))

(defwidget date-time []
           (eventbox :onclick "${EWW_CMD} update show-date=${!show-date}"
                     :cursor "pointer"
                     (box :class "time"
                          :space-evenly false
                          :spacing 2
                          (label :visible {!show-date}
                                 :class "icon"
                                 :text "󰅐")
                          (label :visible {!show-date}
                                 :text "${formattime(EWW_TIME, '%H:%M:%S')}")
                          (label :visible show-date
                                 :class "icon"
                                 :text "󰃭")
                          (label :visible show-date
                                 :text "${formattime(EWW_TIME, '%a, %d %b %Y')}"))))


(defwidget memory []
           (box :class "memory"
                :space-evenly false
                :spacing 2
                :tooltip "${round(EWW_RAM.used_mem / 1024 / 1024 / 1024, 2)}GiB / ${round(EWW_RAM.total_mem / 1024 / 1024 / 1024, 2)}GiB used"
                (label :class "icon"
                       :text "󰍛")
                (label :text "${round(EWW_RAM.used_mem_perc, 1)}%")))

(defwidget cpu []
           (box :class "cpu"
                :space-evenly false
                :spacing 2
                :tooltip {jq(EWW_CPU, "reduce .cores[] as $core (\"\"; . + $core.core + \": \" + ($core.usage | tostring) + \"%\\n\") | {tooltip:.}").tooltip}
                (label :class "icon"
                       :text "󰻠")
                (label :text "${round(EWW_CPU.avg, 2)}%")))

(defwidget temperature []
           (box :class "temperature"
                :space-evenly false
                :spacing 2
                :tooltip {jq(EWW_TEMPS, ". | to_entries | reduce .[] as $temp (\"\"; . + $temp.key + \": \" + ($temp.value | tostring) + \"\\n\") | {tooltip:.}").tooltip}
                (label :class "icon"
                       :text "󰔏")
                (label :text "${round(EWW_TEMPS.ZENPOWER_TDIE, 1)}°C")))

(defwidget volume []
           (eventbox :onhover "${EWW_CMD} update show-volume=true"
                     :onhoverlost "${EWW_CMD} update show-volume=false"
                     :onclick "pamedia mute"
                     :onmiddleclick "helvum"
                     :onrightclick "pavucontrol"
                     :tooltip "Volume: ${volume-current}%"
                     :cursor "pointer"
                     (box :space-evenly false
                          :spacing 5
                          :class "volume"
                          (revealer :transition "slideleft"
                                    :reveal show-volume
                                    :duration "550ms"
                                    (scale :class "volume-bar"
                                           :flipped true
                                           :value volume-current
                                           :orientation "h"
                                           :min 0
                                           :max 101
                                           :round-digits 0
                                           :tooltip "Volume: ${volume-current}%"
                                           :onchange "pamedia set {}"))
                          (label :class "icon"
                                 :visible {!volume-muted}
                                 :text "󰕾")
                          (label :class "icon"
                                 :visible volume-muted
                                 :text "󰸈"))))

(defwidget control []
           (box :class "control"
                :space-evenly false
                :spacing 2
                (volume)
                (cpu)
                (temperature)
                (memory)))

(defwidget power []
           (eventbox :onhover "${EWW_CMD} update power=true"
                     :onhoverlost "${EWW_CMD} update power=false"
                     :cursor "pointer"
                     (box :space-evenly false
                          :class "powermenu"
                          (revealer :transition "slideleft"
                                    :reveal power
                                    :duration "550ms"
                                    (box :orientation "h"
                                         :space-evenly false

                                         (button :class "button-reboot"
                                                 :tooltip "Reboot"
                                                 :onclick "systemctl reboot" "󰜉")

                                         (button :class "button-quit"
                                                 :tooltip "Logout"
                                                 :onclick "i3-msg exit" "󰗼")

                                         (button :class "button-hibernate"
                                                 :tooltip "Hibernate"
                                                 :onclick "systemctl hibernate" "󰤄")))

                          (button :class "button-shutdown"
                                  :tooltip "Shutdown"
                                  :onclick "systemctl shutdown" "󰐥"))))

(defwidget headset []
           (button :class "headset-button"
                   :onclick "headsetcontrol -s 128"
                   :onrightclick "headsetcontrol -s 0"
                   "󰋎 "))

(defwidget title []
           (label :text {wm-info.title}))

(defwidget left [is-home]
           (box :orientation "h"
                :halign "start"
                :space-evenly false
                :spacing 5
                (workspaces)
                (ha-now-playing
                 :visible is-home)))

(defwidget right []
           (box :orientation "h"
                :space-evenly false
                :halign "end"
                (date-time)
                (power)))

(defwidget end []
           (box :orientation "h"
                :halign "end"
                :space-evenly false
                (children)
                (control)
                (right)))

(defwidget bar []
           (box :class "eww_bar"
                :orientation "h"
                :hexpand false
                :vexpand false
                (centerbox
                 (left :is-home true)
                 (title)
                 (end))))

(defwidget bar-work-left []
           (box :class "eww_bar"
                :orientation "h"
                :hexpand false
                :vexpand false
                (centerbox
                 (left :is-home false)
                 (title)
                 (end
                  (headset)))))

;; Bar windows
(defwindow bar-home
           :geometry (geometry :x "0"
                               :y "0"
                               :width "100%"
                               :height "32px"
                               :anchor "top center")
           :stacking "fg"
           :monitor 0
           :reserve (struts :distance "47px"
                            :side "top")
           :wm-ignore false
           :hexpand false
           :vexpand false
           :exclusive true
           (bar))

(defwindow bar-work
           :geometry (geometry :x "0"
                               :y "0"
                               :width "100%"
                               :height "32px"
                               :anchor "top center")
           :stacking "fg"
           :monitor 0
           :exclusive true
           :hexpand false
           :vexpand false
           (bar-work-left))