import pygame
from menu import WholeMenu
from pygame.locals import *
from functions import drawText
from options import *
import os

def main():
	wyn, score = None, None
	paramplus = False
	paramminus = False
	#Initialize Everything
	#opcje = Options("options.txt")
	#MessageBox("Start!","Start game?")
	menu = WholeMenu()
	menu.loadFromXML(os.path.join("menu", "menu.xml"))
#	menu.loadOptions("options2.xml")
	options = Options()
	pygame.init()
	ticks = 0
	fps = 50
	#width, height = 800, 600
	width, height = 1024, 768
	screen = pygame.display.set_mode((width, height),FULLSCREEN|HWSURFACE|DOUBLEBUF)
	#screen = pygame.display.set_mode((width, height),FULLSCREEN)
	pygame.display.set_caption('Kimboot menu test')
	pygame.mouse.set_visible(0)
	clock = pygame.time.Clock()
	tlo = pygame.Surface(screen.get_size(),HWSURFACE|DOUBLEBUF)
	tlo = tlo.convert()
	tlo.fill((250, 250, 250))
	screen.blit(tlo, (0, 0))
	pygame.display.flip()
	(x,y) = pygame.mouse.get_pos()
	screen.blit(tlo, (0, 0))
	pygame.display.flip()
	while 1:
#		if (wyn != None):
#			score = drawText("Points: "+str(wyn[0])+", kills: "+str(wyn[1])+", max kombo: x"+str(wyn[2]),(0,0,0),(255,255,255),(400,400))
#			menu.playing = True
		clock.tick(60)
		if (paramplus):
			menu.setParam(1)
		if (paramminus):
			menu.setParam(-1)
		for event in pygame.event.get():
			if event.type == QUIT:
				return
			elif event.type == MOUSEBUTTONDOWN:
				if event.button > 1:
					return
			elif event.type == KEYDOWN and not menu.playing: 
				if event.key == K_ESCAPE:
					return
				elif event.key == K_k:
					wyn = None
					menu.playing = False
				elif event.key == K_DOWN:
					menu.getCurrentMenu().changeCurrentItem(1)
				elif event.key == K_UP:
					menu.getCurrentMenu().changeCurrentItem(-1)
				elif event.key == K_LEFT:
					paramminus = True
#					menu.setParam(-1)
				elif event.key == K_RIGHT:
					paramplus = True
#					menu.setParam(1)
				elif event.key == K_RETURN:
					wyn = menu.runAction(tlo)
					if (wyn != None):
						menu.setLastScore(wyn)
						options.hiscores.checkAndSave(wyn)
			elif event.type == KEYUP and not menu.playing:
				if event.key == K_LEFT:
					paramminus = False
				elif event.key == K_RIGHT:
					paramplus = False
			elif event.type == MOUSEBUTTONDOWN:
				if event.button > 1:
					return
			elif event.type == MOUSEBUTTONUP:
				pass
		(x,y) = pygame.mouse.get_pos()
		if not menu.playing:
			menu[menu.currentmenu].drawMenu(tlo)
			if (score != None):
				tlo.blit(score[0],score[1])
		screen.blit(tlo, (0, 0))
		pygame.display.flip() 
		clock.tick(fps)
		ticks += 1 
		
if __name__ == '__main__': main()

