#!/bin/bash

# This script is used to change the top and bottom padding of the outer container in the Aerospace theme.

CONFIG_FILE="$HOME/.config/aerospace/aerospace.toml"
BACKUP_FILE="/tmp/aerospace_presentation_backup"

presentation_top_gaps=80
presentation_bottom_gaps=80
presentation_left_gaps=80
presentation_right_gaps=80

if [ "$1" == "on" ]; then
    # Save original lines with line numbers
    grep -n 'outer\.top\|outer\.bottom\|outer\.left\|outer\.right' "$CONFIG_FILE" >"$BACKUP_FILE"

    # Replace with presentation values
    sed -i'' -e "s/^\([[:space:]]*outer\.top[[:space:]]*=\).*/\1 ${presentation_top_gaps}/" "$CONFIG_FILE"
    sed -i'' -e "s/^\([[:space:]]*outer\.bottom[[:space:]]*=\).*/\1 ${presentation_bottom_gaps}/" "$CONFIG_FILE"
    sed -i'' -e "s/^\([[:space:]]*outer\.left[[:space:]]*=\).*/\1 ${presentation_left_gaps}/" "$CONFIG_FILE"
    sed -i'' -e "s/^\([[:space:]]*outer\.right[[:space:]]*=\).*/\1 ${presentation_right_gaps}/" "$CONFIG_FILE"

    # Set dark wallpaper for presentation
    osascript -e 'tell application "System Events" to set picture of every desktop to "/Users/rayandrew/Pictures/Wallpapers/black-gray.jpg"' >/dev/null 2>&1

    aerospace reload-config
    echo -n "Presentation mode ON"

elif [ "$1" == "off" ]; then
    if [ ! -f "$BACKUP_FILE" ]; then
        echo "Error: No backup found. Run 'presentation-mode on' first."
        exit 1
    fi

    # Restore original values line by line
    while IFS=: read -r line_num content; do
        # Escape special characters for sed
        escaped_content=$(printf '%s\n' "$content" | sed 's/[&/\]/\\&/g')
        sed -i'' -e "${line_num}s/.*/${escaped_content}/" "$CONFIG_FILE"
    done <"$BACKUP_FILE"

    # Restore original wallpaper
    osascript -e 'tell application "System Events" to set picture of every desktop to "/Users/rayandrew/Pictures/Wallpapers/bluering.png"' >/dev/null 2>&1

    rm "$BACKUP_FILE"
    aerospace reload-config
    echo "Presentation mode OFF"

else
    echo "Usage: presentation-mode [on|off]"
    exit 1
fi
