'''We're going to have some FUN!
'''
import pygame
from pygame.locals import *
import windows
import data
import menu
import time

def modcol(arg):
	return tuple(pygame.color.Color(arg))

def coladd(arg1, arg2):
	return tuple(pygame.color.Color(*arg1) + pygame.color.Color(arg2, arg2, arg2))

def colsub(arg1, arg2):
	return tuple(pygame.color.Color(*arg1) - pygame.color.Color(arg2, arg2, arg2))

def main():
	pygame.init()
	pygame.Color = modcol
	pygame.color.add = coladd
	pygame.color.subtract = colsub
	mapimg = pygame.image.load(data.filepath('map.png'))
	mapw, maph = mapimg.get_rect().size
	screen = pygame.display.set_mode((mapw, maph+100))
	pygame.display.set_caption('Crazy Atropos')

	MENU = 'menu'
	GAME = 'game'
	ENDING = 'ending'
	mode = MENU

	menuimg = data.loadImage('intro.png')
	mapimg = mapimg.convert()

	background = pygame.Surface(screen.get_size()).convert()
	background.blit(menuimg, (0,0))

	#Deal with menu
	font = pygame.font.Font(None, 25)
	insfont = pygame.font.Font(None, 20) #Instruction font
	x = background.get_width() - 150
	y = background.get_height()/2
	menu.init(x, y, font, insfont)
	menu.draw(background, menuimg)

	#Deal with ending
	endimg = None
	endings = {windows.NUKE_VICTORY:pygame.image.load(data.filepath('nuclear.png')), windows.CONQUEST_VICTORY:pygame.image.load(data.filepath('victory.png')), windows.DEFEAT:pygame.image.load(data.filepath('defeat.png'))}

	screen.blit(background, (0,0))
	pygame.display.flip()

	clock = pygame.time.Clock()
	now = time.time()
	while True:
		clock.tick(30)
		#Check for victory conditions:
		if mode == GAME and windows.ending is not None:
			mode = ENDING
			endimg = endings[windows.ending]
		#Input
		for event in pygame.event.get():
			if event.type == QUIT:
				return
			elif event.type == KEYDOWN and event.key == K_ESCAPE:
				return
			elif event.type == MOUSEBUTTONDOWN and event.button == 1:
				if mode == MENU:
					if not menu.howtomode:
						if menu.startrect.collidepoint(event.pos):
							map, threads = windows.init(screen, mapimg)
							map.populate()
							threads.populate()
							mode = GAME
						elif menu.howtorect.collidepoint(event.pos):
							menu.displayHowto()
						elif menu.quitrect.collidepoint(event.pos):
							return
					else:
						menu.displayHowto(False)
				elif mode == GAME:
					threads.cutThread(event.pos)
				elif mode == ENDING: #On first click, go back to menu
					mode = MENU
		pos = pygame.mouse.get_pos()
		if mode == MENU:
			menu.draw(screen, menuimg)
			menu.drawMouseover(pos, screen, menuimg)
		elif mode == GAME:
			#Fun graphics
			threads.mouseover(pos)
			#Logic
			map.update()
			threads.update()
			if time.time() - now >= 0.5: #Tick every half second
				map.tick()
				threads.tick()
				now = time.time()

			#Render
			map.draw()
			threads.draw()
			threads.drawMouseover(screen)
		elif mode == ENDING:
			screen.blit(endimg, (0, 0))

		pygame.display.flip()

if __name__ == '__main__':
	import cProfile
	cProfile.run('main()', 'profile')
