#!/bin/bash
# bvchelpers.sh - BVideoconverter Helpers Installer
# Installs only recommended helper programs (video editors, subtitle tools, etc.)
# Languages: English / Suomi / Svenska
#
# Copyright (C) 2026 Mikko Silvennoinen
# License: GNU Affero General Public License v3 or later

set -e

# ============================================================
# 0. Language selection (EN / FI / SV)
# ============================================================
echo "Select language / Valitse kieli / Välj språk:"
echo " 1) English"
echo " 2) Suomi"
echo " 3) Svenska"
read -r -p "Choice / Valinta / Val: " lang_choice
case "$lang_choice" in
    2) LANG_CODE="FI" ;;
    3) LANG_CODE="SV" ;;
    *) LANG_CODE="EN" ;;
esac

say() {
    local en="$1" fi="$2" sv="$3"
    case "$LANG_CODE" in
        FI) echo "$fi" ;;
        SV) echo "$sv" ;;
        *) echo "$en" ;;
    esac
}

show_info_gui() {
    local msg="$1"
    if command -v zenity >/dev/null 2>&1; then
        zenity --info --width=480 --title="BVideoconverter Helpers" --text="$msg" 2>/dev/null || echo "$msg"
    elif command -v yad >/dev/null 2>&1; then
        yad --info --width=480 --title="BVideoconverter Helpers" --text="$msg" --button=OK 2>/dev/null || echo "$msg"
    else
        echo "$msg"
    fi
}

prompt_yn() {
    local q_en="$1" q_fi="$2" q_sv="$3"
    say "$q_en" "$q_fi" "$q_sv"
    read -r -p "> " answer
    c="${answer:0:1}"
    case "$LANG_CODE" in
        FI) [[ "$c" =~ [kK] ]] && return 0 ;; # k = kyllä
        SV) [[ "$c" =~ [jJ] ]] && return 0 ;; # j = ja
        *) [[ "$c" =~ [yY] ]] && return 0 ;;
    esac
    return 1
}

# ============================================================
# 1. Basic checks (root & apt)
# ============================================================
if [ "$(id -u)" != "0" ]; then
    say "This script must be run as root (use sudo)." \
        "Tämä skripti vaatii pääkäyttäjän oikeudet (käytä sudo)." \
        "Skriptet kräver root-rättigheter (använd sudo)."
    exit 1
fi

if ! command -v apt-get >/dev/null 2>&1; then
    say "This script requires APT (Debian/Ubuntu/Mint-based system)." \
        "Tämä skripti vaatii APT:n (Debian/Ubuntu/Mint-pohjainen järjestelmä)." \
        "Skriptet kräver APT (Debian/Ubuntu/Mint-baserat system)."
    exit 1
fi

# ============================================================
# 2. Helper programs by category (one per category)
# ============================================================
VIDEO_EDITORS="kdenlive openshot-qt shotcut"
SUBTITLE_EDITORS="gnome-subtitles gaupol subtitlecomposer"
PICTURE_VIEWERS="xviewer eog gpicview"
VIDEO_PLAYERS="celluloid vlc mpv"
PICTURE_EDITORS="gimp krita pinta"
SCREEN_CAPTURES="simplescreenrecorder recordmydesktop"
OTHER_PROGRAMS="flameshot"

declare -A CATEGORIES=(
    ["Video Editor"]="$VIDEO_EDITORS"
    ["Subtitle Editor"]="$SUBTITLE_EDITORS"
    ["Picture Viewer"]="$PICTURE_VIEWERS"
    ["Video Player"]="$VIDEO_PLAYERS"
    ["Picture Editor"]="$PICTURE_EDITORS"
    ["Screen Capture"]="$SCREEN_CAPTURES"
    ["Other Programs"]="$OTHER_PROGRAMS"
)

declare -A CAT_NAMES
if [ "$LANG_CODE" = "FI" ]; then
    CAT_NAMES["Video Editor"]="Videoeditori"
    CAT_NAMES["Subtitle Editor"]="Tekstityseditori"
    CAT_NAMES["Picture Viewer"]="Kuvien katseluohjelma"
    CAT_NAMES["Video Player"]="Videosoitin"
    CAT_NAMES["Picture Editor"]="Kuvankäsittelyohjelma"
    CAT_NAMES["Screen Capture"]="Näytönkaappausohjelma"
    CAT_NAMES["Other Programs"]="Muut ohjelmat"
elif [ "$LANG_CODE" = "SV" ]; then
    CAT_NAMES["Video Editor"]="Videoredigerare"
    CAT_NAMES["Subtitle Editor"]="Undertextredigerare"
    CAT_NAMES["Picture Viewer"]="Bildvisare"
    CAT_NAMES["Video Player"]="Videospelare"
    CAT_NAMES["Picture Editor"]="Bildredigerare"
    CAT_NAMES["Screen Capture"]="Skärminspelning"
    CAT_NAMES["Other Programs"]="Andra program"
else
    CAT_NAMES["Video Editor"]="Video Editor"
    CAT_NAMES["Subtitle Editor"]="Subtitle Editor"
    CAT_NAMES["Picture Viewer"]="Picture Viewer"
    CAT_NAMES["Video Player"]="Video Player"
    CAT_NAMES["Picture Editor"]="Picture Editor"
    CAT_NAMES["Screen Capture"]="Screen Capture"
    CAT_NAMES["Other Programs"]="Other Programs"
fi

# ============================================================
# 3. Install one helper per category
# ============================================================
say "Installing recommended helper programs for BVideoconverter..." \
    "Asennetaan suositeltuja apuohjelmia BVideoconverterille..." \
    "Installerar rekommenderade hjälpprogram för BVideoconverter..."

for cat in "${!CATEGORIES[@]}"; do
    programs="${CATEGORIES[$cat]}"
    installed=false
    for pkg in $programs; do
        if dpkg -l "$pkg" 2>/dev/null | grep -q "^ii"; then
            say "$pkg already installed for $(cat_names "$cat")." \
                "$pkg on jo asennettu kategoriaan $(cat_names "$cat")." \
                "$pkg är redan installerat för kategori $(cat_names "$cat")."
            installed=true
            break
        fi
        if apt-get install -y "$pkg" >/dev/null 2>&1; then
            say "$pkg installed for $(cat_names "$cat")." \
                "$pkg asennettu kategoriaan $(cat_names "$cat")." \
                "$pkg installerat för kategori $(cat_names "$cat")."
            installed=true
            break
        fi
    done
    if ! $installed; then
        say "Failed to install any program for $(cat_names "$cat")." \
            "Yhtäkään ohjelmaa ei voitu asentaa kategoriaan $(cat_names "$cat")." \
            "Kunde inte installera något program för kategori $(cat_names "$cat")."
    fi
done

say "All done! You can now use BVideoconverter with these helpers." \
    "Valmista! Voit nyt käyttää BVideoconverteria näiden apuohjelmien kanssa." \
    "Klart! Du kan nu använda BVideoconverter med dessa hjälpprogram."

exit 0
