import pygame
from os import path

import PodSix

class Gfx:
	"""
	The graphics engine. Stores a copy of the display surface and a mini font engine.
	"""
	def __init__(self):
		self.font = {}
		self.rects = []
		self.oldrects = []
	
	def HideMouse(self):
		pygame.mouse.set_visible(False)
	
	def ShowMouse(self):
		pygame.mouse.set_visible(True)
	
	def GetMouse(self):
		return pygame.mouse.get_pos()
	
	def SetSize(self, size, fullscreen=False):
		self.window = pygame.display.set_mode(size, (fullscreen and pygame.FULLSCREEN or 0))
		self.screen = pygame.display.get_surface()
		self.width = self.screen.get_width()
		self.height = self.screen.get_height()
		self.screenRect = pygame.Rect(0, 0, self.width, self.height)
		# TODO: reload all fonts at the new size
	
	def LoadFont(self, fontname, relsize=0.2, altname=None, antialias=True):
		if not altname:
			altname = fontname
		self.font[altname] = {
				'font': pygame.font.Font(path.join("resources", fontname + ".ttf"), int(self.screen.get_width() * relsize)),
				'antialias': antialias
				}
	
	def UseFont(self, fontname, relsize=0.2, altname=None, antialias=True):
		if not altname:
			altname = fontname
		self.font[altname] = {
				'font': pygame.font.SysFont(fontname, int(self.screen.get_width() * relsize)),
				'antialias': antialias,
				}
	
	def Caption(self, caption):
		"""
		Set the window caption.
		"""
		pygame.display.set_caption(caption)
	
        def SetIcon(self, filename):
                pygame.display.set_icon(pygame.image.load(filename))
	
	def DirtyRect(self, rect):
		self.rects.append(rect)	
	
	def Flip(self):
		"""
		Post rendering stuff such as:
		Flip the pygame display surface.
		"""
		if len(self.rects):
			pygame.display.update(self.rects + self.oldrects)
			self.oldrects = self.rects[:]
			self.rects = []
		else:
			pygame.display.flip()
	
	###
	###	Drawing routines
	###
	
	def TextSize(self, text, font="default"):
		return [p /gfx.width for p in self.font[font]['font'].size(text)]
	
	def FontHeight(self, font='default'):
		return float(self.font[font]['font'].get_height()) / gfx.width
	
	def AlignToPos(self, r, pos):
		for p in pos:
			if hasattr(r, p):
				setattr(r, p, gfx.screen.get_width() * pos[p])
	
	def DrawText(self, text, pos, color=[0, 0, 0], font="default", shadow=False):
		lines = text.split("\n")
		rect = None
		for t in lines:
			yplus = lines.index(t) * self.font[font]['font'].get_linesize()
			
			txt = self.font[font]['font'].render(t, self.font[font]['antialias'], color)
			basepos = txt.get_rect()
			
			self.AlignToPos(basepos, pos)
			
			if shadow:
				txtsh = self.font.render(t, self.font[font]['antialias'], shadow)
				
				shpos = pygame.rect.Rect(basepos)
				
				shpos.centerx = basepos.centerx + 2
				shpos.centery = basepos.centery + yplus
				self.screen.blit(txtsh, shpos)
				
				shpos.centerx = basepos.centerx - 2
				shpos.centery = basepos.centery + yplus
				self.screen.blit(txtsh, shpos)
				
				shpos.centerx = basepos.centerx
				shpos.centery = basepos.centery + yplus + 2
				self.screen.blit(txtsh, shpos)
				
				shpos.centerx = basepos.centerx
				shpos.centery = basepos.centery + yplus - 2
				self.screen.blit(txtsh, shpos)
			
			basepos.centery += yplus
			self.screen.blit(txt, basepos)
			if not rect:
				rect = basepos
			else:
				rect.union_ip(basepos)
		return rect
	
	def DrawPolygon(self, points, color):
		pygame.draw.polygon(self.screen, color, points)
		return self.DrawLines(points, color, True)
	
	def DrawLines(self, points, color, closed):
		return pygame.draw.aalines(self.screen, color, closed, points)
	
	def DrawRect(self, rect, color, thickness):
		return pygame.draw.rect(self.screen, color, rect, thickness)
	
	def DrawCircle(self, center, color, radius, thickness=1):
		return pygame.draw.circle(self.screen, color, center, radius, thickness)
	
	def BlitImage(self, image, position=None, center=None, *args):
		if position:
			newpos = position
		elif center:
			newpos = [center[x] - image.Size()[x] / 2 for x in range(2)]
		else:
			raise exception("please supply center= or position=")
		return self.screen.blit(image.surface, newpos, *args)
	
	def SetBackgroundColor(self, color):
		self.screen.fill(color)

# this is a singleton and we always want it instantiated
gfx = Gfx()


