# Arup Guha
# 6/12/2024
# Previously edited by both Arup and Erick Verlangieri on 5/30/2022
# This the "main" file for the multi-screen fruit game.

import fruitgame
import highscores
import pygame, sys
from pygame.locals import *

# Initialize all imported pygame modules.
pygame.init()

# Create a custom caption that will be displayed on the top of your window of your game.
pygame.display.set_caption('Fruit Game!')

# Create a Font object from the system fonts. I chose size 100.
font = pygame.font.SysFont(None, 36)
bigfont = pygame.font.SysFont(None, 52)

# Create an object to help track time
clock = pygame.time.Clock()

def menu():

    # While true, so infinite loop.
    while True:

        # Creates a screen with width and height.
        screen = pygame.display.set_mode((fruitgame.SCREEN_W, fruitgame.SCREEN_H))

        # Fill surface with a white.
        screen.fill("white")

        # Get mouse position x and y.
        mx, my = pygame.mouse.get_pos()

        # Refers to the draw_text function I made above.
        # Requires text, font, color, surface, x, y.
        # This is how we draw text to screen. IMPORTANT!!
        fruitgame.draw('Menu', bigfont, "black", screen, 450, 20)

        # Three buttons for three choices.
        buttons = []
        buttons.append(pygame.Rect(300, 100, 400, 100))
        buttons.append(pygame.Rect(300, 250, 400, 100))
        buttons.append(pygame.Rect(300, 400, 400, 100))

        # Draws buttons onto screen
        for i in range(len(buttons)):
            pygame.draw.rect(screen, "cyan", buttons[i])

        # Creates and draws text on buttons.
        fruitgame.draw("Play Fruit Game", font, "black", screen, 400, 135)
        fruitgame.draw("See High Scores", font, "black", screen, 410, 285)
        fruitgame.draw("Quit", font, "black", screen, 480, 435)
        
        # Get events from the queue
        for event in pygame.event.get():

            # If you close the game.
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            # If you press down a key.
            if event.type == KEYDOWN:
                
                # If that key was the escape button do this.
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()

            # If you click on screen.
            if event.type == MOUSEBUTTONDOWN:
                
                # 1 - left click, 2 - middle click, 3 - right click, 4 - scroll up, 5 - scroll down
                if event.button == 1:
                    
                    # Rectangle to play the fruit game.
                    if buttons[0].collidepoint((mx, my)):
                        fruitgame.main()

                    # To see high scores.
                    if buttons[1].collidepoint((mx, my)):
                        highscores.show()

                    # Normal way to quit.
                    if buttons[2].collidepoint((mx, my)):
                        pygame.quit()
                        sys.exit()

        # Update and wait.
        pygame.display.update()
        clock.tick(30)


# Loads main menu.
menu()
