import pygame
import sys
import random

pygame.init()

WINDOW_WIDTH = 900
WINDOW_HEIGHT = 700
BACKGROUND_COLOR = (30, 30, 50)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 80)
GRAY = (100, 100, 100)


class Lottery:
    def __init__(self):
        self.items = ["Trip", "Movie", "Dinner",
                      "Gift", "Sport", "Study", "Sleep", "Book"]
        self.result = None
        self.drawing = False
        self.current_index = 0
        self.animation_speed = 20
        self.animation_counter = 0
        self.history = []

    def start_draw(self):
        if not self.drawing and self.items:
            self.drawing = True
            self.animation_speed = 20
            self.animation_counter = 0
            self.result = None
            return True
        return False

    def update_animation(self):
        if self.drawing:
            self.animation_counter += 1
            if self.animation_counter >= max(1, int(20 / self.animation_speed)):
                self.animation_counter = 0
                self.current_index = (self.current_index + 1) % len(self.items)

                if self.animation_speed > 2:
                    self.animation_speed -= 0.5
                else:
                    self.drawing = False
                    self.result = self.items[self.current_index]
                    self.history.append(self.result)
                    if len(self.history) > 10:
                        self.history.pop(0)
                    return True
        return False


def main():
    screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption("Lottery Game")
    clock = pygame.time.Clock()

    lottery = Lottery()
    font_big = pygame.font.Font(None, 72)
    font_medium = pygame.font.Font(None, 48)
    font_small = pygame.font.Font(None, 32)

    running = True
    while running:
        mouse_pos = pygame.mouse.get_pos()
        mouse_click = pygame.mouse.get_pressed()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                button_rect = pygame.Rect(WINDOW_WIDTH//2 - 80, 450, 160, 60)
                if button_rect.collidepoint(mouse_pos) and not lottery.drawing:
                    lottery.start_draw()

        lottery.update_animation()

        screen.fill(BACKGROUND_COLOR)

        # Title
        title = font_medium.render("Lottery Game", True, YELLOW)
        title_rect = title.get_rect(center=(WINDOW_WIDTH//2, 50))
        screen.blit(title, title_rect)

        # Result box
        result_rect = pygame.Rect(WINDOW_WIDTH//2 - 200, 120, 400, 200)
        pygame.draw.rect(screen, (60, 60, 80), result_rect)
        pygame.draw.rect(screen, WHITE, result_rect, 3)

        if lottery.drawing:
            current = lottery.items[lottery.current_index] if lottery.items else "Empty"
            text = font_big.render(current, True, YELLOW)
        elif lottery.result:
            text = font_big.render(lottery.result, True, YELLOW)
        else:
            text = font_big.render("Click Draw", True, GRAY)

        text_rect = text.get_rect(center=result_rect.center)
        screen.blit(text, text_rect)

        # Draw button
        button_rect = pygame.Rect(WINDOW_WIDTH//2 - 80, 450, 160, 60)
        if button_rect.collidepoint(mouse_pos):
            pygame.draw.rect(screen, (100, 160, 230), button_rect)
        else:
            pygame.draw.rect(screen, (70, 130, 200), button_rect)
        pygame.draw.rect(screen, BLACK, button_rect, 3)
        button_text = font_medium.render("DRAW", True, WHITE)
        button_text_rect = button_text.get_rect(center=button_rect.center)
        screen.blit(button_text, button_text_rect)

        # Items list
        list_rect = pygame.Rect(50, 550, 800, 100)
        pygame.draw.rect(screen, (60, 60, 80), list_rect)
        pygame.draw.rect(screen, WHITE, list_rect, 2)

        items_text = font_small.render(
            "Items: " + ", ".join(lottery.items), True, WHITE)
        screen.blit(items_text, (60, 575))

        # History
        if lottery.history:
            history_text = font_small.render(
                "History: " + " -> ".join(lottery.history[-5:]), True, GRAY)
            screen.blit(history_text, (60, 620))

        pygame.display.flip()
        clock.tick(60)

    pygame.quit()
    sys.exit()


if __name__ == "__main__":
    main()
