# Leo Salazar
# 6/6/2024
# Mouse Input Example (Drag and Drop sorting game)

import pygame
import random

pygame.init()

# set up
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
items = 10
done = False

# makes a font object with default font style, and size 60
font = pygame.font.Font(None, 60)

# sets up the phrase that will be displayed
message = font.render("Complete!", True, "yellow")
left_side = font.render("Ellipses", True, "red")
right_side = font.render("Rectangles", True, "red")

# returns the rectangle object for each message
comp_rect = message.get_rect()
left_rect = left_side.get_rect()
right_rect = right_side.get_rect()

# sets up the position of the text box
comp_rect.center = (screen.get_width()/2, 10 + comp_rect.height/2)
left_rect.center = (screen.get_width()/4, 10 + left_rect.height/2)
right_rect.center = ((screen.get_width()/4) * 3, + right_rect.height/2)

color = pygame.color.Color(255,255,255)

# set up a variable to keep track of moving item and list of objects
active_item = None
stuff = []

# this makes items number of Rect objects and stores them in the stuff array
for i in range(items):
    w = random.randint(30, 60)
    h = random.randint(30, 60)
    x = random.randint(10, screen.get_width() - w - 10)
    y = random.randint(10, screen.get_height() - h - 10)
    box = pygame.Rect(x, y, w, h)
    stuff.append(box)

# checks to see if the rectangles and ellipses are in the correct side
def checkDone():
    correct = True
    for i in range(len(stuff)//2):
        correct &= stuff[i].right > screen.get_width()/2
    for i in range(len(stuff)//2, len(stuff)):
        correct &= stuff[i].left < screen.get_width()/2

    return correct

while running:

    for event in pygame.event.get():

        # escapes the game loop
        if event.type == pygame.QUIT:
            running = False

        # checks to see if when mouse button is clicked, a box is clicked and marks it as the active box
        if event.type == pygame.MOUSEBUTTONDOWN:
            for num, box in enumerate(stuff):
                if box.collidepoint(event.pos):
                    active_item = num
        
        # if the left click is released, remove the box as the active box
        if event.type == pygame.MOUSEBUTTONUP:
            active_item = None
            done = checkDone()

        # if the mouse is being moved and there is an active box,
        # move the box the same x and y direction as the mouse
        if event.type == pygame.MOUSEMOTION:
            if active_item != None:
                stuff[active_item].move_ip(event.rel)

    screen.fill("black")

    pygame.draw.line(screen, "white", (screen.get_width()/2, 0), (screen.get_width()/2, screen.get_height()))

    # this loops through the list of boxes and draws them all
    for i in range(len(stuff)//2):
        pygame.draw.rect(screen, "white", stuff[i])
    for i in range(len(stuff)//2, len(stuff)):
        pygame.draw.ellipse(screen, "white", stuff[i])

    # displays complete message when shapes have been sorted
    if done:
        screen.blit(message, comp_rect)

    # displays the text box on the screen
    screen.blit(left_side, left_rect)
    screen.blit(right_side, right_rect)

    # updates the screen
    pygame.display.flip()

# quits the game
pygame.quit()