#!/usr/bin/env bash

function array_sort() {
  local -n ref=$1
  IFS=$'\n' ref=($(sort <<<"${ref[*]}"))
  unset IFS
}
#initial check for occupied workspaces
set_mon="$1"
occupied_workspace=("$(hyprctl workspaces | grep " $set_mon" | awk '{print $3}')")
active_workspace="$(hyprctl monitors | grep " $set_mon" -A 3 | awk 'NR==4{print $3}')"
declare active_mon
declare last_created

if [[ ! "${occupied_workspace[*]}" =~ ${active_workspace} ]]; then
  occupied_workspace+=("$active_workspace")
fi
array_sort occupied_workspace

workspaces() {
  if [[ ${1:0:9} == "workspace" ]]; then #set focused workspace
    active_workspace=${1:11}
  elif [[ ${1:0:15} == "createworkspace" ]] && [[ $active_mon == $set_mon ]]; then #add workspace to Occupied workspace
    active_workspace=${1:17}
    occupied_workspace+=("$active_workspace")
    last_created=$active_workspace
    array_sort occupied_workspace
    if [[ ! "${occupied_workspace[*]}" =~ ${active_workspace} ]]; then
      occupied_workspace+=("$active_workspace")
    fi
  elif [[ ${1:0:16} == "destroyworkspace" ]] && [[ $active_mon == $set_mon ]]; then
    # removing workspace from occupied
    occupied_workspace=("${occupied_workspace[@]/${1:18}/}")
  elif [[ ${1:0:10} == "focusedmon" ]]; then
    string=${1:12}
    monitor_name=${string/,*/}
    active_workspace=${string##*,}
    active_mon="$monitor_name"
    if [[ $active_mon == $set_mon ]]; then
      # for safety measures
      occupied_workspace=("$(hyprctl workspaces | grep " $set_mon" | awk '{print $3}')")
      active_workspace="$(hyprctl monitors | grep " $set_mon" -A 3 | awk 'NR==4{print $3}')"
      array_sort occupied_workspace
      if [[ ! "${occupied_workspace[*]}" =~ ${active_workspace} ]]; then
        occupied_workspace+=("$active_workspace")
      fi
    fi
    # remove element
    if [[ -n $last_created ]] && [[ $active_mon == "$set_mon" ]]; then
      occupied_workspace=("${occupied_workspace[@]/$last_created/}")
    fi
    unset -v last_created
  elif [[ ${1:0:13} == "moveworkspace" ]]; then
    string=${1:15}
    monitor_name=${string##*,}
    active_workspace=${string/,*/}
    if [[ $monitor_name == $set_mon ]]; then
      if [[ ! "${occupied_workspace[*]}" =~ ${active_workspace} ]]; then
        occupied_workspace+=("$active_workspace")
      fi
      array_sort occupied_workspace
    else
      occupied_workspace=("${occupied_workspace[@]/$active_workspace/}")
    fi
  fi
}
module() {
  #output eww widget
  echo -n "(eventbox :onscroll \"echo {} | sed -e 's/up/-1/g' -e 's/down/+1/g' | xargs hyprctl dispatch workspace\""
  echo -n "  (box	:class \"works\"	:orientation \"h\" :spacing 2 :space-evenly \"false\""
  for i in ${occupied_workspace[@]}; do
    if [ $i = $active_workspace ]; then
      echo -n "    (button :onclick \"hyprctl dispatch workspace $i\" :class \"0act\" \"$i\") "
    else
      echo -n "    (button :onclick \"hyprctl dispatch workspace $i\" :class \"0norm\" \"$i\") "
    fi
  done
  echo -n "  )"
  echo ")"
}

# initial
active_mon="$set_mon"
module
active_mon="$(hyprctl monitors | grep -B 7 "focused: yes" | awk 'NR==1{print $2;}')"

socat -u UNIX-CONNECT:/tmp/hypr/"$HYPRLAND_INSTANCE_SIGNATURE"/.socket2.sock - | while read -r event; do
  workspaces "$event"
  module
done