import random
import time
import sys
import os
import platform

# --- Universal color setup (works everywhere) ---
def enable_colors():
    system = platform.system().lower()
    
    # Check if we're in QPython (Android)
    is_qpython = 'android' in system or 'qpython' in sys.argv[0].lower()
    
    # Windows color setup
    if "windows" in system:
        try:
            os.system("")  # Enables ANSI on Windows 10+
            return "\033[91m", "\033[92m", "\033[93m", "\033[0m"
        except:
            return "", "", "", ""
    
    # QPython/Android - colors don't work, return empty strings
    if is_qpython:
        return "", "", "", ""
    
    # Linux, Mac, etc. - colors should work
    return "\033[91m", "\033[92m", "\033[93m", "\033[0m"

RED, GREEN, YELLOW, RESET = enable_colors()

# --- Universal message printer (handles colors gracefully) ---
def print_colored(text, color_type):
    if color_type == "red" and RED:
        print(RED + text + RESET)
    elif color_type == "green" and GREEN:
        print(GREEN + text + RESET)
    elif color_type == "yellow" and YELLOW:
        print(YELLOW + text + RESET)
    else:
        # Fallback with emoji for QPython/no colors
        if color_type == "red":
            print("🔴 " + text)
        elif color_type == "green":
            print("✅ " + text)
        elif color_type == "yellow":
            print("⚠️  " + text)
        else:
            print(text)

# --- Android-specific path generator with emphasis on SD card folders ---
def generate_fake_path():
    system = platform.system().lower()
    is_qpython = 'android' in system or 'qpython' in sys.argv[0].lower()
    
    if "windows" in system:
        base = "C:\\Windows\\System32\\"
        names = ["kernel32.dll", "user32.dll", "ntoskrnl.exe", "winlogon.exe", 
                 "lsass.exe", "drivers\\disk.sys", "ntdll.dll", "advapi32.dll"]
        return base + random.choice(names)
    
    elif is_qpython or "android" in system:
        # --- ANDROID SPECIFIC PATHS ---
        # Priority folders (will appear more often)
        priority_folders = [
            "/sdcard/DCIM/Camera/",
            "/sdcard/DCIM/Pictures/",
            "/sdcard/DCIM/Screenshots/",
            "/sdcard/qpython/scripts3/",
            "/sdcard/qpython/scripts/",
            "/sdcard/Download/",
            "/sdcard/Music/",
            "/sdcard/Movies/",
            "/sdcard/WhatsApp/Media/",
            "/sdcard/Android/data/"
        ]
        
        # System folders (less priority)
        system_folders = [
            "/system/bin/",
            "/system/lib/",
            "/data/data/",
            "/cache/"
        ]
        
        # File names for media folders
        media_files = [
            "IMG_{:04d}.jpg", "VID_{:04d}.mp4", "Screenshot_{}.png",
            "PXL_{}.jpeg", "WhatsApp_Image.jpg", "burnt_toast.jpg",
            "signal-{}.jpg", "telegram_photo.jpg", "gallery_{}.jpg",
            "recording_{}.m4a", "download_{}.bin"
        ]
        
        # QPython script files
        script_files = [
            "main.py", "script.py", "backup.py", "test.py",
            "myapp.py", "game.py", "tool.py", "scraper.py",
            "bot.py", "automation.py"
        ]
        
        # Decide which type of file to delete (80% priority folders, 20% system)
        if random.random() < 0.8:  # 80% chance for user data (scarier!)
            folder = random.choice(priority_folders)
            
            if "DCIM" in folder or "Pictures" in folder:
                # Media files
                if random.random() < 0.7:
                    file = random.choice(media_files)
                    # Add random numbers to make it realistic
                    random_num = random.randint(1000, 9999)
                    file = file.format(random_num, random_num, random_num)
                else:
                    file = f"media_{random.randint(1,9999)}.jpg"
            
            elif "qpython" in folder:
                # Script files
                file = random.choice(script_files)
            
            else:
                # Other user files
                extensions = [".jpg", ".png", ".mp4", ".mp3", ".pdf", ".docx", ".txt"]
                file = f"file_{random.randint(1,9999)}{random.choice(extensions)}"
            
            return folder + file
        
        else:  # System files
            folder = random.choice(system_folders)
            system_files = ["libcore.so", "app_process", "sh", "linker", "toybox", 
                           "servicemanager", "surfaceflinger", "logcat"]
            return folder + random.choice(system_files)
    
    else:
        # Linux/Mac
        base = "/usr/lib/"
        names = ["libc.so", "libm.so", "libdl.so", "libpthread.so", "ld-linux.so"]
        return base + random.choice(names)

# --- Special pause effect for important folders ---
def special_folder_animation(folder_path):
    print_colored(f"\n[!!!] ACCESSING: {folder_path}", "red")
    time.sleep(0.5)
    
    # Show a "scanning" effect
    for i in range(5):
        print_colored(f"    Scanning files... [{'█' * (i+1)}{'░' * (4-i)}]", "yellow")
        time.sleep(0.2)
    
    # Show fake file enumeration
    file_count = random.randint(50, 500)
    print_colored(f"    Found {file_count} files in {folder_path}", "yellow")
    time.sleep(0.8)
    
    # Dramatic pause
    print_colored(f"    DELETING ALL FILES IN {folder_path}...", "red")
    time.sleep(1.5)  # The big pause!
    
    # Rapid deletion simulation
    for i in range(min(20, file_count)):
        percentage = int((i+1) / min(20, file_count) * 100)
        sys.stdout.write(f"\r    Deleting files... [{percentage}%]")
        sys.stdout.flush()
        time.sleep(0.05)
    
    print()  # New line
    print_colored(f"    ✓ {file_count} files deleted from {folder_path}", "red")
    time.sleep(0.8)

# --- Main fake delete function ---
def fake_delete():
    print_colored("\n[!] CRITICAL ERROR: Incorrect guess!", "red")
    time.sleep(0.8)
    print_colored("[*] Initiating system wipe...", "yellow")
    time.sleep(0.8)

    # Check if running on Android
    system = platform.system().lower()
    is_android = 'android' in system or 'qpython' in sys.argv[0].lower()
    
    if is_android:
        print_colored("\n[*] TARGET DETECTED: Android Device", "yellow")
        print_colored("[*] ACCESSING STORAGE: /sdcard/", "yellow")
        time.sleep(1)
        
        # --- SPECIAL ANDROID FOLDER SEQUENCE ---
        # First, handle the priority folders with dramatic pauses
        priority_folders = [
            "/sdcard/DCIM/Pictures/",
            "/sdcard/qpython/scripts3/"
        ]
        
        for folder in priority_folders:
            special_folder_animation(folder)
        
        print_colored("\n[*] PROCEEDING TO SYSTEM FILES...", "yellow")
        time.sleep(1)
    
    SCROLL_DURATION = 7  # seconds
    start_time = time.time()
    counter = 0

    print_colored("\n[*] Deleting system files...\n", "yellow")
    
    # Detect if running on slow device (QPython)
    is_qpython = 'android' in system or 'qpython' in sys.argv[0].lower()
    
    while time.time() - start_time < SCROLL_DURATION:
        file = generate_fake_path()
        percentage = random.randint(1, 100)
        
        # Adjust speed based on platform
        if is_qpython:
            delay = random.uniform(0.05, 0.12)  # Slower for QPython
        elif "windows" in system:
            delay = random.uniform(0.02, 0.07)  # Fast for Windows
        else:
            delay = random.uniform(0.03, 0.08)  # Normal for others
        
        # Check if it's a priority folder for extra emphasis
        is_priority = False
        if is_android:
            if "/sdcard/DCIM/" in file or "/sdcard/qpython/" in file:
                is_priority = True
        
        # Message variations
        msg_type = random.choice(["delete", "corrupt", "overwrite"])
        
        # Extra emphasis for priority folders
        prefix = "⚠️  " if is_priority else ""
        
        if msg_type == "delete":
            print_colored(f"{prefix}Deleting {file} ... [{percentage}%]", "red")
        elif msg_type == "corrupt":
            print_colored(f"{prefix}Corrupting {file} ... [DATA LOSS]", "red")
        else:
            print_colored(f"{prefix}Overwriting {file} with random data ...", "red")
        
        counter += 1
        time.sleep(delay)
        
        # Small progress indicator every 100 lines
        if counter % 100 == 0:
            elapsed = int(time.time() - start_time)
            remaining = SCROLL_DURATION - elapsed
            print_colored(f"[*] Progress: {elapsed}/{SCROLL_DURATION} seconds, {counter} files processed ({remaining}s remaining)", "yellow")
    
    # Final scary messages
    print_colored("\n[!] SYSTEM BOOT SECTOR CORRUPTED!", "red")
    time.sleep(0.5)
    print_colored("[!] PARTITION TABLE LOST!", "red")
    time.sleep(0.5)
    print_colored("[!] MASTER BOOT RECORD OVERWRITTEN!", "red")
    
    if is_android:
        time.sleep(0.5)
        print_colored("[!] SD CARD PARTITION DAMAGED!", "red")
        time.sleep(0.5)
        print_colored("[!] /sdcard/DCIM/ - CORRUPTED!", "red")
        time.sleep(0.5)
        print_colored("[!] QPython scripts folder - PURGED!", "red")
    
    time.sleep(0.8)
    
    print_colored("\n[✓] Disk wipe complete! Your system is ready for reformatting!", "green")
    
    # Universal input prompt
    try:
        print("\n\033[33mYou lost!\033[0m Time to buy a new device 🤷‍♂️. Thanks for playing!!")
        input("\nPress Enter to start shopping...")
    except:
        pass

# --- Game entry point ---
def main():
    print_colored("=== WELCOME TO RUSSIAN ROULETTE ===", "yellow")
    time.sleep(0.5)
    
    number = random.randint(1, 10)
    
    try:
        guess = int(input("\nGuess a number between 1 and 10: "))
    except:
        print("Invalid input!")
        return
    
    if guess == number:
        print_colored("\n✅ ACCESS GRANTED - You Won!", "green")
    else:
        print_colored("\n❌ ACCESS DENIED - Incorrect guess!", "red")
        time.sleep(1)
        fake_delete()

# --- Run the game ---
if __name__ == "__main__":
    main()
