;; Variables

(include "sidebar.yuck")
(include "notifications.yuck")
(include "battery.yuck")
(include "volume.yuck")

(defpoll clock_time :interval "5s" "date '+\%l:\%M \%p'")
(defpoll clock_full :interval "5s" "date '+\%a \%b \%d, \%-I:\%M \%p'")
(defpoll clock_date :interval "1h" "date '+%b %d, %Y'")
(defpoll calendar_day :interval "2h" "date '+%d'")
(defpoll calendar_year :interval "2h" "date '+%Y'")
(defpoll mic_percent :interval "3s" "pamixer --get-volume --default-source")
(defpoll brightness_percent :interval "5s" "scripts/bright")
(defpoll COL_WLAN :interval "1m" "scripts/wifi --COL")
(defpoll ESSID_WLAN :interval "1m" "scripts/wifi --ESSID")
(defpoll WLAN_ICON :interval "1m" "scripts/wifi --ICON")
(defpoll HOST :interval "1h" "hostname")
(defpoll DISK :interval "1m" "disk")
;; (defpoll nextcloud :interval "2s" "scripts/nextcloud.sh")
(deflisten volume_percent :interval "0.2s"  "tail -F /tmp/vol")
(deflisten appname "scripts/appname")

(deflisten workspaces :initial "[]" "scripts/get-workspaces")
(deflisten current_workspace :initial "1" "scripts/get-active-workspace")

(defvar vol_reveal false)
(defvar br_reveal false)
(defvar music_reveal false)
(defvar control_reveal false)
(defvar wifi_rev false)
(defvar time_rev false)


;; widgets

(defwidget workspaces []
           (eventbox
            :onscroll "echo {} | sed -e 's/up/-1/g' -e 's/down/+1/g' | xargs hyprctl dispatch workspace"
            :valign "center" 
            (box
             :class "worksbox"
             :orientation "h"
             :spacing 10
             :space-evenly false
             :valign "center"
             (for workspace in workspaces
                  (button
                   :onclick "hyprctl dispatch workspace ${workspace.id}"
                   :class "works ${workspace.id == current_workspace ? "current_workspace" : ""} ${workspace.windows > 0 ? "occupied_workspace" : "empty_workspace"}"
                   :visible {workspace.windows > 0 ? "true" : workspace.id == current_workspace ? "true" : "false"}
                   "${workspace.id == 1 ? "" :
                      workspace.id == 2 ? "" :
                      workspace.id == 3 ? "󰈙" :
                      workspace.id == 4 ? "󰭹" :
                      workspace.id == 5 ? "" :
                      workspace.id == 6 ? "" :
                      workspace.id == 7 ? "󰕧" :
                      workspace.id == 8 ? "󰭹" :
                      workspace.id == 9 ? "" :
                      workspace.id}")))))
 
 

(defwidget appnames []
  (eventbox
    :onhover "${EWW_CMD} update control_reveal=true"
    :onhoverlost "${EWW_CMD} update control_reveal=false"
    (box
     :orientation "h"
     :space-evenly "false"
     :vexpand "false"
     :hexpand "false" 
     (literal 
      :content appname)
     (revealer
      :transition "slideright"
      :reveal control_reveal
      :duration "250ms"
      (box
       :vexpand "false"
       :hexpand "false"
       :orientation "h"
       :space-evenly "false"
       (button
        :class "app-button"
        :onclick "hyprctl dispatch togglefloating 1" "") 
       (button
	:class "app-button"
	:onclick "hyprctl dispatch fullscreen 1" "")
       (button
	:class "app-button"
        :onclick "hyprctl dispatch killactive 1" ""))))))

(defwidget wifi [] 
  (eventbox 
    :onhover "${EWW_CMD} update wifi_rev=true"
    :onhoverlost "${EWW_CMD} update wifi_rev=false"
    (box
     :class "wifi"
     :vexpand "false"
     :hexpand "false"
     :space-evenly "false"
     (revealer 
      :transition "slideright"
      :reveal wifi_rev
      :duration "250ms"
      (label
       :class "module_essid"
       :text ESSID_WLAN))
     (button
      :class "module-wifi"
      :onclick "alacritty -e nmtui"
      WLAN_ICON))))

(defwidget bat []
	   (box
	    :class "bat_module"
	    :vexpand "false"
	    :hexpand "false" 
            :space-evenly false
            :spacing 0
            :visible {HOST == "kaladin" ? "false" : "true"}
            (button 
             :onclick "scripts/pop system"
             :class "iconbat"
             :tooltip "${EWW_BATTERY["BAT1"].capacity}% battery left"
             "")
            (label
             :text "${round(EWW_BATTERY['BAT1'].capacity, 0)}%"
             :class "bat_label")))

(defwidget disk []
	   (box
	    :class "disk_module"
	    :vexpand "false"
	    :hexpand "false" 
            :space-evenly false
            :spacing 0
            (button 
             :onclick "scripts/pop system"
             :class "icondisk"
             :tooltip "${DISK}"
             "󰋊")
            (label
             :text "${DISK}"
             :class "disk_label")))

(defwidget bright []
	   (box
	    :class "bright_module"
	    :vexpand "false"
	    :hexpand "false" 
            :space-evenly false
            :visible {HOST == "kaladin" ? "false" : "true"}
            (button 
             :class "iconbright"
             ;; :onclick "scripts/pop audio"
	     :tooltip "${brightness_percent}%"
             "")
            (label
             :text brightness_percent
             :class "bright_label"))
           )

(defwidget volume []
	   (box
	    :class "vol_module"
	    :vexpand "false"
	    :hexpand "false" 
            :space-evenly false
            (button 
             :class "iconvol"
             :onclick "scripts/pop audio"
	     :tooltip "${volume_percent}%"
             "󰕾")
            (label
             :text volume_percent
             :class "vol_label")))

(defwidget cpu []
	   (box
	    :class "cpu_module"
	    :vexpand "false"
	    :hexpand "false" 
            :space-evenly false
            (button 
             :onclick "scripts/pop system"
             :class "iconcpu"
             :tooltip "using ${EWW_CPU.avg}% of cpu"
             "")
            (label
             :text "${round(EWW_CPU.avg, 0)}%"
             :class "cpu_label")))

(defwidget mem []
	   (box 
            :class "mem_module" 
            :space-evenly false
            :vexpand "true" 
            :hexpand "false" 
            (button 
             :onclick "scripts/pop system"
             :class "iconmem"
             :tooltip "using ${EWW_RAM.used_mem_perc}% ram"
             "")
            (label
             :text "${round(EWW_RAM.used_mem_perc, 0)}%"
             :class "mem_label")))

(defwidget kdeconnect []
           (box
            :class "kdeconnect_module"
            :vexpand "false"
            :hexpand "false"))

(defwidget clock_module []
           (eventbox
            :onhover "${EWW_CMD} update time_rev=true"
  	    :onhoverlost "${EWW_CMD} update time_rev=false"
            (box
             :class "module"
             :space-evenly "false"
             :orientation "h"
             :spacing "0" 
             (label
              :text clock_full
              :class "clock_time_class" ))))
  
(defwidget notif_button []
  (box
    :class "notif_button"
    :space-evenly "false"
    :orientation "h"
    (button
     :onclick "if ${EWW_CMD} state | grep 'rightside: false' ; then ${EWW_CMD} update rightside=true ; else ${EWW_CMD} update rightside=false ; fi"
      (label
      :text "󰅽"))))
  
(defwidget sep []
           (label
            :class "separ"
            :text "|"))

(defwidget gap []
  (box 
    :orientation "h"
    :hexpand "false"
    :visible "true"
    :width "150"))

(defwidget tray []
           (systray
            :class "tray"
            :hexpand "false"
            :halign "center"
            :visible "true"
            :spacing 10
            :icon-size 20))

;;define widget groups
(defwidget left []
	   (box 
	    :orientation "h" 
	    :space-evenly false  
            :halign "start"
            :valign "center"
	    :class "left_modules"
            :spacing 8
            (workspaces)
            (appnames)))
(defwidget center []
	   (box 
            :orientation "h" 
	    :space-evenly false  
            :halign "center"
	    :class "center_modules"
            (clock_module)))
(defwidget right []
	   (box
	    :orientation "h" 
	    :space-evenly false  
            :halign "end"
	    :class "right_modules"
            (label
             :class "left_sep"
             :text "|")
            (box
             :orientation "h"
             :space-evenly false
             :class "sysmodules"
             (disk)
             (cpu)
             (mem)
             (volume)
             ;; {right_modules.width > 1 ? null : (bright)}
             (bright)
             (bat))
            (label
             :class "right_sep"
             :text "|")
            (tray)
            (notif_button)))

(defwidget bar_layout []
  (centerbox 
    :class "bar_class"
    :orientation "h"
    (left)
    (center)
    (right)))

(defwindow bar0
    :monitor 0
    :exclusive true
    :geometry (geometry 
        :x "15px"
        :y "3px"
        :width "95%"
        :height "50px"
        :anchor "bottom center")
    :stacking "fg"
    :windowtype "dock"
    (bar_layout))

(defwindow bar1
    :monitor 0
    :exclusive true
    :geometry (geometry 
        :x "15px"
        :y "10px"
        :width "80%"
        :height "30px"
        :anchor "top center")
    :stacking "fg"
    :windowtype "dock"
    (bar_layout))

(defwindow bar2
    :monitor 2
    :exclusive false
    :geometry (geometry 
        :x "15px"
        :y "0px"
        :width "100%"
        :height "50px"
        :anchor "top center")
    :stacking "fg"
    :windowtype "dock"
    (bar_layout))



;; (defwidget audio []
;; 	(box 
;; 	    :class "audio-box"
;; 	    :orientation "v"
;; 	    :space-evenly "false"
;; 	    :vexpand "false"
;; 	    :hexpand "false"
;; 	    (box 
;; 	        :halign "v"
;; 	        :space-evenly "false"
;; 	        :hexpand "false"
;; 	        :vexpand "false"
;; 	        (box 
;; 	            :class "speaker_icon"
;; 	            :orientation "v")
;; 	        (box 
;; 	            :orientation "v"
;; 	            :halign "center"
;; 	            :vexpand "false"
;; 	            :hexpand "false"
;;                 (label
;;                     :class "speaker_text"
;;                     :text "speaker"
;;                     :valign "center"
;;                     :halign "left" )
;; 	            (box
;; 	                :class "speaker_bar"
;; 	                :halign "center"
;; 	                :vexpand "false"
;; 	                :hexpand "false"
;; 	                (scale
;; 	                  :value volume_percent
;; 			  :orientation "h"
;; 			  :onchange "amixer -D pulse sset Master {}%" 
;;                           :tooltip "volume on ${volume_percent}%"
;; 			  :max 100
;; 			  :min 0))))
;;         (label
;;             :text "_____________________________________"
;;             :class "audio_sep"
;;             :halign "center")
;; 	    (box 
;; 	        :halign "v"
;; 	        :space-evenly "false"
;; 	        :hexpand "false"
;; 	        :vexpand "false"
;; 	        (box 
;; 	            :class "mic_icon"
;; 	            :orientation "v")
;; 	            (box 
;; 	                :orientation "v"
;; 	                :halign "center"
;; 	                :vexpand "false"
;; 	                :hexpand "false"
;;                     (label
;;                         :class "mic_text"
;;                         :text "mic"
;;                         :valign "center"
;;                         :halign "left" )
;; 	                (box 
;; 	                    :class "mic_bar"
;; 	                    :halign "center"
;; 	                    :vexpand "false"
;; 	                    :hexpand "false"
;; 	                    (scale
;; 	                     :value mic_percent
;; 			     :orientation "h"
;;                              :tooltip "mic on ${mic_percent}%"
;; 			     :onchange "amixer -D pulse sset Capture {}%" 
;; 			     :max 100
;; 			     :min 0))))))
;; (defwindow audio_ctl
;;     :stacking "fg"
;;     :focusable "false"
;;     :screen 1
;;     :monitor 0
;; 	:geometry (geometry 
;;         :x "20" 
;; 		:y "7%" 
;;         :anchor "top right"
;; 		:width "280px" 
;; 		:height "60px")
;;     (audio))


;; ;;Define System widget  
;; (defwidget system []
;;   (box 
;;     :class "sys_win"
;;     :orientation "v"
;;     :space-evenly "false"
;;     :hexpand "false"
;;     :vexpand "false"
;;     :spacing 0
;;     (box 
;;         :class "sys_bat_box"
;;         :orientation "h"
;;         :space-evenly "false"
;;         (circular-progress
;;             :value {EWW_BATTERY["BAT1"].capacity}
;;             :class "sys_bat"
;;             :thickness 9
;;             (label :text ""
;;                 :class "sys_icon_bat"
;;                 :limit-width 2
;;                 :wrap false))
;;         (box 
;;             :orientation "v" 
;;             :space-evenly "false" 
;;             :spacing 0 
;;             :hexpand "false"
;;             :vexpand "false"
;;             (label
;;                 :text "battery"
;;                 :halign "start"
;;                 :class "sys_text_bat"
;;                 :limit-width 9
;;                 :wrap false)
;;             (label 
;;                 :text "${EWW_BATTERY["BAT1"].capacity}%"
;;                 :halign "start"
;;                 :class "sys_text_bat_sub"
;;                 :limit-width 22
;;                 :wrap false)
;;             (label :text "${EWW_BATTERY["BAT1"].status}"
;;                 :halign "start"
;;                 :class "sys_text_bat_sub"
;;                 :limit-width 22
;;                 :wrap false)))
;;     (label
;;         :text "____________________________________"
;;         :class "sys_sep"
;;         :halign "center")
;;     (box 
;;         :class "sys_mem_box"
;;         :orientation "h"
;;         :space-evenly "false"
;;         :halign "start"
;;         (circular-progress
;;             :value {EWW_CPU.avg}
;;             :class "sys_mem"
;;             :thickness 9
;;             (label
;;                 :text ""
;;                 :class "sys_icon_mem"
;;                 :limit-width 2
;;                 :wrap false
;;                 :angle 0.0))
;;         (box
;;             :orientation "v"
;;             :space-evenly "false"
;;             :spacing 0
;;             :hexpand "false"
;;             :vexpand "false"
;;             (label
;;                 :text "processor"
;;                 :halign "start"
;;                 :class "sys_text_mem"
;;                 :limit-width 9
;;                 :wrap false)
;;             (label
;;                 :text "${EWW_CPU.avg} | ${round((EWW_RAM.total_mem / 1024), 0)}mb "
;;                 :halign "start"
;;                 :class "sys_text_mem_sub"
;;                 :limit-width 22
;;                 :wrap false)
;;             (label :text "3.4 GHZ"
;;                 :halign "start"
;;                 :class "sys_text_mem_sub"
;;                 :limit-width 22
;;                 :wrap false)
;;             ))
;;     (label
;;         :text "____________________________________"
;;         :class "sys_sep"
;;         :halign "center")
;;     (box 
;;         :class "sys_mem_box"
;;         :orientation "h"
;;         :space-evenly "false"
;;         :halign "start"
;;         (circular-progress
;;             :value {EWW_RAM.used_mem_perc}
;;             :class "sys_mem"
;;             :thickness 9
;;             (label
;;                 :text ""
;;                 :class "sys_icon_mem"
;;                 :limit-width 2
;;                 :wrap false
;;                 :angle 0.0))
;;         (box
;;             :orientation "v"
;;             :space-evenly "false"
;;             :spacing 0
;;             :hexpand "false"
;;             :vexpand "false"
;;             (label
;;                 :text "memory"
;;                 :halign "start"
;;                 :class "sys_text_mem"
;;                 :limit-width 9
;;                 :wrap false)
;;             (label
;;                 :text "${round((EWW_RAM.available_mem / 1024), 0)} | ${round((EWW_RAM.total_mem / 1024), 0)}mb "
;;                 :halign "start"
;;                 :class "sys_text_mem_sub"
;;                 :limit-width 22
;;                 :wrap false)
;;             (label :text "${round((EWW_RAM.used_mem / 1024), 0)}mb free"
;;                 :halign "start"
;;                 :class "sys_text_mem_sub"
;;                 :limit-width 22
;;                 :wrap false)
;;             ))))
;; (defwindow system
;;     :stacking "fg"
;;     :focusable "false"
;;     :screen 1
;;     :monitor 0
;;     :geometry (geometry
;;                :x "20" 
;;                :y "0" 
;;                :anchor "bottom right"
;;                :width "290px" 
;;                :height "120px")
;;     (system))



;; ;;Calendar Widget
;; (defwidget cal []
;; 	(box 
;; 	    :class "cal"
;; 	    :orientation "v"
;; 	    (box
;; 	     :class "cal-in"
;; 	     (calendar
;; 	      :day calendar_day 
;; 	      :year calendar_year))))
;; (defwindow calendar
;;     :stacking "fg"
;;     :focusable "false"
;;     :screen 1
;;     :monitor 0
;;     :geometry (geometry
;;                :x "20" 
;;                :y "0" 
;;                :anchor "bottom right"
;; 	       :width "270px" 
;; 	       :height "60px")
;;     (cal))
