#!/usr/bin/env bash

# Get current workspace ID
workspace_id=$(hyprctl activeworkspace -j | jq '.id')

# Get active window info
active_window=$(hyprctl activewindow -j)
is_floating=$(echo "$active_window" | jq '.floating')

echo "Current Workspace: $workspace_id"
echo "Active Window Floating: $is_floating"

if [ "$is_floating" == "true" ]; then
    echo "Switching to Tiled..."
    # Find a tiled window on the current workspace
    # We sort by focus history or just pick the first one
    target_addr=$(hyprctl clients -j | jq -r --argjson ws "$workspace_id" '[.[] | select(.workspace.id == $ws and .floating == false)] | sort_by(.focusHistoryID) | .[0].address')
else
    echo "Switching to Floating..."
    # Find a floating window on the current workspace
    target_addr=$(hyprctl clients -j | jq -r --argjson ws "$workspace_id" '[.[] | select(.workspace.id == $ws and .floating == true)] | sort_by(.focusHistoryID) | .[0].address')
fi

echo "Target Address: $target_addr"

if [ "$target_addr" != "null" ] && [ -n "$target_addr" ]; then
    hyprctl dispatch focuswindow address:$target_addr
else
    echo "No target window found."
fi
