# Arup Guha
# 6/12/2024
# This file manages loading and displaying the high scores.

import random
import math
import time
import pygame, sys
from pygame.locals import *
import fruitgame

SCORE_W = 600
SCORE_H = 600
    
def show():

    # Get old scores.
    scorelist = fruitgame.loadscores("highscores.txt")

    # Basic Set Up
    pygame.init()
    font = pygame.font.SysFont("Arial", 36)
    DISPLAYSURF = pygame.display.set_mode((SCORE_W, SCORE_H))
    pygame.display.set_caption("Fruit Game High Scores")
    clock = pygame.time.Clock()

    # Button for getting back to main menu.
    button = pygame.Rect(SCORE_W//2-200,SCORE_H-100, 400, 100)

    # Main game loop starts here.
    while True:
        
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            # Looking for mouse button press.
            if event.type == MOUSEBUTTONDOWN:

                # Just look at left mouse button.
                if pygame.mouse.get_pressed()[0]:

                    pos = pygame.mouse.get_pos()

                    # We just finish this function and go to the main menu.
                    if button.collidepoint(pos):
                        return

        # Set up screen, button and button text.
        DISPLAYSURF.fill("white")
        pygame.draw.rect(DISPLAYSURF, "green", button)
        fruitgame.draw("Return to Main Menu", font, "black", DISPLAYSURF, SCORE_W//2-135, SCORE_H-70)
        
        # Display high scores.
        for i in range(len(scorelist)):
            fruitgame.draw(str(i+1)+". "+scorelist[i][1], font, "blue", DISPLAYSURF, 150,25+45*i)
            fruitgame.draw(str(scorelist[i][0]), font, "blue", DISPLAYSURF, 400,25+45*i)
        
        pygame.display.update()            
        clock.tick(30)

