#!/usr/bin/env bash

source ~/.config/theme/workspace_colors

# 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) "')
}

# 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")'" }'
  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 - | 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
