#!/bin/bash

#sleep 5
#screenshot_dir="$HOME/Screenshots"
#timestamp=$(date +"%Y-%m-%d-%H-%M-%S")
#screenshot_file="$screenshot_dir/screenshot_$timestamp.png"

# Function to select the region
#select_region() {
#    region=$(/usr/bin/slurp)
#if [ -z "$region" ]; then
#    echo "No region selected, retrying..."
#    exit 1
#fi
#echo "$region"
#}

# Function to take screenshot
#take_screenshot() {
#    /usr/bin/grim -g "$1" "$screenshot_file"
#    ~/.local/bin/pastebin_client.sh upload "$screenshot_file" yes | /usr/bin/wl-copy
#}

#main() {
#    while true; do
#        region=$(select_region)
#        if [ -n "$region" ]; then
#            take_screenshot "$region"
#            echo "Screenshot taken and uploaded."
#            break
#        else
#            echo "No region selected. Try again..."
#            sleep 2
#        fi
#    done
#}

#main

sleep 5
screenshot_dir="$HOME/Screenshots"
mkdir -p "$screenshot_dir"
timestamp=$(date +"%Y-%m-%d-%H-%M-%S")
screenshot_file="$screenshot_dir/screenshot_$timestamp.png"

# Función para seleccionar región con slurp
select_region() {
    region=$(/usr/bin/slurp)
    if [ -z "$region" ]; then
        echo "No region selected, retrying..."
        exit 1
    fi
    echo "$region"
}

# Función para tomar screenshot y subir
take_screenshot() {
    /usr/bin/grim -g "$1" "$screenshot_file" && \
    ~/.local/bin/gitlab-paste "$screenshot_file"
}

main() {
    while true; do
        region=$(select_region)
        if [ -n "$region" ]; then
            take_screenshot "$region"
            notify-send "📤 Screenshot subido" "El enlace ha sido copiado al portapapeles"
            break
        else
            echo "No region selected. Try again..."
            sleep 2
        fi
    done
}

main

