# Import standard python modules
import os

# Import third-party python modules.
import pygame
pygame.init()

# Import own modules.
from game.classes.Preferences import *
from game.components import helpers
from game.components import object_creation
from game.classes.ButtonMenu import ButtonMenu


def run( playground, clock, caterpillars, jukebox ):
	"""
	"""
	# Set up the labels. 
	labels = object_creation.create_labels( GAME_OVER_MENU["labels"] )
	# Set up the buttons.
	buttons = object_creation.create_buttons( GAME_OVER_MENU["buttons"] )
	button_menu1 = ButtonMenu( buttons )

	the_highscore = 0
	for l in labels:
		if l._unique_name == "lbl_the_score":
			the_highscore = helpers.calculate_score( caterpillars )
			l.set_text( str( the_highscore ) )

	# Get the name of the player / team.
	the_name = helpers.load_options( VAR_OPTS_FILENAME )["name"]
	
	# First, update the highscore table.  Make sure the file exists.
	if not os.path.exists( DEFAULT_HIGHSCORE_TABLES[len( caterpillars )]["filename"] ):
		# Create the default file.
		helpers.init_options( DEFAULT_HIGHSCORE_TABLES[len( caterpillars )]["filename"], DEFAULT_HIGHSCORE_TABLES[len( caterpillars )]["data"] )
	# Open the file and read the highscores.
	highscores = helpers.load_options( DEFAULT_HIGHSCORE_TABLES[len( caterpillars )]["filename"] )

	# Now update the highscores.
	highscores = helpers.update_highscores( highscores, the_name, the_highscore )

	# Now write the highscores to the file again.
	helpers.save_options( DEFAULT_HIGHSCORE_TABLES[len( caterpillars )]["filename"], highscores )

	running = True
	result = None

	while running:

		events = pygame.event.get()
		if helpers.user_quit( events ):
			running = False

		choice = button_menu1.update( events )
		if choice is not None:
			running = False

		playground.fill( WHITE )

		labels.draw( playground )
		button_menu1.draw( playground )

		clock.tick( FPS )
		pygame.display.update()

		# Music still playing?
		jukebox.still_playing()			


	return choice
