#!/bin/bash

REQUIRED_TOOLS=("git" "curl" "fastfetch" "lsd" "micro" "zoxide" "fzf" "kitty")

echo -n "📦 Checking and installing dependencies... "
# OS Detection
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$ID
else
    echo "Unsupported OS. Manual installation required."
    exit 1
fi

# Install dependancies
install_tool() {
    local tool=$1
    echo "--- Tool '$tool' is missing. ---"
    read -p "Would you like to install $tool? (y/n): " confirm
    
    if [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]]; then
        case $OS in
            ubuntu|debian|kali|pop|mint|linuxmint|kubuntu|xubuntu|lubuntu|elementary|zorin)
                sudo apt update && sudo apt install -y "$tool"
                echo "'$tool' Installed"
                ;;
            fedora|rhel|centos)
                sudo dnf install -y "$tool" > /dev/null 2>&1
                ;;
            *)
                echo "Package manager for '$OS' not defined in this script."
                exit 1
                ;;
        esac
    else
        echo "Exiting: $tool is required for this script."
        exit 1
    fi
}

# Confirm everything installed correctly
for tool in "${REQUIRED_TOOLS[@]}"; do
    if ! command -v "$tool" &> /dev/null; then
        install_tool "$tool"
    fi
done



# --- 0. Pre-installation Setup ---
echo "🎨 Customize your Terminal Color Palette"
echo "Select a primary color for your Starship prompt:"
echo "  1) Vibrant Orange (#FF8C00) [Default]"
echo "  2) Yellow         (#E6D709)"
echo "  3) Mint Green     (#50FA7B)"
echo "  4) Light Blue     (#8BE9FD)"
echo "  5) Pink           (#FF79C6)"
echo "  6) Red            (#FF5555)"
echo "  7) Purple         (#BD93F9)"
echo "  8) Cyan           (#00FFFF)"
echo "  9) Lime           (#32CD32)"
echo " 10) Blue           (#007BFF)"

read -p "Enter the number of your choice [1]: " color_choice

case $color_choice in
    2) PRIMARY_COLOR="#E6D709" ;;
    3) PRIMARY_COLOR="#50FA7B" ;;
    4) PRIMARY_COLOR="#8BE9FD" ;;
    5) PRIMARY_COLOR="#FF79C6" ;;
    6) PRIMARY_COLOR="#FF5555" ;;
    7) PRIMARY_COLOR="#BD93F9" ;;
    8) PRIMARY_COLOR="#00FFFF" ;;
    9) PRIMARY_COLOR="#32CD32" ;;
    10) PRIMARY_COLOR="#007BFF" ;;
    *) PRIMARY_COLOR="#FF8C00" ;; # Default
esac

echo -e "Selected hex color: $PRIMARY_COLOR\n"

# --- 2. Setup Fonts ---
echo -n "🔨 Syncing fonts... "
mkdir -p ~/.local/share/fonts

if [ -d "./fonts" ]; then
    changed=false

    for font in ./fonts/*.ttf; do
        dest="$HOME/.local/share/fonts/$(basename "$font")"
        if [ ! -f "$dest" ] || ! cmp -s "$font" "$dest"; then
            cp "$font" "$dest"
            changed=true
        fi
    done

    if [ "$changed" = true ]; then
        fc-cache -f > /dev/null 2>&1
        echo "Fonts updated."
    else
        echo "No changes."
    fi
else
    echo "No fonts folder found."
fi

# --- 3. Install Starship ---
echo -n "  Installing/Updating Starship... "
if curl -sS https://starship.rs/install.sh | sh -s -- -y > /dev/null 2>&1; then
    echo "Done."
else
    echo "Failed to install Starship."
fi

# --- 4. Setup Configs ---

# Fastfetch
echo -n "  Configuring Fastfetch... "
mkdir -p ~/.config/fastfetch
if [ -f "config.jsonc" ]; then
    cp -f config.jsonc ~/.config/fastfetch/config.jsonc && echo "Done."
else
    echo "Skipped (config.jsonc not found)."
fi

# Starship
echo -n "  Configuring Starship... "
mkdir -p ~/.config
if [ -f "starship.toml" ]; then
    cp -f starship.toml ~/.config/starship.toml
    sed -i "s/{{PRIMARY_COLOR}}/${PRIMARY_COLOR}/g" ~/.config/starship.toml
    echo "Done."
else
    echo "Failed (starship.toml not found)."
fi

# Configure Kitty
echo -n "󰄛  Configuring Kitty... "
mkdir -p ~/.config/kitty
if [ -f "kitty.conf" ]; then
    cp -f kitty.conf ~/.config/kitty/kitty.conf
    sed -i "s/{{PRIMARY_COLOR}}/${PRIMARY_COLOR}/g" ~/.config/kitty/kitty.conf
    echo "Done."
fi

# Bashrc
echo -n "󱆃  Applying .bashrc... "
if [ -f ".bashrc" ]; then
    if [ ! -L ~/.bashrc ] && [ ! -f ~/.bashrc.bak ]; then
        cp ~/.bashrc ~/.bashrc.bak
    fi
    cp -f .bashrc ~/.bashrc && echo "Done."
else
    echo "Skipped (.bashrc not found)."
fi

# --- 5. Refresh Kitty ---
echo -n "🔄 Refreshing Kitty config... "
pkill -USR1 kitty && echo "Done." || echo "Kitty not running."

echo "-----------------------------------------------"
echo "✅ All set! Terminal Restarting."
echo "-----------------------------------------------"