#!/bin/bash

check_flatpak() {
    if ! command -v flatpak &>/dev/null; then
        echo "Flatpak is not installed. Please install Flatpak and run the script again."
        exit 1
    fi
}

menu_office_suites() {
    dialog --clear --backtitle "Office Suite Selector" \
        --title "Select an office suite" \
        --menu "Choose one of the following options:" 18 40 5 \
        1 "LibreOffice Fresh (Arch)" \
        2 "LibreOffice Still (Arch)" \
        3 "LibreOffice (Flatpak)" \
        4 "OnlyOffice (Flatpak)" \
        5 "Calligra Suite (AUR)" \
        6 "Exit" 2>&1 >/dev/tty
}

while true; do
    choice=$(menu_office_suites)
    case $choice in
        1)
            echo "Installing LibreOffice (Fresh) from Arch repos..."
            pkexec pacman -S libreoffice-fresh --noconfirm
            echo -e "\n==> LibreOffice installed successfully."
            ;;
        2)
            echo "Installing LibreOffice (Still) from Arch repos..."
            pkexec pacman -S libreoffice-fresh --noconfirm
            echo -e "\n==> LibreOffice installed successfully."
            ;;

        2)
            check_flatpak
            echo "Installing LibreOffice from Flatpak..."
            flatpak install flathub org.libreoffice.LibreOffice
            echo -e "\n==> LibreOffice installed successfully."
            ;;
        3)
            check_flatpak
            echo "Installing OnlyOffice from Flatpak..."
            flatpak install flathub org.onlyoffice.desktopeditors
            echo -e "\n==> OnlyOffice installed successfully."
            ;;
        4)
            echo "Installing Calligra Suite from AUR..."
            pkexec pacman -S calligra --noconfirm
            echo -e "\n==> Calligra Suite installed successfully."
            ;;
        5)
            echo "Exiting..."
            exit 0
            ;;
    esac
done
