; TODO: Update this value when pressing the button.
(defpoll bright :interval '1s' "brightnessctl -m | cut -d , -f 4 | head -c -2")

(defpoll ssid :interval "1s" `nmcli -terse -fields SSID,ACTIVE device wifi | awk --field-separator ':' '{if($2=="yes")print$1}'`)
(defpoll bluethooth :interval '1s' "echo info | timeout 1 bluetoothctl | grep 'Name'")
(defpoll theme :interval '1s' "cat ~/.local/state/theme")


(defwidget stat_card [icon name percent value colorClass ?click]
 (button
  :click click
  (box
   :space-evenly false
   :class "disclose-stats-box"
   (circular-progress :value percent
    :thickness 10
    :class "${colorClass}-txt"
    (label :halign "center" :class "stats-label ${colorClass}-txt" :text icon))
   (label :class "stats-separator" :text "")
   (box :hexpand true :halign "center" :orientation "vertical" :class "disclose-stats-info-box"
    (label :halign "center" :class "info-value ${colorClass}-txt" :text value)
    (label :halign "start" :class "info-label" :text name)))))

(defwidget dstats []
    (box :space-evenly false
       :orientation "vertical"
       :class "disclose-stats"
       :spacing 10
       (stat_card
           :icon ""
           :name "CPU"
           :percent 0
           :value {round(EWW_CPU.avg, 2)}
           :colorClass "red")
       (stat_card
           :icon ""
           :name "Memory"
           :percent {EWW_RAM.used_mem_perc}
           :value "${round(EWW_RAM.used_mem_perc, 0)}%"
           :colorClass "green")
       (stat_card
           :icon {theme == "light" ? "󰃞" : "󰃝"}
           :name "Brightness"
           :percent bright
           :value "${bright}%"
           :colorClass "blue"
           :click "settheme")))


(defwidget wifi [] 
    (box
        :class "module"
        :spacing 16
        :style "margin: 8px 16px 16px 16px"
        (button
            :class "accent disclose-big-button"
            :click "nm-connection-editor"
            "直 : ${ssid != "" ? ssid : "Disconnected"}")
        (button
            :class "secondary disclose-big-button"
            :onclick "blueberry"
            " : ${bluethooth != "" ? bluethooth : 'Disconnected'}")))

;; vim:filetype=yuck
