;;Input values


(deflisten monitors
    :initial "[]"
    `scripts/monitors.sh`)

(defvar brightnessValue 96000)

(defpoll activeNetworks
    :interval "2s"
    `nmcli connection show --active | awk 'NR>1 && $3!="loopback" { print $1 }'`)

(deflisten workspaces
    :initial "[]"
    `scripts/workspaces.sh`)

(defpoll activeWorkspace
    :interval "2s"
    `hyprctl activeworkspace | awk 'NR==1{print $3}'`)

(deflisten bluetooth
    :initial "[]"
    `scripts/bluetooth.sh`)

(defvar volumeValue 100)


;; Widget templates


(defwidget icon [widgetclass ?hover percentage image]
    (box
    :class "${widgetclass}"
        :space-evenly false
        (overlay
            :tooltip "${hover}"
            (circular-progress
                :width 20
                :value "${percentage}"
                :start-at 75
                :thickness 2
                :clockwise false)
            (transform
                :scale-x "50%"
                :scale-y "50%"
                :halign "center"
                :valign "center"
                :translate-x "16"
                :translate-y "16"
                (image
                    :path "${image}"
                    :image-width "32"
                    :image-height "32")))))


;; Widgets


(defwidget workspaces []
    (box
        (for workspace in workspaces
            (label
                :width "15"
                :class {workspace.active == 1 ? "activeWorkspace" :""}
                :visible {workspace.occupied == 1 ? "true" : "false"}
                :text "${workspace.num}"))))

(defwidget time []
    (eventbox
        :onhover "eww open calendar"
        :onhoverlost "eww close calendar"
        {formattime(EWW_TIME, "%H:%M:%S")}))

(defwidget cpu []
    (icon
        :widgetclass "cpu"
        :hover "${round(EWW_CPU.avg, 1)}%"
        :percentage "${round(EWW_CPU.avg, 1)}"
        :image "images/cpu.png"))

(defwidget ram []
    (icon
        :widgetclass "ram"
        :hover "${round(EWW_RAM.used_mem_perc, 1)}%"
        :percentage "${round(EWW_RAM.used_mem_perc, 1)}"
        :image "images/ram.png"))

(defwidget volume []
    (icon
        :widgetclass "volume"
        :hover "${volumeValue}%"
        :percentage "${volumeValue}"
        :image "images/volume.png"))

(defwidget brightness []
    (icon
        :widgetclass "brightness"
        :hover "${brightnessValue / 960}%"
        :percentage "${brightnessValue / 960}"
        :image "images/brightness.png"))

(defwidget network []
    (icon
        :widgetclass "network"
        :hover "${activeNetworks}"
        :percentage "0"
        :image "images/wifi.png"))

;;(defwidget bluetooth []
;;    (icon
;;        :widgetclass "bluetooth"
;;        :hover "${bluetoothDevices}"
;;        :percentage "${bluetoothBattery}"
;;        :image "images/bluetooth.png"))

(defwidget battery []
    (icon
        :widgetclass {EWW_BATTERY.BAT1.status == "Charging" ? "battery" : EWW_BATTERY.total_avg > 25 ? "battery" : "batteryLow"}
        :hover "${round(EWW_BATTERY.total_avg, 1)}%"
        :percentage "${round(EWW_BATTERY.total_avg, 1)}"
        :image {EWW_BATTERY.BAT1.status == "Charging" ? "images/charging.png" : EWW_BATTERY.total_avg > 25 ? "images/battery.png" : "images/batteryLow.png"}))

(defwidget power []
    (icon
        :widgetclass "power"
        :hover "placeholder"
        :percentage "0"
        :image "images/power.png"))

(defwidget space []
    (box
        :width 5))

(defwidget seperator []
    (box
        :width 20
        (label
            :text "|"
            )))

(defwidget cal []
    (calendar
        :day {formattime(EWW_TIME, "%d")}
        :month {formattime(EWW_TIME, "%m")}
        :year {formattime(EWW_TIME, "%y")}))

(defwidget winPower []
    (box
        :orientation "v"
        (box
            :halign "end"
            (power))))


;; Widget blocks


(defwidget start []
    (box
        :orientation "h"
        :space-evenly false
        :halign "start"
        (space)
        (workspaces)))

(defwidget center []
    (box
        :orientation "h"
        :space-evenly false
        :halign "center"
        (time)))

(defwidget end []
    (box
        :orientation "h"
        :space-evenly false
        :halign "end"
        (cpu)
        (space)
        (ram)
        (seperator)
        (volume)
        (space)
        (brightness)
        (seperator)
        (network)
        (space)
;;        (bluetooth)
        (seperator)
        (battery)
        (space)
        (power)))

(defwidget bar []
    (centerbox    :orientation "h"
        (start)
        (center)
        (end)))


;; Window definition


(defwindow bar
    :monitor 0
    :class "bar"
    :geometry (geometry
        :x "0%"
        :y "5px"
        :width "1279px"
        :height "3%"
        :anchor "top center")
    :stacking "fg"
    :exclusive true
    (bar))

(defwindow calendar
    :monitor 0
    :class "calendar"
    :geometry
        (geometry
        :x "0%"
        :y "5px"
        :width "0%"
        :height "0%"
        :anchor "top center")
    :stacking "overlay"
    (cal))

(defwindow power
    :monitor 0
    :class "bars"
    :geometry
        (geometry
        :x "5px"
        :y "5px"
        :width "2%"
        :height "3%"
        :anchor "top right")
    :stacking "fg"
    (winPower))