;; variables
(defvar window false)
(defpoll time :interval "1s" "date '+%H:%M'")
(defpoll date :interval "1s" "date '+%A, %B %d'")
(deflisten notifications-cards "Main/scripts/logger.zsh subscribe")
(defpoll notifications-crits :interval "1s" "Main/scripts/logger.zsh crits")
(defpoll weather-icon :interval "5m" "Main/scripts/weather --icon")
(defpoll weather-temp :interval "5m" "Main/scripts/weather --temp")
(defpoll weather-desc :interval "5m" "Main/scripts/weather --stat")
(defpoll weather-updatetime :interval "5m" "Main/scripts/weather --updatetime")
(deflisten weather-color :initial "#7aa2f7" "Main/scripts/weather --hex")
(defpoll calendar-day :interval "20h" "+%d")
(defpoll calendar-year :interval "20h" "+%Y")
(defpoll calendar-month :interval "20h" "%+B")
(defpoll cpu :interval "1s" "Main/scripts/system --cpu")
(defpoll memory :interval "1s" "Main/scripts/system --mem")
(defpoll profile-image :interval "10h" "Main/scripts/details --image")
(defpoll profile-username :interval "1h" "whoami")
(defpoll profile-name :interval "1h" "Main/scripts/details --name")
(defpoll profile-kernel :interval "1h" "Main/scripts/details --kernel")
(defpoll profile-os :interval "1h" "Main/scripts/details --os")
(defpoll music-length :interval "1s" "Main/scripts/music-length")
(defpoll music-title :interval "1s" "Main/scripts/music-title --status")
(defpoll music-pp :interval "1s" "Main/scripts/music-title --icon")
(defpoll music-artist :interval "1s" "Main/scripts/music-artist")
(defpoll music-art :interval "1s" "Main/scripts/music-art")
(defpoll music-art-blur :interval "1s" "Main/scripts/music-art-blur")
(defpoll volume :interval "1s" "Main/scripts/system --vol")
(defpoll brightness :interval "1s" "Main/scripts/system --bri")
(defpoll fortune :interval "1h" "Main/scripts/fortune-split 32 6")
(deflisten cava "Main/scripts/cava")
(defpoll title :interval "1s" "Main/scripts/music-title --status")
(defpoll artist :interval "1s" "Main/scripts/player --artist")
(defpoll album :interval "1s" "Main/scripts/player --album")
(defpoll length-info :interval "1s" "Main/scripts/player --length-info")
(defvar volume-hover false)
(defvar brightness-hover false)

;; widgets
(defwidget main []
  (box :class "main-window" :space-evenly "false" :orientation "v"
    (box :class "top-row" :orientation "h" :space-evenly "false"
      (box :class "date-box" :space-evenly "false" :orientation "v" :hexpand "false" :vexpand "false"
        (label :class "time" :text time)
        (label :class "date" :text date))
      (box :class "weather-box" :space-evenly "false" :hexpand "true" :orientation "v"
      (label :class "weather-desc" :halign "start" :text weather-desc)
      (label :class "weather-temp" :halign "start" :text weather-temp)
      (label :class "weather-updatetime" :halign "start" :text weather-updatetime)
      (label :class "weather-icon" :halign "end" :valign "start" :text weather-icon :style "color: ${weather-color}")))
    (box :class "second-row" :orientation "h" :space-evenly "false"
      (box :class "stats-box" :space-evenly "false" :orientation "v" :spacing 8
        (label :class "stats-header" :halign "start" :text "Stats")
        (box :class "volume-stats" :hexpand "false" :vexpand "false" :space-evenly "false"
          (label :tooltip "${volume}%" :class "volume-icon" :text "󰕾")
          (eventbox :class "volume-eventbox"
                    :onhover "eww update volume-hover=true"
                    :onhoverlost "eww update volume-hover=false"
                    :cursor "pointer"
                    :space-evenly "false"
            (scale :min 0 :max 101 :class "volume-scale" :width 150
                   :value {volume == "" ? 0 : volume}
                   :round-digits 0
                   :onchange { volume-hover ? "wpctl set-volume -l 1.0 @DEFAULT_AUDIO_SINK@ {}%" : "" }
                  )))
        (box :class "brightness-stats" :hexpand "false" :vexpand "false" :space-evenly "false"
          (label :tooltip "${brightness}%" :class "brightness-icon" :text "󰃠")
          (eventbox :class "brightness-eventbox"
                    :onhover "eww update brightness-hover=true"
                    :onhoverlost "eww update brightness-hover=false"
                    :cursor "pointer"
                    :space-evenly "false"
            (scale :min 0 :max 101 :class "brightness-scale" :width 150
                   :value {brightness == "" ? 0 : brightness}
                   :round-digits 0
                   :onchange { brightness-hover ? "brightnessctl set {}\% --class=backlight" : "" })))
        (box :class "cpu-stats" :hexpand "false" :vexpand "false" :space-evenly "false"
          (label :tooltip "${cpu}%" :class "cpu-icon" :text "󰘚")
          (scale :min 0 :max 101 :active false :value {cpu == "" ? 0 : cpu} :class "cpu-scale" :width 150))
        (box :class "memory-stats" :hexpand "false" :vexpand "false" :space-evenly "false"
          (label :tooltip "${memory}%" :class "memory-icon" :text "󰍛")
          (scale :min 0 :max 101 :active false :value {memory == "" ? 0 : memory} :class "memory-scale" :width 150)))
      (box :class "fortune-box" :space-evenly "false" :hexpand "true" :orientation "v"
        (label :class "fortune-header" :halign "start" :text "Fortune")
        (label :class "fortune-text" :halign "start" :text fortune :show-truncated true :wrap true)))
    (box :class "third-row" :orientation "h" :space-evenly "false"
      (box :class "music-art-box" :space-evenly "false" :orientation "v" :hexpand "false" :vexpand "false" :style "background-image: url(\"${music-art-blur}\");"
        (box :class "music-stuff-box" :space-evenly "false" :hexpand "false" :vexpand "false" :orientation "v"
          (box :class "player-box" :hexpand "false" :vexpand "false" :space-evenly "true"
            (box :class "player-info-box" :hexpand "false" :vexpand "false" :space-evenly "false" :orientation "v" :spacing 15
              (label :class "player-info-title" :text "󰝚  Title: ${title}" :halign "start" :limit-width 30 :tooltip "${title}")
              (label :class "player-info-artist" :text "󰠃  Artist: ${artist}" :halign "start" :limit-width 30 :tooltip "${artist}")
              (label :class "player-info-album" :text "󰀥  Album: ${album}" :halign "start" :limit-width 30 :tooltip "${album}")
              (label :class "player-info-length" :text "󰦚  Length: ${length-info}" :halign "start" :tooltip "${length-info}"))
            (box :class "player-cover-box" :hexpand "false" :halign "end" :vexpand "false" :space-evenly "false" :style "background-image: url(\"${music-art}\");"))
          (label :vexpand "true" :hexpand "false" :space-evenly "false" :valign "end"
          :class "cavabar"
          :text cava)))
      (box :class "music-controls-box" :space-evenly "true" :hexpand "true" :vexpand "false" :orientation "v"
        (button :class "music-previous" :halign "center" :onclick "playerctl previous" "󰒮")
        (button :class "music-pp" :halign "center" :onclick "playerctl play-pause" "${music-pp}")
        (button :class "music-next" :halign "center" :onclick "playerctl next" "󰒭")))
    (box :class "profile-stuff-box" :space-evenly "false" :hexpand "false" :vexpand "false" :orientation "h" :height 140
      (box :class "profile-img" :space-evenly "false" :hexpand "false" :vexpand "false" :orientation "v" :style "background-image: url(\"${profile-image}\");")
      (box :class "profile-stuff" :orientation "v" :space-evenly "false" :hexpand "false" :vexpand "false" :spacing 8
        (label :class "profile-name" :text profile-name :halign "start")
        (label :class "profile-os" :text profile-os :halign "start")
        (label :class "profile-kernel" :text profile-kernel :halign "start")))))

;; windows
(defwindow main
  :windowtype "normal"
  :wm-ignore true
  :monitor 0
  :geometry (geometry :x 0 :y 0)
  (main))
