#!/usr/bin/env bash

# define colors
#        red       peach     green     blue
colors=("#f38ba8" "#fab387" "#a6e3a1" "#89b4fa")
#        pink      yellow    teal      lavender
dimmed=("#f5c2e7" "#f9e2af" "#94e2d5" "#b4befe")
empty="#313244"

# get initial focused workspace
focusedws=$(hyprctl -j monitors | jaq -r '.[] | select(.focused == true) | .activeWorkspace.id')

declare -A o=([1]=0 [2]=0 [3]=0 [4]=0 [5]=0 [6]=0 [7]=0 [8]=0 [9]=0 [10]=0)
declare -A monitormap
declare -A workspaces

# set color for each workspace
status() {
  if [ "${o[$1]}" -eq 1 ]; then
    mon=${monitormap[${workspaces[$1]}]}

    if [ "$focusedws" -eq "$1" ]; then
      echo -n "${colors[$mon]}"
    else
      echo -n "${dimmed[$mon]}"
    fi
  else
    echo -n "$empty"
  fi
}

# handle workspace create/destroy
workspace_event() {
  o[$1]=$2
  while read -r k v; do workspaces[$k]="$v"; done < <(hyprctl -j workspaces | gojq -r '.[]|"\(.id) \(.monitor)"')
}
# handle monitor (dis)connects
monitor_event() {
  while read -r k v; do monitormap["$k"]=$v; done < <(hyprctl -j monitors | gojq -r '.[]|"\(.name) \(.id) "')
}

# get all apps titles in a workspace
applist() {
  ws="$1"

  apps=$(hyprctl -j clients | jaq -jr '.[] | select(.workspace.id == '"$ws"') | .title + "\\n"')
  echo -En "${apps%"\n"}"
}

# generate the json for eww
generate() {
  echo -n '['

  for i in {1..10}; do
    echo -n ''"$([ "$i" -eq 1 ] || echo ,)" '{ "number": "'"$i"'", "color": "'"$(status "$i")"'" }' #, "tooltip": "'$(applist "$i")'" }'
  done

  echo ']'
}

# setup

# add monitors
monitor_event

# add workspaces
while read -r k v; do workspaces[$k]="$v"; done < <(hyprctl -j workspaces | gojq -r '.[]|"\(.id) \(.monitor)"')

# check occupied workspaces
for num in "${!workspaces[@]}"; do
  o[$num]=1
done
# generate initial widget
generate

# main loop
socat -u UNIX-CONNECT:/tmp/hypr/"$HYPRLAND_INSTANCE_SIGNATURE"/.socket2.sock - | rg --line-buffered "workspace|mon(itor)?" | while read -r line; do
  case ${line%>>*} in
    "workspace")
      focusedws=${line#*>>}
      ;;
    "focusedmon")
      focusedws=${line#*,}
      ;;
    "createworkspace")
      workspace_event "${line#*>>}" 1
      ;;
    "destroyworkspace")
      workspace_event "${line#*>>}" 0
      ;;
    "monitor"*)
      monitor_event
      ;;
  esac
  generate
done