#!/bin/bash

# GUI-based Wrapper for Hotspot Setup

# Install Zenity if not installed
if ! command -v zenity &> /dev/null; then
    echo "Zenity not found, installing..."
    sudo apt-get install -y zenity
fi

# Set the path to the original script (same directory as this wrapper)
SCRIPT_DIR="$(dirname "$0")"
ORIGINAL_SCRIPT="$SCRIPT_DIR/setup-hotspot.sh"

# Ensure the original script is executable
chmod +x "$ORIGINAL_SCRIPT"

# Prompt user for SSID and interface using Zenity
SSID=$(zenity --entry --title="Hotspot SSID" --text="Enter SSID for the hotspot:" --entry-text="BirdNET-Hotspot")
INTERFACE=$(zenity --entry --title="WiFi Interface" --text="Enter WiFi interface:" --entry-text="wlan0")

# Prompt user for password (shown while typing, no default value)
PASSWORD=$(zenity --entry --title="Hotspot Password" --text="Enter password (min 8 chars):")

# Validate password length
if [[ -z "$PASSWORD" || ${#PASSWORD} -lt 8 ]]; then
    zenity --error --text="Error: Password must be at least 8 characters long."
    exit 1
fi

# Confirm the entered details
zenity --question --title="Confirm Hotspot Setup" --text="SSID: $SSID\nPassword: $PASSWORD\nInterface: $INTERFACE\n\nProceed?"

if [[ $? -ne 0 ]]; then
    zenity --info --text="Setup cancelled."
    exit 0
fi

# Proceeding with setup
zenity --info --text="Starting Hotspot Setup..."

# Execute the original script with provided parameters
sudo "$ORIGINAL_SCRIPT" --ssid "$SSID" --password "$PASSWORD" --interface "$INTERFACE"

# Notify user of completion
zenity --info --text="Hotspot setup complete! SSID: $SSID, Interface: $INTERFACE"
