import pygame
import random

# ===== Initialization =====
pygame.init()
WIDTH, HEIGHT = 700, 500
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rock ✂ Paper — Rock Paper Scissors")

# ===== Colors =====
BG = (25, 25, 35)
BTN = (60, 130, 190)
BTN_HOVER = (90, 160, 230)
WHITE = (240, 240, 240)
GRAY = (160, 160, 160)
WIN_COLOR = (100, 220, 120)
LOSE_COLOR = (220, 100, 100)
DRAW_COLOR = (220, 220, 100)

FONT = pygame.font.Font(None, 36)
FONT_SM = pygame.font.Font(None, 24)
FONT_BIG = pygame.font.Font(None, 48)

# ===== Data =====
CHOICES = ["Rock", "Scissors", "Paper"]
ICONS = {"Rock": "✊", "Scissors": "✌", "Paper": "🖐"}

RULES = {
    ("Rock", "Scissors"): "Win",
    ("Scissors", "Paper"): "Win",
    ("Paper", "Rock"): "Win",
}

player_score = 0
computer_score = 0
round_result = ""
player_choice = ""
computer_choice = ""

# ===== Buttons =====
buttons = []
for i, choice in enumerate(CHOICES):
    rect = pygame.Rect(80 + i * 200, 320, 160, 60)
    buttons.append((rect, choice))


def judge(player, computer):
    if player == computer:
        return "Draw"
    return RULES.get((player, computer), "Lose")


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

# ===== Main Loop =====
while running:
    screen.fill(BG)

    mouse_pos = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            for rect, choice in buttons:
                if rect.collidepoint(event.pos):
                    player_choice = choice
                    computer_choice = random.choice(CHOICES)
                    round_result = judge(player_choice, computer_choice)

                    if round_result == "Win":
                        player_score += 1
                    elif round_result == "Lose":
                        computer_score += 1

    # ===== Title =====
    title = FONT_BIG.render("🪨 Rock ✂ Scissors 📄 Paper", True, WHITE)
    screen.blit(title, (WIDTH // 2 - title.get_width() // 2, 20))

    # ===== Choices Display =====
    p_text = FONT.render(
        f"You: {ICONS.get(player_choice, '?')} {player_choice}", True, WHITE
    )
    c_text = FONT.render(
        f"Computer: {ICONS.get(computer_choice, '?')} {computer_choice}", True, WHITE
    )
    screen.blit(p_text, (80, 120))
    screen.blit(c_text, (380, 120))

    # ===== Round Result =====
    if round_result:
        color = (
            WIN_COLOR if round_result == "Win"
            else LOSE_COLOR if round_result == "Lose"
            else DRAW_COLOR
        )
        result_text = FONT.render(
            f"Result: {'You ' + round_result if round_result != 'Draw' else 'Draw'}",
            True,
            color,
        )
        screen.blit(result_text, (WIDTH // 2 - result_text.get_width() // 2, 170))

    # ===== Score =====
    score_text = FONT.render(
        f"Score   You {player_score} : {computer_score} Computer", True, GRAY
    )
    screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 220))

    # ===== Buttons =====
    for rect, choice in buttons:
        hover = rect.collidepoint(mouse_pos)
        pygame.draw.rect(screen, BTN_HOVER if hover else BTN, rect, border_radius=12)
        pygame.draw.rect(screen, GRAY, rect, 2, border_radius=12)
        btn_text = FONT.render(f"{ICONS[choice]} {choice}", True, WHITE)
        screen.blit(btn_text, btn_text.get_rect(center=rect.center))

    # ===== Hint =====
    hint = FONT_SM.render("Click a button to play ↓", True, GRAY)
    screen.blit(hint, (WIDTH // 2 - hint.get_width() // 2, 290))

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

pygame.quit()