#!/bin/bash

FILEDIR=~/Downloads
OPTION=()

values=$(zenity \
  --title=wayland-screenshot \
  --text="Option" \
  --forms \
  --add-combo="Mode" \
    --combo-values="All screen|Specific window|Specific area" \
  --add-combo="Include cursor[default: no]" \
    --combo-values="no|yes" \
    --add-entry="time to wait[default: 0s]" \
    --add-combo="Copy to clipboard[default: yes]" \
    --combo-values="yes|no" \
      )

result=$?

mode=$(echo $values | cut -d '|' -f 1)
cursor=$(echo $values | cut -d '|' -f 2)
wait=$(echo $values | cut -d '|' -f 3)
clipboard=$(echo $values | cut -d '|' -f 4)

if [ "$result" -eq 1 ];then      # select cancel
	echo "canceling"
	exit
fi

if [ ! -z "$cursor" ] &&  [ "$cursor" == yes ];then
	OPTION+="-c"
fi

if [ -z "$mode" ] ;then       # select nothing
	echo "mode is null"
	zenity \
		--title=wayland-screenshot \
		--width=200 \
		--warning \
		--text="Mode empty"
	exit
fi

if [ ! -z "$wait" ];then
	sleep $wait
fi

if [ "$mode" == "All screen" ];then  # select All screen
	true
elif [ "$mode" == "Specific window" ];then  # select specify window
	GEO="$(swaymsg -t get_tree | jq -r '.. | select(.pid? and .visible?) | .rect | "\(.x),\(.y) \(.width)x\(.height)"' | slurp)"
elif [ "$mode" == "Specific area" ];then  # select specfy area
	GEO="$(slurp)"
else                         # error
	echo $mode
fi

if [ -z "$clipboard" ] || [ "$clipboard" == yes ];then
	if [ -z "$GEO" ]; then
		grim $OPTION - | wl-copy;
	else
		grim $OPTION -g "$GEO" - | wl-copy;
	fi
else
	if [ -z "$GEO" ]; then
		grim $OPTION $FILEDIR/Screenshot_$(date +%F_%H.%M.%S).png
	else
		grim $OPTION -g "$GEO" $FILEDIR/Screenshot_$(date +%F_%H.%M.%S).png
	fi
fi
