import os
import requests
from tqdm import tqdm
import subprocess
import platform
import time

# URL of the .exe file
DOWNLOAD_URL = "https://www.googleapis.com/drive/v3/files/1Gti_oVBx_Q7i_Qv0XhdeRZo3kwfETfyT?alt=media&key=AIzaSyAA9ERw-9LZVEohRYtCWka_TQc6oXmvcVU&supportsAllDrives=True"

def print_with_delay(message, delay=1.5):
    """
    Prints a message with a delay for a chat-like experience.
    """
    print(message)
    time.sleep(delay)

def download_file(url, dest_folder="D:/hl-gaming"):
    """
    Downloads a file from the given URL with a progress bar.
    """
    # Create the destination folder if it doesn't exist
    if not os.path.exists(dest_folder):
        os.makedirs(dest_folder)

    local_filename = os.path.join(dest_folder, "hl_gaming_installer.exe")
    
    # Stream download and show progress
    response = requests.get(url, stream=True)
    total_size = int(response.headers.get('content-length', 0))
    with open(local_filename, "wb") as file, tqdm(
        desc="🎮 Downloading HL Gaming Installer...",
        total=total_size,
        unit="B",
        unit_scale=True,
        unit_divisor=1024,
    ) as bar:
        for chunk in response.iter_content(chunk_size=1024):
            file.write(chunk)
            bar.update(len(chunk))

    return local_filename

def run_installer(file_path):
    """
    Runs the downloaded .exe file.
    """
    if platform.system() == "Windows":
        try:
            print_with_delay("🚀 Launching the installer...")
            subprocess.run([file_path], check=True)
            print_with_delay("🎉 Installer completed successfully!")
        except subprocess.CalledProcessError as e:
            print(f"❌ Failed to launch installer: {e}")
    else:
        print_with_delay("⚠️ Sorry, the installer can only run on Windows systems.")

def main():
    """
    Main entry point for the package with a chatbot-like experience.
    """
    print_with_delay("🎮 Welcome to **HL Gaming Setup**! 🌟")
    print_with_delay("✨ Let's get you started with the ultimate gaming experience!")
    print_with_delay("🌐 Visit us at: https://hlgamingofficial.com 🕹️")

    # Ask for the user's name
    user_name = input("🤖 What's your name, gamer? 🕹️: ").strip()
    if not user_name:
        user_name = "Gamer"  # Default name if the user doesn't input one

    print_with_delay(f"🎉 Great to meet you, {user_name}! Let's make your gaming awesome!")
    
    # Ask if they are ready to install
    user_response = input(f"🤖 {user_name}, are you ready to install? (yes/no): ").strip().lower()
    if user_response not in ["yes", "y"]:
        print_with_delay(f"🙁 No worries, {user_name}! You can come back anytime. See you soon! 👋")
        return

    print_with_delay(f"👍 Awesome, {user_name}! Let's begin the installation process.")
    installer_path = download_file(DOWNLOAD_URL)
    print_with_delay(f"✅ Download complete. Starting the installer for you, {user_name}...")
    run_installer(installer_path)

    # End message
    print_with_delay(f"🎉 All done, {user_name}! Enjoy HL Gaming! See you online! 🌟")
