#!/bin/zsh

name_executable() {
    read "?The current name of the executable is $1. Do you want to change it? (y/N)" CHOICE

    if [[ $CHOICE == "y" || $CHOICE == "Y" ]]; then
        read "?Enter the name of the executable: " NAME
        echo $NAME
    else
        echo $1
    fi
}

save_desktop_entry() {
    if [[ "$OSTYPE" == "linux-gnu" ]]; then
        NAME=$1
        EXECUTABLE=$2

        echo "Creating a desktop entry for the $NAME project...\n"

        # Create the desktop entry
        sudo echo "[Desktop Entry]" > /usr/share/applications/$NAME.desktop
        sudo echo "Name=$NAME" >> /usr/share/applications/$NAME.desktop
        sudo echo "Exec=$EXECUTABLE" >> /usr/share/applications/$NAME.desktop
        sudo echo "Type=Application" >> /usr/share/applications/$NAME.desktop
        sudo echo "# Auto-generated by the install script" >> /usr/share/applications/$NAME.desktop
    else
        echo "This operating system is not yet supported."
        exit 1
    fi
}

help() {
    echo "This script installs a project onto the user's system."
    echo "Usage: install"
    echo "       install <path-to-project>"
    echo "       install -h | --help"
    echo "\nIf no path is provided, the script will look for a dependencies.txt file in the current directory."
    exit 0
}

error_missing_dependencies() {
    echo "The dependencies.txt file is missing from the project directory."
    echo "This file should contain the required packages for the project to run."
    echo "The format should be as follows: <command>,<package>"
    echo "For example: python,python3"
    echo "Please create this file before continuing."
    echo "You can find an example file in the root directory of this project."
    exit 1
}

success() {
    NAME=$1
    EXECUTABLE=$2

    echo "The $NAME project and it's dependencies have been successfully installed onto your system."
    echo "You can now run the project by typing $EXECUTABLE into the terminal."

    read "?Would you like to create a desktop entry for the $NAME project? (y/N): " CHOICE

    if [[ $CHOICE == "y" || $CHOICE == "Y" ]]; then
        save_desktop_entry $NAME $EXECUTABLE
    fi
}

install_c() {
    NAME=$1
    echo "\nOther languages and build systems are not yet supported."

    exit 0
}

install_python() {
    NAME=$1
    EXECUTABLE=$(name_executable $1)

    # Get the current working directory and the virtual environment
    CWD=$(pwd)
    ENV=$(pwd)/bin/activate
    COMMAND="python $CWD/src/$NAME.py"

    # Create the virtual environment
    python -m venv .
    source bin/activate

    # Install Python packages in the virtual environment
    if [[ -f requirements.txt ]]; then
        pip install -r requirements.txt
    fi


    # Create the executable
    sudo echo "#!/bin/zsh" > /usr/local/bin/$EXECUTABLE
    sudo echo "source $ENV" >> /usr/local/bin/$EXECUTABLE
    sudo echo "$COMMAND" >> /usr/local/bin/$EXECUTABLE
    sudo echo "# Auto-generated by the install script" >> /usr/local/bin/$EXECUTABLE
    sudo chmod +x /usr/local/bin/$EXECUTABLE

    success "$NAME" "$EXECUTABLE"
}

install_java() {
    NAME=$1
    echo "Other languages and build systems are not yet supported."

    exit 0
}

install_other() {
    NAME=$1
    echo "Other languages and build systems are not yet supported."

    exit 0
}

install() {
    PROJECT_NAME=$1

    echo "\nInstalling the $PROJECT_NAME project onto your system..."

    echo "Please select the project language or build system:"
    echo "1. C"
    echo "2. Python"
    echo "3. Java"
    echo "4. Other"

    read "?Enter the number of the project type: " PROJECT_TYPE

    case $PROJECT_TYPE in
        1)
            install_c "$PROJECT_NAME"
            ;;
        2)
            install_python "$PROJECT_NAME"
            ;;
        3)
            install_java "$PROJECT_NAME"
            ;;
        4)
            install_other "$PROJECT_NAME"
            ;;
        *)
            echo "Invalid option"
            ;;
    esac
}

install_dependencies() {
    echo "Checking for required packages..."

    # Check if the check-dependency command is installed
    if ! command -v check-dependency &> /dev/null
    then
        echo "The command check-dependency could not be found. Please install it before continuing."
        echo "You can find it at https://github.com/quaxlyqueen/scripts."
        exit 1
    fi

    # Iterate through each line of the dependencies.txt file
    # and check if the package is installed with check-dependency <command> <package>
    while read -r line; do
        command=$(echo $line | awk -F ',' '{print $1}')
        package=$(echo $line | awk -F ',' '{print $2}')

        check-dependency $command $package

        # If the package could not be installed, exit the script
        if [[ $? -eq 1 ]]; then
            echo "The command $command from package $package could not be installed. Please install it manually before continuing."
            exit 1
        fi

    done < dependencies.txt
}

check_sudo() {
    if [[ $EUID -ne 0 ]]; then
        echo "This script must be run as root"
        echo "Try 'sudo ./install'"
        exit 1
    fi
}

check_theme() {
    USER=$SUDO_USER
    if [ ! -d /home/$USER/.config ];
    then
        echo "Creating the ~/.config directory..."
        su $USER -c "mkdir /home/$USER/.config"
    fi

    if [ ! -d /home/$USER/.config/theme ];
    then
        echo "Creating the /home/$USER/.config/theme directory..."
        su $USER -c "mkdir /home/$USER/.config/theme"
    fi

    THEME_DIR=/home/$USER/.config/theme

    if [ ! -f $THEME_DIR/active.theme ];
    then
        su $USER -c "touch $THEME_DIR/light.theme"
        echo "base=#C6CAED" > $THEME_DIR/light.theme
        echo "accent=#4D5382" >> $THEME_DIR/light.theme
        echo "hover=#262940" >> $THEME_DIR/light.theme

        su $USER -c "touch $THEME_DIR/dark.theme"
        echo "base=#262940" > $THEME_DIR/dark.theme
        echo "accent=#4D5382" >> $THEME_DIR/dark.theme
        echo "hover=#C6CAED" >> $THEME_DIR/dark.theme

        su $USER -c "ln -s $THEME_DIR/dark.theme $THEME_DIR/active.theme"
    fi
}

install-extra() {
    if [ -f "install-extra" ]; then
        echo "Running install-extra..."
        ./install-extra
    fi
}

check_args() {
    check_sudo
    check_theme

    if [[ $# -gt 1 ]]; then
        echo "ERROR: There were too many arguments.\n"
        echo "Usage: install"
        echo "       install <path-to-project>"
        echo "       install -h | --help"
        exit 1

    # No arguments provided
    elif [[ $# -eq 0 ]]; then

        # Check if the current directory contains the required dependencies.txt file.
        if [[ ! -f dependencies.txt ]]; then
            error_missing_dependencies

        # Install the project
        else
            PROJECT_NAME=$(pwd | cut -d '/' -f5)
            install_dependencies
            install "$PROJECT_NAME"
            install-extra
            exit 0
        fi

    # User argument provided
    else

        # Display help message
        if [[ $1 == "-h" || $1 == "--help" ]]; then
            help
        fi

        # Check if the specified directory exists
        if [[ ! -d $1 ]]; then
            echo "The specified project directory does not exist"
            exit 1

        # Check if the specified directory contains the required dependencies.txt file.
        elif [[ ! -f $1/dependencies.txt ]]; then
            error_missing_dependencies

        # Install the project
        else
            PROJECT_NAME=$1
            install_dependencies
            cd $PROJECT_NAME
            install-extra
            exit 0
        fi
    fi
}

check_args $1
