#!/bin/bash

# Function to install an app
install_app() {
    local app_directory="$1"
    local selected_app_name="$2"
    local installed_file="$HOME/.linstore/installed"
    local temp_script=$(mktemp)

    if [ -e "$app_directory/install" ]; then
        echo "cd \"$app_directory\" && chmod +x install && ./install && echo \"$selected_app_name\" >> \"$installed_file\" && echo \"$selected_app_name is installed\" && cat install > \"$HOME/.linstore/installscripts/${selected_app_name}\"" >"$temp_script"
        echo "x-terminal-emulator -e 'bash -c \"sudo echo && bash $temp_script\"'"
    else
        local architecture=$(cat "$HOME/.linstore/architecture.txt")
        local install_script="install-$architecture"

        if [ -e "$app_directory/$install_script" ]; then
            echo "cd \"$app_directory\" && chmod +x \"$install_script\" && ./\"$install_script\" && echo \"$selected_app_name\" >> \"$installed_file\" && echo \"$selected_app_name is installed\" && cat \"$install_script\" > \"$HOME/.linstore/installscripts/${selected_app_name}\"" >"$temp_script"
            echo "x-terminal-emulator -e 'bash -c \"sudo echo && bash $temp_script\"'"
        else
            if [ -e "$app_directory/packages" ]; then
                echo "cd \"$app_directory\" && sudo apt update && sudo apt install \$(cat \"$app_directory/packages\") && echo \"$selected_app_name\" >> \"$installed_file\" && echo \"$selected_app_name is installed\" && cat packages > \"$HOME/.linstore/installscripts/${selected_app_name}\"" >"$temp_script"
                echo "x-terminal-emulator -e 'bash -c \"sudo echo && bash $temp_script\"'"
            else
                echo "echo 'Installation script not found for this architecture.'"
            fi
        fi
    fi
}

# Function to install an app
installation_script() {
    local app_directory="$1"
    local selected_app_name="$2"
    local installed_file="$HOME/.linstore/installed"

    if [ -e "$app_directory/install" ]; then
        echo "install"
    else
        local architecture=$(cat "$HOME/.linstore/architecture.txt")
        local install_script="install-$architecture"

        if [ -e "$app_directory/$install_script" ]; then
            echo "$install_script"
        else
            if [ -e "$app_directory/packages" ]; then
                echo "packages"
            else
                echo "install"
            fi
        fi
    fi
}

# Function to uninstall an app
uninstall_app() {
    local app_directory="$1"
    local selected_app_name="$2"
    local uninstalled_file="$HOME/.linstore/uninstalled"
    local temp_script=$(mktemp)

    if [ -e "$app_directory/uninstall" ]; then
        echo "cd \"$app_directory\" && chmod +x uninstall && ./uninstall && echo \"$selected_app_name\" >> \"$uninstalled_file\" && echo \"$selected_app_name has been uninstalled\"" >"$temp_script"
        echo "x-terminal-emulator -e \"sudo echo && bash $temp_script\""
    else
        local architecture=$(cat "$HOME/.linstore/architecture.txt")
        local uninstall_script="uninstall-$architecture"

        if [ -e "$app_directory/$uninstall_script" ]; then
            echo "cd \"$app_directory\" && chmod +x \"$uninstall_script\" && ./\"$uninstall_script\" && echo \"$selected_app_name\" >> \"$uninstalled_file\" && echo \"$selected_app_name has been uninstalled\"" >"$temp_script"
            echo "x-terminal-emulator -e \"sudo echo && bash $temp_script\""
        else
            if [ -e "$app_directory/packages" ]; then
                echo "cd \"$app_directory\" && sudo apt update && sudo apt remove \$(cat \"$app_directory/packages\") && echo \"$selected_app_name\" >> \"$uninstalled_file\" && echo \"$selected_app_name has been uninstalled\"" >"$temp_script"
                echo "x-terminal-emulator -e \"sudo echo && bash $temp_script\""
            else
                echo "echo 'Uninstallation script not found for this architecture.'"
            fi
        fi
    fi
}

# Function to uninstall an app
uninstall_app() {
    local app_directory="$1"
    local selected_app_name="$2"
    local uninstalled_file="$HOME/.linstore/uninstalled"
    local temp_script=$(mktemp)

    if [ -e "$app_directory/uninstall" ]; then
        echo "cd \"$app_directory\" && chmod +x uninstall && ./uninstall && echo \"$selected_app_name has been uninstalled\"" >"$temp_script"
    else
        local architecture=$(cat "$HOME/.linstore/architecture.txt")
        local uninstall_script="uninstall-$architecture"

        if [ -e "$app_directory/$uninstall_script" ]; then
            echo "cd \"$app_directory\" && chmod +x \"$uninstall_script\" && ./\"$uninstall_script\" && echo \"$selected_app_name has been uninstalled\"" >"$temp_script"
        else
            if [ -e "$app_directory/packages" ]; then
                echo "cd \"$app_directory\" && sudo apt update && sudo apt remove \$(cat \"$app_directory/packages\") && echo \"$selected_app_name has been uninstalled\"" >"$temp_script"
            else
                echo "echo 'Uninstallation script not found for this architecture.'" >"$temp_script"
            fi
        fi
    fi

    echo "x-terminal-emulator -e 'sudo echo && bash \"$temp_script\"'"
}

# Function to search for apps
search_apps() {
    query=$(GTK_THEME="${theme}" yad --title="Search Apps" --text="Enter app name:" --entry)
    if [ -n "$query" ]; then
        app_list=$(ls -w 1 ./apps | awk 'BEGIN {ORS="\n"} {print}')
        num_apps=$(echo "$app_list" | wc -l)
        num_apps=$((num_apps))
        echo "$num_apps"

        apps_found=()
        for app in ./apps/*; do
            app_name=$(basename "$app")
            if [[ "${app_name,,}" == *"${query,,}"* ]]; then
                app_icon="apps/$app_name/icon-24.png"
                app_category_file="$app/category"
                app_category=$(head -n 1 "$app_category_file")
                apps_found+=("$app_icon" "$app_name")
            fi
        done

        if [ ${#apps_found[@]} -gt 0 ]; then
            selected_category2=$(GTK_THEME="${theme}" yad --title="Search Results for ${query}" --width=400 --height=300 --list --column="Icon:IMG" --column="App Name" "${apps_found[@]}" --borders=10)
            if [ -n "$selected_category2" ]; then
                ./gui showdetails "${selected_category2}"
            fi
        else
            GTK_THEME="${theme}" yad --title="Search Results for ${query}" --width=300 --height=100 --text="No apps found for '${query}'.\nPlease try another query." --borders=10 --button="OK:0" --center
        fi
    fi
}

# Main logic to handle command line arguments
if [[ $1 == "search" ]]; then
    search_apps
elif [[ $1 == "install" ]]; then
    install_app "$2" "$3"
elif [[ $1 == "whatscript" ]]; then
    installation_script "$2" "$3"
elif [[ $1 == "uninstall" ]]; then
    uninstall_app "$2" "$3"
fi
