# Arup Guha
# 6/19/2024
# Wordle Main File

import random
import math
import time
import pygame, sys
from pygame.locals import *
from wordlefile import wordle
import wordlefunctions

# Screen size
SCREEN_W = 480
SCREEN_H = 600

def main():

    # Basic Set Up
    pygame.init()
    font = pygame.font.SysFont("Arial", 36)
    DISPLAYSURF = pygame.display.set_mode((SCREEN_W, SCREEN_H))
    pygame.display.set_caption("Play Wordle!")

    # Create wordle object
    mywordle = wordle("wordlewords.txt")

    # Original status (still playing!)
    mystatus = mywordle.getstatus()

    # For either quitting or playing a new game.
    font = pygame.font.SysFont("Arial", 36)
    quitbutton = pygame.Rect(300, 550, 70, 45)
    resetbutton = pygame.Rect(100, 550, 110, 45)

    # Main game loop starts here.
    while True:
        
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            # We're active and looking for a keypress.
            if event.type == KEYDOWN and mystatus == wordle.PLAYING:

                # Add a valid letter.
                if event.key >= K_a and event.key <= K_z:
                    mywordle.updatecurstr(event.unicode)

                # Undo a letter.
                if event.key == K_DELETE or event.key == K_BACKSPACE:
                    mywordle.backspace()

                # Process a word and update the status.
                if event.key == K_RETURN:
                    mywordle.updateguess()
                    mystatus = mywordle.getstatus()

            # Just for quitting.
            if event.type == MOUSEBUTTONDOWN and mystatus != wordle.PLAYING:

                # Get where it happened.
                pos = pygame.mouse.get_pos()

                # Hit a button.
                if event.button == 1:

                    # Time to quit.
                    if quitbutton.collidepoint(pos):
                        pygame.quit()
                        sys.exit()

                    # More wordle!
                    if resetbutton.collidepoint(pos):
                        mywordle = wordle("wordlewords.txt")
                        mystatus = mywordle.getstatus()
                        

        # Always draw the board and letters.
        wordlefunctions.drawEmptyBoard()
        mywordle.drawLetters(DISPLAYSURF)

        # Only show on screen after a game finishes before quit or restart.
        if mystatus != wordle.PLAYING:
            pygame.draw.rect(DISPLAYSURF,"cyan", resetbutton)
            wordlefunctions.draw("Restart",font,"black",DISPLAYSURF,110, 550)
            pygame.draw.rect(DISPLAYSURF,"cyan", quitbutton)
            wordlefunctions.draw("Quit",font,"black",DISPLAYSURF,310, 550)
            
        # Only show with win.
        if mystatus == wordle.WIN:
            wordlefunctions.draw("You got it, you win!", font, "black", DISPLAYSURF, 100, 500)

        # Only show with loss.
        if mystatus == wordle.LOSE:
            wordlefunctions.draw("You lose. Word was "+mywordle.solution+".", font, "black", DISPLAYSURF, 75, 500)
        
        pygame.display.update()

# Run it.
main()
        
                    
