;; 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")
(deflisten volume_percent :interval "0.5s"  "tail -F /tmp/vol")
(deflisten workspace "scripts/workspace")
(deflisten appname "scripts/appname")

(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 []
           (literal
            :content workspace))

;; (defwidget workspaces []
;;            (box
;;             :class "works"
;;             :orientation "h"
;;             :spacing 5
;;             :space-evenly "false"
;;             :valign "center"
;;             (button
;;              :onclick "hyprctl dispatch workspace 1"
;;              :onrightclick "hyprctl dispatch workspace 1 && $HOME/.config/hypr/themes/neon/scripts/default_app"
;;              :class "0$o1$f1" "")
;;             (button
;;              :onclick "hyprctl dispatch workspace 2"
;;              :onrightclick "hyprctl dispatch workspace 2 && $HOME/.config/hypr/themes/neon/scripts/default_app"
;;              :class "0$o2$f2" "")
;;             (button
;;              :onclick "hyprctl dispatch workspace 3"
;;              :onrightclick "hyprctl dispatch workspace 3 && $HOME/.config/hypr/themes/neon/scripts/default_app"
;;              :class "0$o3$f3" "")
;;             (button
;;              :onclick "hyprctl dispatch workspace 4"
;;              :onrightclick "hyprctl dispatch workspace 4 && $HOME/.config/hypr/themes/neon/scripts/default_app"
;;              :class "0$o4$f4" "")
;;             (button
;;              :onclick "hyprctl dispatch workspace 5"
;;              :onrightclick "hyprctl dispatch workspace 5 && $HOME/.config/hypr/themes/neon/scripts/default_app"
;;              :class "0$o5$f5" "")))

(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 bright []
;;            (eventbox
;;             :onhover "${EWW_CMD} update br_reveal=true" 
;;             :onhoverlost "${EWW_CMD} update br_reveal=false"
;;             :visible {HOST == "kaladin" ? "false" : "true"}
;;             (box 
;;              :class "module" 
;;              :space-evenly "false" 
;;              :orientation "h" 
;;              :spacing "3" 
;; 	     (scale
;;               :class "brightbar"
;; 	      :value brightness_percent
;; 	      :orientation "h"
;; 	      :tooltip "${brightness_percent}%"
;; 	      :max 100
;; 	      :min 0
;; 	      :onchange "brightnessctl s {}%" )
;;              (label
;;               :text ""
;;               :class "bright_icon"
;;               :tooltip "brightness"))))

;; (defwidget volume []
;;            (eventbox 
;;             :onhover "${EWW_CMD} update vol_reveal=true"
;; 	    :onhoverlost "${EWW_CMD} update vol_reveal=false"
;;             (box
;;              :class "module-2"
;;              :space-evenly "false"
;;              :orientation "h"
;;              :spacing "3" 
;; 	     (scale  
;; 	      :class "volbar"
;; 	      :value volume_percent
;;               :draw-value: true
;; 	      :orientation "h"
;; 	      :tooltip "${volume_percent}%"
;; 	      :max 150
;; 	      :min 0
;; 	      :onchange "pamixer --set-volume {}" )
;; 	     (button
;; 	      :onclick "scripts/pop audio"
;; 	      :class "volume_icon"
;; 	      "墳"))))

(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 "networkmanager_dmenu"
      WLAN_ICON))))

(defwidget bat []
	   (box
	    :class "bat_module"
	    :vexpand "false"
	    :hexpand "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 bright []
	   (box
	    :class "bright_module"
	    :vexpand "false"
	    :hexpand "false" 
            (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" 
            (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" 
            (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" 
            :vexpand "false" 
            :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 nextcloud_button []
  (box
    :class "notif_button"
    :space-evenly "false"
    :orientation "h"
    (button
     :onclick "exec nextcloud"
      (label
      :text ""))))

(defwidget sep []
  (box 
    :class "separator"
    :vexpand "false"
    :hexpand "false"
    (label
        :class "separ"
        :text "|")))

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



;;define widget groups
(defwidget left []
	   (box 
	    :orientation "h" 
	    :space-evenly false  
            :halign "start"
	    :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"
            (sep)
            (cpu)
            (mem)
            (volume)
            (bright)
            (bat)
            (sep)
            (wifi)
            (notif_button)
            (nextcloud_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 "100%"
        :height "60px"
        :anchor "bottom center")
    :stacking "fg"
    :windowtype "dock"
    (bar_layout))
(defwindow bar1
    :monitor 1
    :exclusive true
    :geometry (geometry 
        :x "15px"
        :y "10px"
        :width "80%"
        :height "30px"
        :anchor "bottom 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))