#!/bin/bash

# get initial focused workspace
focusedws=$(hyprctl -j monitors | jq -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

# handle workspace create/destroy
workspace_event() {
  if (( $1 <= 10 )); then
    o[$1]=$2
    while read -r k v; do workspaces[$k]="$v"; done < <(hyprctl -j workspaces | jq -r '.[]|"\(.id) \(.monitor)"')
  fi
  if [ "$2" == "0" ]; then
    unset "workspaces[$1]"
  fi
}

# handle monitor (dis)connects
monitor_event() {
  while read -r k v; do monitormap["$k"]=$v; done < <(hyprctl -j monitors | jq -r '.[]|"\(.name) \(.id) "')
}

# generate the json for eww
generate() {

  activeWorkspace=$(hyprctl -j monitors | jq -r '.[] | select(.focused == true) | .activeWorkspace.id')
  
  echo -n '['

  for i in {1..10}; do
    active=0
    if [[ $activeWorkspace == $i ]]; then
      active=1
    fi
    echo -n ''$([ $i -eq 1 ] || echo ,)'{"num":"'$i'","occupied":"'${o[$i]}'","active":"'$active'"}'
    # echo -n ''$([ $i -eq 1 ] || echo ,) '{ "number": "'"$i"'", "activity": "'"$(status_activity $i)"'", "color": "'$(status "$i")'" }'
  done

  # echo -n ',{"num":"'$focusedws'","clr":"'$(status "$focusedws")'"}'

  echo ']'
}

# setup

# add monitors
monitor_event

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

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

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

socat -u UNIX-CONNECT:/tmp/hypr/"$HYPRLAND_INSTANCE_SIGNATURE"/.socket2.sock - | while read -r line; do
  # echo "${#workspaces[@]} ${#o[@]}"
  # echo $line
  case ${line%>>*} in
    "focusedmon")
      focusedws=${line#*,}
      generate
      ;;
    "createworkspace")
      # workspace_event "${line#*>>}" 1
      o=([1]=0 [2]=0 [3]=0 [4]=0 [5]=0 [6]=0 [7]=0 [8]=0 [9]=0 [10]=0)
      workspaces=()
      # add workspaces
      while read -r k v; do workspaces[$k]="$v"; done < <(hyprctl -j workspaces | jq -r '.[]|"\(.id) \(.monitor)"')
      # check occupied workspaces
      for num in "${!workspaces[@]}"; do
        o[$num]=1
      done
      # focusedws=${line#*>>}
      generate
      ;;
    "movewindow")
      generate
      ;;
    "destroyworkspace")
      # workspace_event "${line#*>>}" 0
      o=([1]=0 [2]=0 [3]=0 [4]=0 [5]=0 [6]=0 [7]=0 [8]=0 [9]=0 [10]=0)
      workspaces=()
      # add workspaces
      while read -r k v; do workspaces[$k]="$v"; done < <(hyprctl -j workspaces | jq -r '.[]|"\(.id) \(.monitor)"')
      # check occupied workspaces
      for num in "${!workspaces[@]}"; do
        o[$num]=1
      done
      generate
      ;;
    "monitor"*)
      monitor_event
      generate
      ;;
    "workspace")
      
      generate
      ;;
  esac
  # echo $line
  # generate
done

