(defwidget bar-1 [screen]
	(box :class "main-cont"
		 :orientation "h"
		(box :class "segment-start"
			:halign "start"
			:valign "center"
			(start-control :screen screen)
		)
		(box :class "segment-center"
			:halign "center"
			:valign "center"
			:space-evenly true
			(mid-control)
		)
		(box :class "segment-end"
			:halign "end"
			:valign "center"
			(end-control)
		)
	)
)

(defwidget bar-2 [screen]
	(box :class "main-cont"
		 :orientation "h"
		(box :class "segment-start"
			:halign "start"
			:valign "center"
			(start-control :screen screen)
		)
		(box :class "segment-center"
			:halign "center"
			:valign "center"
			:space-evenly true
			(mid-control)
		)
		(box :class "segment-end"
			:halign "end"
			:valign "center"
		)
	)
)

;; Powermenu widget
(defwidget power []
  (eventbox  ;;:onhover "${EWW_CMD} update power=true"
             :onhoverlost "${EWW_CMD} update power=false"
    (box     :orientation "h"
             :space-evenly "false"
             :vexpand "false"
             :class "powermenu ${power ? 'pm-active' : ''}"
      ;; Open menu button
      (revealer  :transition "slideleft"
                 :reveal power
                 :duration "450ms"
        (box     :orientation "h"
                 :class "pm-inner"
                 :space-evenly true
                 :valign "center"
                 :spacing 8

          ;; Lock screen
          (button    :class "icon lock"
                     :tooltip "Lock screen"
                     :onclick "betterlockscreen -l"  "󰌾")

          ;; Quit WM (goto login manager)
          (button    :class "icon logout"
                     :tooltip "Logout"
                     :onclick "loginctl kill-session $XDG_SESSION_ID" "󰍃")

          ;; Reboot
          (button    :class "icon reboot"
                     :tooltip "Reboot"
                     :onclick "reboot"  "󰑓")

          ;; Poweroff
          (button    :class "icon poweroff"
                     :tooltip "Poweroff"
                     :onclick "poweroff"  "󰐥")

        ))
	  (button    :class "icon logo ${power ? 'logo-seperator' : ''}"
				   :tooltip "Open menu"
				   :onclick "${EWW_CMD} update power=true"  "Λ")
        )))
(defvar power false)

;; Control widget (container)
(defwidget start-control [screen]
  (box :class "control" :spacing 16 :space-evenly false :orientation "h"
	(workspaces :wsp workspaces-data :screen screen)
    )
  )
(defwidget mid-control []
  (box :class "control" :spacing 16 :space-evenly false :orientation "h"
    (time)
    )
  )
(defwidget end-control []
  (box :class "control" :spacing 16 :space-evenly false :orientation "h"
    (music)
    (volume)
    (power)
    )
  )

;; Clock Widget
(defwidget time []
  (box  :orientation "h"
      :class "time"
	  :space-evenly "false"
      :halign "center"
  (button  :onclick "scripts/popup calendar"
      :class "day" full-time)
  ))
(defpoll full-time :interval "1s"  "date +'%d %B (%m), %Y - %A %H:%M:%S'")

;; Volume widget
(defwidget volume []
  (eventbox :onhover "${EWW_CMD} update volume=true"
			  :onhoverlost "${EWW_CMD} update volume=false"
	  (box :class "" :space-evenly "false" :orientation "h" :spacing "4" 
		(revealer :transition "slideleft"
				  :reveal volume
				  :duration "350ms"
			(scale    :class "volbar"
					  :value current-volume
					  :flipped true
					  :orientation "h"
					  :max 100
					  :min 0
					  :onchange "amixer -D pulse sset Master {}%" )
			)
		(button   :onclick "amixer -D pulse set Master 1+ toggle"
				  :tooltip "Volume on/off"
				  :class "icon vol" {muted-volume != "on" ? "󰝟" : "󰕾"}
				  )
	   )
  )
)
(defvar volume false)
(defpoll current-volume :interval "1ms" "amixer -D pulse sget Master | grep 'Left:' | awk -F'[][]' '{ print $2 }' | tr -d '%'")
(defpoll muted-volume :interval "1ms" "amixer -D pulse sget Master | grep 'Left:' | awk -F'[][]' '{ print $4 }' | tr -d '%'")

;; Battery Widgets ;;
(defwidget bat []
  (box  :orientation "v"
      :space-evenly "false"
    (label  :class "icon bat"
        :halign "center"
        :text battery
        :tooltip "Battery: ${battery-cappacity}%")))
(defpoll battery      :interval "1s"  "scripts/battery icon")
(defpoll battery-cappacity  :interval "1s"  "scripts/battery percent")

;; Wifi Widgets ;;
(defwidget wifi []
  (box       :orientation "v"
             :halign "center"
             :tooltip "SSID: ${wifi-name}"
    (button  :class "icon wifi-icon" wifi-icon
             )))
(defpoll wifi-icon :interval "1s" "scripts/wifi icon")
(defpoll wifi-name :interval "1s" "scripts/wifi name")

;; Workspaces container
(defwidget workspaces [wsp]
  (box :space-evenly true :spacing 16 :orientation "h" :class "workspaces"
   (for ws in wsp
        (workspace :data ws)
    )))

;; Workspace widget
(defwidget workspace [data]
   (button :onclick "wmctrl -s ${data['index']}"
           :class {data["iscurrent"] == true ? "active" : "inactive"}
           ;; "${data['name']}"
		   :tooltip "Switch to workspace ${data['name']}"
		   {data["iscurrent"] == true ? "" : ""}
           ))
(deflisten workspaces-data "scripts/workspace.py")

;; Music widget
(defwidget music []
  (revealer     :reveal {music-active == "true"}
    (eventbox   :onhover "${EWW_CMD} update music=true"
                :onhoverlost "${EWW_CMD} update music=false"
        (box    :orientation "h"
                :space-evenly "false"
                :class "musicmenu ${music ? 'mm-active' : ''}"
        (revealer   :transition "slideleft"
                    :reveal music
                    :flipped true
                    :duration "450ms"
            (box    :orientation "h"
                    :class "mm-inner"
                    :space-evenly true
                    :valign "center"
                    :spacing 8

                ;; Pause
                (button    :class "icon prev"
                            :tooltip "Pause/play"
                            :onclick "playerctl previous" "")
                ;; Pause
                (button    :class "icon pause"
                            :tooltip "Pause/play"
                            :onclick "playerctl play-pause" {music-status == "Playing" ? "󰏤" : "󰐊"})
                ;; Next
                (button    :class "icon next"
                            :tooltip "Pause/play"
                            :onclick "playerctl next" "")
            ))
        ;; Open menu button
        (button     :class "icon"
                    :tooltip "Music"
                    :onclick "${EWW_CMD} update music=true"  "󰎈")

            )
    )))

(defpoll music-active         :interval "200ms"  "scripts/musicstatus")
(defpoll music-status         :interval "200ms"  "playerctl status")
(defpoll artUrl               :interval "200ms"  "playerctl metadata artUrl")
(defpoll title                :interval "200ms"  "playerctl metadata title")
(defpoll artist               :interval "200ms"  "playerctl metadata artist")
(defpoll songtime             :interval "500ms"  "scripts/getSongDuration")

(defvar music false)

;; WINDOWS
(defwindow bar-1
  :monitor 0
  :windowtype "dock"
  :geometry (geometry :x "0%"
                      :y "0%"
                      :width "100%"
                      :height "32px"
                      :anchor "top center")
  :reserve (struts :side "top" :distance "32px")
  :hexpand "false"
  :vexpand "false"
  (bar-1 :screen 1))

(defwindow bar-2
  :monitor 1
  :windowtype "dock"
  :geometry (geometry :x "0%"
                      :y "0%"
                      :width "100%"
                      :height "32px"
                      :anchor "top center")
  :reserve (struts :side "top" :distance "32px")
  :hexpand "false"
  :vexpand "false"
  (bar-2 :screen 2))

