#!/bin/bash

ID="$1"

if [[ -z "$ID" ]]; then
    echo "Usage: $0 Number"
    exit 1
fi
# Command Helper: hyprctl clients -j | jq '.[] | select(.workspace.id == 1)'
# Icons: https://www.nerdfonts.com/cheat-sheet
declare -A MAP=(
    # Youtube
    ["Grayjay"]=""
    # Browser
    ["Ablaze Floorp"]=""
    ["floorp"]=""
    ["LibreWolf"]=""
    ["midori"]=""
    ["brave-browser"]=""
    ["chromium"]=""
    # E-Mail
    ["org.mozilla.Thunderbird"]="󰇮"
    # Terminals
    ["kitty"]=""
    ["Alacritty"]=""
    # Videoplayer
    ["VLC media player"]="󰕼"
    ["mpv"]=""
    ["ffplay"]=""
    # Video editing
    ["org.kde.kdenlive"]=""
    # Chats
    ["discord"]=""
    ["vesktop"]=""
    ["Signal"]="󰭹"
    ["xyz.chatboxapp.app"]=""
    # Coding
    ["Visual Studio Code"]=""
    ["VSCodium"]=""
    ["jetbrains-idea"]=""
    ["jetbrains-studio"]=""
    # Text Editor
    ["org.xfce.mousepad"]="󱩼"
    # Password
    ["org.keepassxc.KeePassXC"]=""
    ["Yubico Authenticator"]=""
    # Cloud
    ["com.nextcloud.desktopclient.nextcloud"]="󰅟"
    # App Store
    ["Octopi"]="󰒚"
    # Games
    ["steam"]=""
    ["org.prismlauncher.PrismLauncher"]="󰍳"
    ["Minecraft* 1.21.11"]="󰍳"
    ["game"]=""
    # Digital Arts
    ["Blender"]=""
    ["Krita"]=""
    # VM
    ["virt-manager"]=""
    ["VirtualBox Manager"]=""
    ["VirtualBox Machine"]=""
    ["remote-viewer"]=""
    # Audio
    ["qpwgraph"]="󱡫"
    ["Carla"]="󱡫"
    # Video
    ["com.obsproject.Studio"]="󰨜"
    ["hyprland-share-picker"]="󱎴"
    # Document Editor/Viewer
    ["libreoffice-startcenter"]=""
    ["libreoffice-base"]=""
    ["libreoffice-calc"]="󱎏"
    ["libreoffice-draw"]="󱎐"
    ["libreoffice-impress"]=""
    ["libreoffice-math"]=""
    ["libreoffice-writer"]="󱎒"
    ["com.github.flxzt.rnote"]="󱩼"
    ["Xreader"]="󱩼"
    ["com.github.xournalpp.xournalpp"]="󱩼"
    ["qpdfview.local.qpdfview"]="󱩼"
    # Hardware
    ["Mission Center"]=""
    ["QDirStat"]="󰋊"
    ["Disks"]="󰋊"
    # File Manager
    ["nemo"]="󰉋"
    ["org.gnome.Nautilus"]="󰉋"
    ["org.kde.dolphin"]="󰉋"
    ["xdg-desktop-portal-gtk"]="󰉋"
    # Settings
    ["nwg-look"]="" # gtk-settings
    ["qt5ct"]=""
    ["qt6ct"]=""
    ["sddm-conf"]=""
    ["blueman-manager"]="󰂯"
    ["nm-connection-editor"]="󱛆"
    # Calculator
    ["Calculator"]="󰪚"
    # VPN
    ["Proton VPN"]="󰳌"
    ["com.cisco.secureclient.gui"]="󰳌"
    # Torrent
    ["org.qbittorrent.qBittorrent"]="󰉍"
    # Hardware Design
    ["Xilinx Unified 2022.2 Installer"]=""
    ["Xilinx Information Center"]=""
    ["Xilinx Unified 2022.2 Installer"]=""
    ["vsim"]=""
    # BoxBuddy
    ["io.github.dvlv.boxbuddyrs"]="󰒋"
    # Stream
    ["Fladder"]="󰼁"
    ["crunchyroll"]=""
    ["netflix"]=""
    ["primevideo"]="󰯜"
    # Image Viewer
    ["viewnior"]=""
    # Image Editor
    ["gimp"]="󱇣"
)

# APPS=$(hyprctl clients -j | jq -r ".[] | select(.workspace.id == $ID) | .initialTitle")

mapfile -t APPS < <(
    hyprctl clients -j |
    jq -r ".[] | select(.workspace.id == $ID) | .initialTitle"
)

mapfile -t APPS_CLASS < <(
    hyprctl clients -j |
    jq -r ".[] | select(.workspace.id == $ID) | .initialClass"
)

for i in "${!APPS[@]}"; do
    if [[ -n "${MAP["${APPS[$i]}"]}" ]]; then
        APPS[$i]="${MAP["${APPS[$i]}"]}"
    elif [[ -n "${MAP["${APPS_CLASS[$i]}"]}" ]]; then
        APPS[$i]="${MAP["${APPS_CLASS[$i]}"]}"
    else
        APPS[$i]="${APPS_CLASS[$i]}|${APPS[$i]}"
    fi
done

# for app in "${APPS[@]}"; do
#   echo "$app"
# done

RESULT="$ID $(printf '%s  ' "${APPS[@]}")"
RESULT=${RESULT% }   # remove trailing space
echo "$RESULT"