#!/bin/bash

ALL_NODES='recurse(.nodes[]?, .floating_nodes[]?) | select(.pid and .visible)'
WINDOW_NAME='(.app_id // .window_properties.class)'
ID_PREFIX='"\(.pid):"'
WINDOW_PROTOCOL='(if .shell == "xwayland" then "X11" else "wayland" end)'
WINDOW_GEOMETRY='(.rect | "\(.x),\(.y) \(.width)x\(.height) ")'
WINDOW_PID='(.id | tostring)'
CLEAR_OBJ="del(.focus, .pid, .border, .current_border_width, .window_rect, .orientation, .layout, .percent, .deco_rect, .geometry, .window, .urgent, .marks, .sticky, .fullscreen_mode, .nodes, .floating_nodes, .shell, .max_render_time, .visible, .idle_inhibitors, .inhibit_idle)"

# Output format, e.g. "1234 - 12:firefox (wayland)" or "5678 - 17:discord (X11)"
FILT="$ALL_NODES | $WINDOW_GEOMETRY + $WINDOW_PID"

function getprop() {
	typeset -A views
	local selected

	while read POS GEOM INFO; do
		views["$POS $GEOM"]="$INFO"
	done


	selected="$(printf "%s\n" "${!views[@]}" | slurp)"
	if [[ -n "$selected" ]]; then
   window_id=${views[${selected}]};
   echo "SelectedId $window_id"
   SELECT_ID=$(echo ".. | (.nodes? // empty)[] | select(.id == $window_id) | $CLEAR_OBJ ")
   selected="$(swaymsg -t get_tree | jq -r "$ALL_NODES" | jq -c ". | select(.id==$window_id)" | jq)"
   echo "Selected: $selected"
   notify-send "Debug:" "$(echo $selected | jq -c "$CLEAR_OBJ" | jq)";
	fi
}

swaymsg -t get_tree | jq -r "$FILT" | getprop
