#!/bin/bash

REQUIRED_TOOLS=("gum")

echo -n "📦 Checking and installing dependencies... "
# OS Detection
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$ID
else
    echo "Unsupported OS. Manual installation required."
    exit 1
fi

# Install dependancies
install_tool() {
    local tool=$1
    echo "--- Tool '$tool' is missing. ---"
    read -p "Would you like to install $tool? (y/n): " confirm
    
    if [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]]; then
        case $OS in
            ubuntu|debian|kali|pop|mint|linuxmint|kubuntu|xubuntu|lubuntu|elementary|zorin)
                sudo apt update && sudo apt install -y "$tool"
                echo "'$tool' Installed"
                ;;
            fedora|rhel|centos)
                sudo dnf install -y "$tool" > /dev/null 2>&1
                ;;
            *)
                echo "Package manager for '$OS' not defined in this script."
                exit 1
                ;;
        esac
    else
        echo "Exiting: $tool is required for this script."
        exit 1
    fi
}

# Confirm everything installed correctly
for tool in "${REQUIRED_TOOLS[@]}"; do
    if ! command -v "$tool" &> /dev/null; then
        install_tool "$tool"
    fi
done

declare -A INSTALL_CMDS
declare -a MENU_OPTIONS

add_app() {
    local name="$1"
    local desc="$2"
    local cmd="$3"
    # Format: Grey for the description text
    local menu_item=$(printf "%-12s | %s" "$name" "$desc")
    MENU_OPTIONS+=("$menu_item")
    INSTALL_CMDS["$menu_item"]="$cmd"
}

# --- YOUR APPS ---
add_app "Gradia"    "Modern screenshot annotation" "flatpak install -y flathub be.alexandervanhee.gradia"
add_app "Obsidian"  "Markdown knowledge base"      "flatpak install -y flathub md.obsidian.Obsidian"
add_app "Brave"     "Privacy browser"              "sudo dnf install -y brave-browser"
add_app "Newsflash" "Desktop RSS reader"           "flatpak install -y flathub io.gitlab.news_flash.NewsFlash"
add_app "Plex Amp"  "Play music from Plex server"  "flatpak install -y flathub com.plexamp.Plexamp"
add_app "RPi Imager" "Raspber Pi Imager"            "sudo dnf install -y rpi-imager"
add_app "Local Send" "Send Files over the network"  "flatpak install -y flathub org.localsend.localsend_app"
add_app "Planify"      "To Do List"                 "flatpak install -y flathub io.github.alainm23.planify"
add_app "Whatsapp" "Zap Zap Whatsapp client"        "flatpak install -y flathub com.rtosta.zapzap"
add_app "Celluloid" "Video PLayer"                 "sudo dnf install -y celluloid"
add_app "Remmina" "Remote Desktop"                 "sudo dnf install -y remmina"
# -----------------

clear

# Title: Orange text with a Grey border
gum style \
    --border rounded \
    --margin "1" \
    --padding "1 2" \
    --border-foreground 244 \
    --foreground 208 \
    "󰣛  Fedora App Selector"

# 1. Selection
SELECTIONS=$(printf "%s\n" "${MENU_OPTIONS[@]}" | gum choose --no-limit \
    --header "Select apps (Space to mark, Enter to confirm)" \
    --header.foreground 244 \
    --cursor.foreground 208 \
    --selected.foreground 208)

if [ -z "$SELECTIONS" ]; then
    gum style --foreground 244 "No apps selected. Exiting."
    exit 0
fi

# 2. Confirmation (Orange theme)
gum confirm --selected.background 208 "Ready to install?" || exit 0

# 3. Installation Loop
echo "$SELECTIONS" | while IFS= read -r selection; do
    cmd="${INSTALL_CMDS[$selection]}"
    app_name=$(echo "$selection" | awk -F' \\| ' '{print $1}' | xargs)
    
    # Spinner: Orange spinner with Grey text
    gum spin --spinner dot --title "Installing $app_name..." --title.foreground 244 --spinner.foreground 208 -- bash -c "$cmd > /dev/null 2>&1"
    
    if [ $? -eq 0 ]; then
        gum style --foreground 208 "  ✓ $app_name installed"
    else
        gum style --foreground 196 "  ✗ Failed to install $app_name"
    fi
done

# 4. Cleanup
stty sane
printf "\033[K"
echo ""
gum style --foreground 208 --bold "󰄬 All tasks finished!"
