from Gfx import gfx
from pygame import draw, image
from pygame.locals import *
from engine.EventHandler import EventMonitor
from engine.SelfCallMixin import SelfCallMixin

class Menu(EventMonitor, SelfCallMixin):
	"""
	Subclass this guy to make your own menus.
	Enter the things in the options array, and then create methods in your superclass called "Menu_myoption()"
	"""
	def __init__(self, font, options=[], indicator=None):
		EventMonitor.__init__(self)
		
		self.quit = False
		# options available on the menu screen
		self.options = options
		self.selected = self.options[0]
		self.font = font
		self.indicator = indicator
		
		self.visible = False
	
	def SetOptions(options):
		self.options = options
		self.selected = self.options[0]
	
	def Update(self):
		if self.visible:
			self.Pump()
	
	def Draw(self):
		if self.visible:
			for o in self.options:
				col = ((self.selected == o and (255, 0, 0)) or (0, 0, 0))
				txt = self.font.render(o, 1, col)
				textpos = txt.get_rect()
				textpos.centerx = gfx.screen.get_width() / 2
				textpos.centery = gfx.screen.get_height() * (self.options.index(o) * 0.1 + 0.3)
				gfx.screen.blit(txt, textpos)
				if self.selected == o and self.indicator:
					txt = self.font.render(self.indicator, 1, col)
					textpos = txt.get_rect()
					textpos.left = gfx.screen.get_width() * 0.1
					textpos.centery = gfx.screen.get_height() * (self.options.index(o) * 0.1 + 0.3)
					gfx.screen.blit(txt, textpos)
	
	def KeyDown(self, e):
		if e.key in [K_RETURN, K_SPACE, K_RIGHT]:
			self.CallMethod("Menu_" + self.selected)
	
	def KeyDown_down(self, e):
		self.selected = self.options[(self.options.index(self.selected) + 1) % len(self.options)]
	
	def KeyDown_up(self, e):
		self.selected = self.options[(self.options.index(self.selected) - 1) % len(self.options)]
	

