#!/bin/bash

source swiftly-utils.sh

# Define functions for each command
activate() {
    "ensure_sourced"

    # Check if the project is already activated
    if [[ "$SWIFTLY_ACTIVATED" == "true" ]]; then
        echo "Swiftly project already activated."
        return
    fi

    # Run check_swiftly from swiftly.core.main.py
    python3 -c "from swiftly.core.main import check_swiftly; check_swiftly()"
    result=$(read_cli_result)

    # Handle the result
    case $result in
        "init")
            init  # Assuming you have an init function defined elsewhere in your script
            return 0  # Exit after initialization
            ;;
        "exit")
            return 0
            ;;
        "continue")
            # Continue with the rest of the script
            ;;
        *)
            echo "Unexpected result from check_swiftly: $result"
            return 1
            ;;
    esac

    # update swiftly
    python3 -c "from swiftly.core.main import update_swiftly; update_swiftly()"

    # get swiftly project name
    project_name=$(python3 -c "from swiftly.utils.get import get_name; print(get_name())")
    export SWIFTLY_PROJECT_NAME="$project_name"
    export SWIFTLY_PROJECT_LOCATION="$(pwd)"

    # Modify the shell prompt
    export OLD_PS1="$PS1"  # Store the current PS1 value

    # git pull
    python3 -c "from swiftly.utils.git import git_pull; git_pull()"

    # get swiftly project runtime
    runtime=$(python3 -c "from swiftly.utils.get import get_runtime; print(get_runtime())")

    # Source the appropriate script and run the activate function
    source "swiftly-${runtime}.sh"
    "activate_${runtime}"  # This will call a function like activate_python, activate_node, etc. based on the runtime

    PS1="(swiftly ${SWIFTLY_PROJECT_NAME}) $OLD_PS1"
    export SWIFTLY_ACTIVATED=true
}

deactivate() {
    "ensure_sourced"
    
    # Check if the project is not activated
    if [[ "$SWIFTLY_ACTIVATED" != "true" ]]; then
        echo "Swiftly project is not activated."
        return
    fi

    # Get the runtime
    runtime=$(python3 -c "from swiftly.utils.get import get_runtime; print(get_runtime())")

    # Source the appropriate script and run the deactivate function
    source "swiftly-${runtime}.sh"
    "deactivate_${runtime}"  # This will call a function like deactivate_python, deactivate_node, etc. based on the runtime

    # Restore the original PS1 value
    PS1="$OLD_PS1"
    unset OLD_PS1

    # Unset the SWIFTLY_ACTIVATED variable
    unset SWIFTLY_ACTIVATED
}

init() {
    echo "Running init function"
    # Add your complex init command here
}

makeapp() {
    echo "Running makeapp function"
    # Add your complex makeapp command here
}

run() {
    echo "Running run function"
    # Add your complex run command here
}

install() {
    echo "Running install function"
    # Add your complex install command here
}

uninstall() {
    echo "Running uninstall function"
    # Add your complex uninstall command here
}

add_framework() {
    echo "Running add-framework function"
    # Add your complex add-framework command here
}

# Function to handle custom commands
custom() {
    echo "Running custom function with arguments: $@"
    # Add your custom command handling logic here
}

# Check if a function exists and call it, otherwise call the custom function
if declare -f "$1" > /dev/null; then
    "$@"
else
    # Check if there's more than one argument
    if [ $# -gt 1 ]; then
        custom "${@:2}"  # Pass all arguments except the first one to custom
    fi
fi

