import pygame
import simple_snake
import something
import animation 

def main():

	pygame.init()
	 
	WHITE = (255, 255, 255)
	RED = (255, 0, 0)
	BLACK = (0, 0, 0)
	 
	class MenuItem(pygame.font.Font):
	    def __init__(self, text, game, font=None, font_size=30,
		         font_color=WHITE, (pos_x, pos_y)=(0, 0)):
		pygame.font.Font.__init__(self, font, font_size)
		self.text = text
		print "TEXT", text
		self.font_size = font_size
		self.font_color = font_color
		self.label = self.render(self.text, 1, self.font_color)
		self.width = self.label.get_rect().width
		self.height = self.label.get_rect().height
		self.dimensions = (self.width, self.height)
		self.pos_x = pos_x
		self.pos_y = pos_y
		self.position = pos_x, pos_y
		self.is_selected = False
		self.game = game 
	    def set_position(self, x, y):
		self.position = (x, y)
		self.pos_x = x
		self.pos_y = y
	 
	    def set_font_color(self, rgb_tuple):
		self.font_color = rgb_tuple
		self.label = self.render(self.text, 1, self.font_color)
	 
	    def is_mouse_selection(self, (posx, posy)):
		if (posx >= self.pos_x and posx <= self.pos_x + self.width) and \
		    (posy >= self.pos_y and posy <= self.pos_y + self.height):
		        return True
		return False
	 
	 
	class GameMenu():
	    def __init__(self, screen, items, bg_color=BLACK, font=None, font_size=30,
		            font_color=WHITE):
		self.screen = screen
		self.scr_width = self.screen.get_rect().width
		self.scr_height = self.screen.get_rect().height
	 
		self.bg_color = bg_color
		self.clock = pygame.time.Clock()
	 
		self.items = []
		for index, item in enumerate(items):
		    game = item[1]
		    menu_item = MenuItem(item[0], game,font, font_size, font_color)#, '/home/nebelhom/.fonts/SHOWG.TTF')
	 
		    # t_h: total height of text block
		    t_h = len(items) * menu_item.height
		    pos_x = (self.scr_width / 2) - (menu_item.width / 2)
		    # This line includes a bug fix by Ariel (Thanks!)
		    # Please check the comments section of pt. 2 for an explanation
		    pos_y = (self.scr_height/2)-(t_h/2)+((index*2)+index*menu_item.height)
	 
		    menu_item.set_position(pos_x, pos_y)
		    self.items.append(menu_item)
	 
		self.mouse_is_visible = True
		self.cur_item = None
	 
	    def set_mouse_visibility(self):
		if self.mouse_is_visible:
		    pygame.mouse.set_visible(True)
		else:
		    pygame.mouse.set_visible(False)
	 
	    def set_item_selection(self, key):
		"""
		Marks the MenuItem chosen via up and down keys.
		"""
		for item in self.items:
		    # Return all to neutral
		    item.set_italic(False)
		    item.set_font_color(WHITE)
	 
		if self.cur_item is None:
		    self.cur_item = 0
		else:
		    # Find the chosen item
		    if key == pygame.K_UP and \
		            self.cur_item > 0:
		        self.cur_item -= 1
		    elif key == pygame.K_UP and \
		            self.cur_item == 0:
		        self.cur_item = len(self.items) - 1
		    elif key == pygame.K_DOWN and \
		            self.cur_item < len(self.items) - 1:
		        self.cur_item += 1
		    elif key == pygame.K_DOWN and \
		            self.cur_item == len(self.items) - 1:
		        self.cur_item = 0
		    if key == pygame.K_RETURN:
		        self.items[self.cur_item].game.main()
		        print "pressed enter"
	 
		self.items[self.cur_item].set_italic(True)
		self.items[self.cur_item].set_font_color(RED)
	    def set_mouse_selection(self, item, mpos):
		"""Marks the MenuItem the mouse cursor hovers on."""
		if item.is_mouse_selection(mpos):
		    item.set_font_color(RED)
		    item.set_italic(True)
		else:
		    item.set_font_color(WHITE)
		    item.set_italic(False)
	 
	 
	    def run(self):
		mainloop = True
		while mainloop:
		    # Limit frame speed to 50 FPS
		    self.clock.tick(50)
	 
		    for event in pygame.event.get():
		        if event.type == pygame.QUIT:
		            mainloop = False
		        if event.type == pygame.KEYDOWN:
		            self.mouse_is_visible = False
		            self.set_item_selection(event.key)
	 
		    if pygame.mouse.get_rel() != (0, 0):
		        self.mouse_is_visible = True
		        self.cur_item = None
	 
		    self.set_mouse_visibility()
	 
		    # Redraw the background
		    self.screen.fill(self.bg_color)
	 
		    for item in self.items:
		        if self.mouse_is_visible:
		            mpos = pygame.mouse.get_pos()
		            self.set_mouse_selection(item, mpos)
		        self.screen.blit(item.label, item.position)
	 
		    pygame.display.flip()
	 
	# Creating the screen
        screen = pygame.display.set_mode((640, 480), 0, 32)
        #I made the menu items tuples since they just need a name and a function name
        menu_items = (('Lists', animation), ('Python Math',simple_snake), ('Quit', something))
 
        pygame.display.set_caption('Game Menu')
        gm = GameMenu(screen, menu_items)
        gm.run()
