import pygame
import random

pygame.init()
WIDTH, HEIGHT = 1020, 680
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shawarma Legend")
clock = pygame.time.Clock()

# Color Palette
BG = (26, 48, 64)
TABLE = (102, 74, 52)
PLATE_COLOR = (242, 234, 220)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (224, 42, 42)
GREEN = (32, 168, 64)
GOLD = (255, 206, 28)
ORANGE = (255, 126, 22)
DARK_GRAY = (44, 44, 44)
HIGHLIGHT = (255, 230, 80)

# Font setup
try:
    font_large = pygame.font.SysFont("Arial", 36)
    font_mid = pygame.font.SysFont("Arial", 26)
    font_small = pygame.font.SysFont("Arial", 20)
except:
    font_large = pygame.font.Font(None, 36)
    font_mid = pygame.font.Font(None, 26)
    font_small = pygame.font.Font(None, 20)

# Ingredient names
MEAT = "Meat"
CUCUMBER = "Pickle"
FRIES = "Fries"
SAUCE = "Garlic Sauce"
ingredients = [MEAT, CUCUMBER, FRIES, SAUCE]

# Button Rectangles
btn_cut_meat = pygame.Rect(40, 140, 110, 70)
plate_rect = pygame.Rect(180, 460, 620, 160)
bread_rect = pygame.Rect(200, 480, 130, 110)
serve_btn = pygame.Rect(840, 480, 130, 90)

# Ingredient Trays 【4个托盘位置】
trays = {
    MEAT: pygame.Rect(190, 320, 120, 80),
    CUCUMBER: pygame.Rect(340, 320, 120, 80),
    FRIES: pygame.Rect(490, 320, 120, 80),
    SAUCE: pygame.Rect(640, 320, 120, 80),
}

# Game State
gold = 0
stock = {MEAT: 0, CUCUMBER: 12, FRIES: 12, SAUCE: 12}
current_shawarma = []
has_bread = False
customers = []
spawn_timer = 0

class Customer:
    def __init__(self):
        self.order = random.sample(ingredients, k=random.randint(2, 4))
        self.patience = 320
        self.max_patience = 320
        self.x = 820
        self.y = 160
        self.leave = False

    def draw(self):
        pygame.draw.ellipse(screen, (184, 162, 140), (self.x, self.y, 60, 80))
        pygame.draw.circle(screen, (245, 215, 185), (self.x + 30, self.y - 12), 22)
        bar_width = 54 * (self.patience / self.max_patience)
        pygame.draw.rect(screen, DARK_GRAY, (self.x + 3, self.y - 32, 54, 8), border_radius=3)
        pygame.draw.rect(screen, GREEN, (self.x + 3, self.y - 32, bar_width, 8), border_radius=3)
        order_text = font_small.render(" + ".join(self.order), True, WHITE)
        screen.blit(order_text, (self.x, self.y - 55))

def spawn_customer():
    if len(customers) < 3:
        customers.append(Customer())

running = True
while running:
    screen.fill(BG)
    mouse_x, mouse_y = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            cx, cy = event.pos
            # Cut Meat
            if btn_cut_meat.collidepoint(cx, cy):
                stock[MEAT] += 1
            # Place bread
            if bread_rect.collidepoint(cx, cy) and not has_bread:
                has_bread = True
                current_shawarma.clear()
            # Add ingredients
            for food, rect in trays.items():
                if rect.collidepoint(cx, cy) and stock[food] > 0 and has_bread:
                    stock[food] -= 1
                    current_shawarma.append(food)
            # Serve button
            if serve_btn.collidepoint(cx, cy) and has_bread and len(customers) > 0:
                target_customer = customers[0]
                if sorted(current_shawarma) == sorted(target_customer.order):
                    gold += 12
                else:
                    gold += 4
                customers.pop(0)
                has_bread = False
                current_shawarma.clear()

    # Spawn customer timer
    spawn_timer += 1
    if spawn_timer > 420:
        spawn_customer()
        spawn_timer = 0

    # Draw worktop
    pygame.draw.rect(screen, TABLE, (30, 280, 960, 360), border_radius=12)

    # Cut Meat Button
    pygame.draw.rect(screen, ORANGE, btn_cut_meat, border_radius=10)
    text_cut = font_mid.render("Cut Meat", True, WHITE)
    screen.blit(text_cut, (btn_cut_meat.x + 14, btn_cut_meat.y + 12))

    # Ingredient Trays 【增加黄色边框，一眼看见托盘】
    for name, rect in trays.items():
        pygame.draw.rect(screen, PLATE_COLOR, rect, border_radius=10)
        pygame.draw.rect(screen, HIGHLIGHT, rect, 3, border_radius=10)
        label = font_small.render(f"{name}: {stock[name]}", True, BLACK)
        screen.blit(label, (rect.x + 10, rect.y + 28))

    # Assembly plate
    pygame.draw.rect(screen, PLATE_COLOR, plate_rect, border_radius=12)
    if has_bread:
        pygame.draw.ellipse(screen, (248, 222, 172), bread_rect)
        contents = font_small.render(" + ".join(current_shawarma), True, BLACK)
        screen.blit(contents, (bread_rect.x + 10, bread_rect.y + 44))
    else:
        tip_bread = font_small.render("Click here for flatbread", True, BLACK)
        screen.blit(tip_bread, (bread_rect.x+5, bread_rect.y))

    # Serve Button
    pygame.draw.rect(screen, GREEN, serve_btn, border_radius=10)
    serve_text = font_mid.render("Serve", True, WHITE)
    screen.blit(serve_text, (serve_btn.x + 36, serve_btn.y + 22))

    # Update customer patience
    for cus in customers:
        cus.patience -= 1
        if cus.patience <= 0:
            cus.leave = True
    customers = [c for c in customers if not c.leave]

    # Draw all customers
    for guest in customers:
        guest.draw()

    # Top UI
    gold_text = font_large.render(f"💰 Gold: {gold}", True, GOLD)
    screen.blit(gold_text, (22, 22))
    hint = font_small.render("Step: Cut Meat → Click flatbread → Click YELLOW BORDER trays to add food → Serve", True, WHITE)
    screen.blit(hint, (22, 70))

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

pygame.quit()