#!/bin/bash

# https://github.com/Gustash/hyprland-interactive-screenshot
# `list_geometry` returns the geometry of the focused of visible windows. You can also get they title
# by setting a second argument to `with_description`. The geometry and the title are seperated by `\t`.
#
# Arguments:
#   $1: `focused` or `visible`
#   $2: `with_description` or nothing
#
# Output examples:
#   - with the `with_description` option:
#      12,43 100x200\tTermite
#   - without the `with_description` option:
#      12,43 100x200
function list_geometry () {
	[ "$2" = with_description ] && local append="\(.title)\t" || local append=
    if [ "$1" = focused ]; then
        hyprctl -j activewindow | jq -r '"'$append'\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"'
    else
        hyprctl -j clients | jq -r '.[] | select(.workspace.id | contains('$(hyprctl -j monitors | jq -r 'map(.activeWorkspace.id) | join(",")')')) | "'$append'\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"'
    fi
}

WINDOWS=`list_geometry visible with_description`
FOCUSED=`list_geometry focused`

CHOICE=`rofi -dmenu -theme ~/.config/rofi/screenshot/screenshot.rasi -p 'Screenshot' << EOF
fullscreen
region
focused
$WINDOWS
EOF`

SAVEDIR=${HYPRLAND_INTERACTIVE_SCREENSHOT_SAVEDIR}
[ -z "$SAVEDIR" ] && SAVEDIR=${SWAY_INTERACTIVE_SCREENSHOT_SAVEDIR:=~}
mkdir -p -- "$SAVEDIR"
FILENAME="$SAVEDIR/$(date +'%Y-%m-%d-%H%M%S_screenshot.png')"
EXPENDED_FILENAME="${FILENAME/#\~/$HOME}"

case $CHOICE in
    fullscreen)
        grim "$EXPENDED_FILENAME"
	;;
    region)
        grim -g "$(slurp)" "$EXPENDED_FILENAME"
	;;
    focused)
        grim -g "$FOCUSED" "$EXPENDED_FILENAME"
	;;
    '')
        notify-send "Screenshot" "Cancelled"
        exit 0
        ;;
    *)
    	GEOMETRY="`echo \"$CHOICE\" | cut -d$'\t' -f1`"
        grim -g "$GEOMETRY" "$EXPENDED_FILENAME"
esac

# If swappy is installed, prompt the user to edit the captured screenshot
if command -v swappy $>/dev/null
then
    EDIT_CHOICE=`echo -e "\n󰆢" | rofi -dmenu -theme ~/.config/rofi/powermenu/confirm.rasi -p 'Edit' -mesg 'Edit?'`

    case $EDIT_CHOICE in
        )
            swappy -f "$EXPENDED_FILENAME" -o "$EXPENDED_FILENAME"
            ;;
        󰆢)
            ;;
        '')
            ;;
    esac
fi

wl-copy < "$EXPENDED_FILENAME"
notify-send "Screenshot" "File saved as <i>'$FILENAME'</i> and copied to the clipboard." -i "$EXPENDED_FILENAME"
