import pygame
import sys

pygame.init()
WIDTH = 880
HEIGHT = 640
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Spend Musk's Fortune")

# Colors
BG = (22, 28, 45)
PANEL = (35, 42, 60)
GOLD = (255, 210, 0)
WHITE = (255,255,255)
GRAY = (140,140,160)
RED = (230, 60, 60)
GREEN = (35, 190, 80)
BLUE = (40, 140, 220)
PURPLE = (140,60,190)
BLACK = (0,0,0)

# Font (compatible with Turtle Editor)
font_big = pygame.font.Font(None, 46)
font_mid = pygame.font.Font(None, 34)
font_small = pygame.font.Font(None, 26)

# Initial money: 997 billion USD
money = 997 * 10**9
game_win = False

# Item list
shop_items = [
    {"name":"Cola", "price":1, "color":(220,40,40)},
    {"name":"Milk Tea", "price":15, "color":(220,160,80)},
    {"name":"Smartphone", "price":5999, "color":(80,80,80)},
    {"name":"Gaming PC", "price":18000, "color":(60,120,220)},
    {"name":"Tesla Sedan", "price":260000, "color":(30,160,70)},
    {"name":"Luxury Sports Car", "price":2800000, "color":(220,40,80)},
    {"name":"City Penthouse", "price":12000000, "color":(120,90,180)},
    {"name":"Beach Villa", "price":45000000, "color":(80,160,190)},
    {"name":"Private Yacht", "price":320000000, "color":(20,120,180)},
    {"name":"Private Jet", "price":950000000, "color":(100,100,110)},
    {"name":"Small Oilfield", "price":12000000000, "color":(100,70,30)},
    {"name":"Falcon Rocket", "price":62000000000, "color":(190,40,40)},
    {"name":"Artificial Satellite", "price":90000000000, "color":(160,160,200)},
    {"name":"Private Island", "price":160000000000, "color":(40,180,130)},
    {"name":"Small Airline", "price":280000000000, "color":PURPLE},
]

# Batch buttons
batch_btn_10 = pygame.Rect(620, 30, 110, 48)
batch_btn_100 = pygame.Rect(750, 30, 110, 48)
batch_mode = 1

buttons = []
btn_w = 180
btn_h = 72
gap_x = 20
gap_y = 16
start_x = 30
start_y = 160

for idx, item in enumerate(shop_items):
    row = idx // 4
    col = idx % 4
    x = start_x + col * (btn_w + gap_x)
    y = start_y + row * (btn_h + gap_y)
    buttons.append({"rect":pygame.Rect(x,y,btn_w,btn_h), "data":item})

clock = pygame.time.Clock()
running = True

def format_money(num):
    if num >= 10**12:
        return f"{num / 10**12:.2f} Trillion"
    elif num >= 10**9:
        return f"{num / 10**9:.2f} Billion"
    elif num >= 10**6:
        return f"{num / 10**6:.2f} Million"
    elif num >= 10000:
        return f"{num / 10000:.2f} TenK"
    else:
        return f"{num:.2f}"

while running:
    screen.fill(BG)
    mx, my = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN and not game_win:
            if event.button == 1:
                if batch_btn_10.collidepoint(mx, my):
                    batch_mode = 10
                if batch_btn_100.collidepoint(mx, my):
                    batch_mode = 100
                for btn in buttons:
                    if btn["rect"].collidepoint(mx, my):
                        cost = btn["data"]["price"] * batch_mode
                        if money >= cost:
                            money -= cost
                            if money <= 0:
                                money = 0
                                game_win = True

    # Top panel
    top_panel = pygame.Rect(20, 20, WIDTH-40, 110)
    pygame.draw.rect(screen, PANEL, top_panel, border_radius=10)
    pygame.draw.rect(screen, GOLD, top_panel, 2, border_radius=10)
    money_text = font_big.render(f"Remaining Wealth: ${format_money(money)}", True, GOLD)
    screen.blit(money_text, (35, 32))
    tip_text = font_small.render("Click items to spend money! Spend all to win!", True, GRAY)
    screen.blit(tip_text, (35, 82))

    # Batch buttons
    color10 = GREEN if batch_mode ==10 else GRAY
    color100 = RED if batch_mode ==100 else GRAY
    pygame.draw.rect(screen, color10, batch_btn_10, border_radius=6)
    pygame.draw.rect(screen, WHITE, batch_btn_10, 2, border_radius=6)
    pygame.draw.rect(screen, color100, batch_btn_100, border_radius=6)
    pygame.draw.rect(screen, WHITE, batch_btn_100, 2, border_radius=6)
    t10 = font_small.render("×10", True, WHITE)
    t100 = font_small.render("×100", True, WHITE)
    screen.blit(t10, t10.get_rect(center=batch_btn_10.center))
    screen.blit(t100, t100.get_rect(center=batch_btn_100.center))

    # Draw item buttons
    for btn in buttons:
        r = btn["rect"]
        item = btn["data"]
        pygame.draw.rect(screen, item["color"], r, border_radius=8)
        pygame.draw.rect(screen, WHITE, r, 2, border_radius=8)
        name_txt = font_mid.render(item["name"], True, WHITE)
        price_txt = font_small.render(f"${format_money(item['price'])}", True, WHITE)
        screen.blit(name_txt, name_txt.get_rect(center=(r.centerx, r.centery-14)))
        screen.blit(price_txt, price_txt.get_rect(center=(r.centerx, r.centery+16)))

    # Win overlay
    if game_win:
        overlay = pygame.Surface((WIDTH, HEIGHT))
        overlay.set_alpha(150)
        overlay.fill(BLACK)
        screen.blit(overlay, (0,0))
        win_txt = font_big.render("🎉 Congratulations! You spent all Musk's money!", True, GOLD)
        restart_txt = font_mid.render("Close window to restart", True, WHITE)
        screen.blit(win_txt, win_txt.get_rect(center=(WIDTH//2, HEIGHT//2 - 40)))
        screen.blit(restart_txt, restart_txt.get_rect(center=(WIDTH//2, HEIGHT//2 + 20)))

    pygame.display.update()
    clock.tick(60)

pygame.quit()
sys.exit()