(defwidget bar [screen]
  (box :orientation "v"
       :vexpand "false"
       :hexpand "false"
    (box :class "segment-start"
         :valign "start"
         :halign "center"
         :space-evenly false
         :orientation "v"
         :space-evenly false
      (power :screen screen)
    )
    (box :class "segment-mid"
         :valign "center"
         :halign "center"
         :orientation "v"
         :space-evenly false
      (workspaces :wsp workspaces-data :screen screen)
    )
    (box :class "segment-end"
         :valign "end"
         :halign "center"
         :orientation "v"
         :space-evenly true
      (control :screen screen)
    )
  )
)

(defwidget bar-2 [screen]
  (box :orientation "v"
       :vexpand "false"
       :hexpand "false"
    (box :class "segment-start"
         :valign "start"
         :halign "center"
         :space-evenly false
         :orientation "v"
         :space-evenly false
      ;; (power :screen screen)
    )
    (box :class "segment-mid"
         :valign "center"
         :halign "center"
         :orientation "v"
         :space-evenly false
      (workspaces :wsp workspaces-data :screen screen)
    )
    (box :class "segment-end"
         :valign "end"
         :halign "center"
         :orientation "v"
         :space-evenly true
        (control-2 :screen screen)
    )
  )
)

;; Powermenu widget
(defwidget power []
  (eventbox  ;;:onhover "${EWW_CMD} update power=true"
             :onhoverlost "${EWW_CMD} update power=false"
    (box     :orientation "v"
             :space-evenly "false"
             :vexpand "false"
             :class "powermenu ${power ? 'pm-active' : ''}"
      ;; Open menu button
      (button    :class "icon logo ${power ? 'logo-seperator' : ''}"
                 :tooltip "Open menu"
                 :onclick "${EWW_CMD} update power=true"  "Λ")
      (revealer  :transition "slidedown"
                 :reveal power
                 :duration "450ms"
        (box     :orientation "v"
                 :class "pm-inner"
                 :space-evenly true
                 :halign "middle"
                 :spacing 6

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

          ;; Quit WM (goto login manager)
          (button    :class "icon quit"
                     :tooltip "Logout"
                     :onclick "killall xmonad-x86_64-linux" "")

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

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

        ))
        )))
(defvar power false)

;; Control widget (container)
(defwidget control []
  (box :class "control" :spacing 12 :space-evenly false :orientation "v"
    ;; (music)
    (volume)
    (wifi)
    (bat)
    (time)
    )
  )

(defwidget control-2 []
  (box :class "control" :spacing 12 :space-evenly false :orientation "v"
    ;; (wifi)
    ;; (bat)
    ;; (music)
    ;; (volume)
    (time)
    )
  )

;; Clock Widget
(defwidget time []
  (box  :orientation "v"
      :class "time"
      :valign "end"
  (button  :onclick "scripts/popup calendar"
      :class "hour" hour)
  (button  :onclick "scripts/popup calendar"
      :class "min"  min)
  (button  :onclick "scripts/popup calendar"
      :class "sec"  sec)
  ))
(defpoll hour  :interval "1s"  "date '+%H'")
(defpoll min  :interval "1s"  "date '+%M'")
(defpoll sec :interval "1s"  "date '+%S'")

;; Volume widget
(defwidget volume []
  (eventbox     :onhover "${EWW_CMD} update volume=true"
                :onhoverlost "${EWW_CMD} update volume=false"
    (box        :orientation "v"
                :space-evenly "false"
                :spacing 2
      (revealer :transition "slideup"
                :reveal volume
                :duration "450ms"
        (scale  :class "volbar"
                :value {current-volume != "" ? current-volume : "0"}
                :orientation "v"
                :flipped true
                :tooltip "Volume: ${current-volume}%"
                :max 101
                :min 0
                :onchange "amixer -D pulse sset Master {}%"
          )
        )
      (button   :onclick "amixer -D pulse set Master 1+ toggle"
                :class "icon volume + ${muted-volume != 'on' ? 'red' : ''}"
                :tooltip "Volume: ${current-volume}%"
                {muted-volume != "on" ? "ﱝ" : "墳"}
      )
  )))
(defvar volume false)
(defpoll current-volume :interval "180ms" "amixer -D pulse sget Master | grep 'Left:' | awk -F'[][]' '{ print $2 }' | tr -d '%'")
(defpoll muted-volume :interval "180ms" "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 "middle"
        :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 "middle"
                :tooltip "SSID: ${wifi-name}"
        (label :class "icon wifi"
                :halign "middle"
                :text "﬉"
                )))
(defpoll wifi-status :interval "1s" "scripts/wifi status")
(defpoll wifi-name :interval "1s" "scripts/wifi name")

;; Workspaces container
(defwidget workspaces [wsp]
  (box :space-evenly true :spacing 4 :orientation "v" :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']}"
           ))
(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"
                 ;; :onclick "wget ${artUrl} -O /tmp/cover.jpg; sleep 1; notify-send -i /tmp/cover.jpg '${title} - ${artist}' '${songtime}'"
        (box     :orientation "v"
                :space-evenly "false"
                :vexpand "false"
                :class "musicmenu ${music ? 'mm-active' : ''}"
        (revealer  :transition "slideup"
                    :reveal music
                    :flipped true
                    :duration "450ms"
            (box     :orientation "v"
                    :class "mm-inner"
                    :space-evenly true
                    :halign "middle"
                    :spacing 4

                ;; Next
                (button    :class "icon next"
                            :tooltip "Pause/play"
                            :onclick "playerctl next" "")
                ;; Pause
                (button    :class "icon pause"
                            :tooltip "Pause/play"
                            :onclick "playerctl play-pause" {music-status == "Playing" ? "" : ""})

                ;; Pause
                (button    :class "icon prev"
                            :tooltip "Pause/play"
                            :onclick "playerctl previous" "")

            ))
        ;; Open menu button
        (button     :class "icon"
                    :tooltip "${title} - ${artist}"
                    :onclick "${EWW_CMD} update music=true"  "")

            )
    )))

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

(defvar music false)

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

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

(defwindow time-thing
    :monitor 0
    :geometry (geometry :x "0%"
                        :y "0%"
                        :width "182px"
                        :height "38px"
                        :anchor "right top"
                        )
    :reserve (struts :side "right" :distance "0px" :orientation "h")
    (time))

