# Arup Guha
# 6/23/2025
# A basic running minesweeper.

import random
import time
import pygame, sys
import minesweeper
import minefunc
from pygame.locals import *
from minesweeper import mineclass

# Screen Parameters.
SCREEN_W = 500
SCREEN_H = 600
 
def main():

    # 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('Minesweeper')

    # Create a Font object from the system fonts. I chose size 100.
    font = pygame.font.SysFont(None, 36)

    # Create an object to help track time
    clock = pygame.time.Clock()

    # Creates a screen with width and height.
    screen = pygame.display.set_mode((SCREEN_W, SCREEN_H))

    # All of these will be set when the game starts.
    myMine = None
    status = minefunc.NOT_STARTED
    endT = -1
    curT = -1
    startT = -1

    # While true, so infinite loop.
    while True:

        # Fill surface with a white.
        screen.fill("white")
        
        # 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:

                if pygame.mouse.get_pressed()[0]:

                    # Get the clicked square.
                    sq = minefunc.getXY(event.pos)

                    # Start the game!
                    if myMine == None and sq != None:

                        # Now this is our game!
                        myMine = mineclass(sq[0], sq[1])
                        myMine.move(sq[0], sq[1])
                        status = minefunc.CONT
                        startT = time.time()

                    # Valid move in the game.
                    elif myMine != None and sq != None:
                        status = myMine.move(sq[0], sq[1])

        # Now draw the board.
        minefunc.drawEmptyBoard(screen)
        if myMine != None:
            myMine.drawVisible(screen, minefunc.TOP_X, minefunc.TOP_Y, minefunc.STEP)

        # Calculate to display current game time.
        if startT > 0 and status == minefunc.CONT:
            curT = int(time.time() - startT)

        # Draw only if game is running.
        if startT > 0:
            minefunc.draw("Time: "+str(curT), font, 'black', screen, SCREEN_W-200, 10)

        # Player lost.
        if status == minefunc.LOST:
            minefunc.draw("You lose!", font, 'red', screen, 10, 10)
            if endT == -1:
                endT = time.time()
           
        # Here's for winning.
        elif status == minefunc.WIN:
            minefunc.draw("You win in "+str(curT)+" seconds!", font, 'red', screen, 10, 10)
            if endT == -1:
                endT = time.time()

        # So it doesn't disappear right away.
        if endT > 0 and time.time()-endT > 5:
            pygame.quit()
            sys.exit()
            
        # Update and wait.
        pygame.display.update()
        clock.tick(30)

# Plays game.
main()
