#!/bin/bash

PIDs=()
classes=()
clientOnWorkspace=()
images=()
numberOnWorkspace=()
area=()
i=0

while read -r p w x y c; do
    if [[ $c ]]; then

        PIDs[$i]="$p"
        classes[$p]="$c"
        clientOnWorkspace[$p]="$w"
        ((numberOnWorkspace[w]+=1))
        area[$p]=$((x * y))



        desktop=$(grep -l "StartupWMClass.*$c$" /usr/share/applications/*.desktop)
        echo $c
        if [ -z $desktop ]; then
            desktop=$(grep -Pl "Name\s*=\s*$c$" /usr/share/applications/*.desktop)
        fi
        if [ -z $desktop ]; then
            desktop=$(ls /usr/share/applications | grep -l /usr/share/applications/${c}.desktop)
        fi
        if [ -z $desktop ]; then
            process=$(ps -p ${p} -o comm=)
            desktop=$(grep -Pl "$process" /usr/share/applications/*.desktop)
        fi
        if [ -z $desktop ]; then
            desktop=$(grep -Pli "$c" /usr/share/applications/*.desktop)
        fi
        if [ -z $desktop ]; then
            continue
        fi

        images[$p]=$(awk -F'[[:space:]]*=[[:space:]]*' '/Icon[[:space:]]*=/{print $2}' $desktop)
        ls /usr/share/icons/ePapirus-Dark/128x128/apps/${images[$p]}.svg
        ((i=$i+1))





    fi
done < <(hyprctl clients -j | jq -r '.[]|"\(.pid) \(.workspace.id) \(.size[0]) \(.size[1]) \(.initialClass)"')

for ((i=0; i<${#PIDs[@]}; i++)); do
    PID=${PIDs[$i]}
    clientInfos[PID]='{"onWorkspace":"'${clientOnWorkspace[$PID]}'","pid":"'${PID}'","class":"'${classes[$PID]}'","mainOn":"''","icon":"'${images[$PID]}'"}'
    echo ${clientInfos[$PID]}
done


#for window in ${!windows[@]}; do
#  echo -n "${window} "
#  echo $(ps -p ${windows[$window]} -o comm=)
#done

#test=$(ps -p ${windows[2]} -o comm=)
#$(feh /usr/share/icons/Papirus-Dark/128x128/apps/${test}.svg)