import pygame
from pygame.constants import *

from invisibleButton import InvisibleButton
from button import Button
from image import Image


class WorldGui:
	def __init__(self, world):
		self.world = world
		self.screenRect = pygame.Rect(
				self.world.game.screen.get_width() / 2 - 800/2,
				self.world.game.screen.get_height() - 185,
				800, 185)

		self.surface = pygame.Surface((self.screenRect.width, 185))
		self.surface = self.surface.convert_alpha()
		self.bgcolor = (255,0,255,0)
		self.font = pygame.font.Font(None, 20)
		self.infoText = ""
		self.miniText = "Minitext"
		self.helptext = ""

		self.elements = []

		self.addElements()

	def addElements(self):
		# -- Image1 --
		image1 = Image(800, 185, (0, 0))
		self.elements.append(image1)
		image1.loadImage("world_gui.png")

		# -- Button1 --
		button1 = InvisibleButton(47, 28, (680, 100))
		self.elements.append(button1)
		button1.setOnClick(self.world.acceptEncounter)

		# -- Button2 --
		button2 = InvisibleButton(47, 28, (680, 137))
		self.elements.append(button2)
		button2.setOnClick(self.world.generateWorld)


		# -- MiniMap --
		minimap = Image(110, 110, (140, 48))
		self.minimap = minimap
		self.elements.append(minimap)

		minimapSurf = pygame.Surface((110, 110), pygame.SRCALPHA)

		tMap = self.world.tiles.tMap

		self.minimapScaleX = 110.0 / self.world.tiles.w
		self.minimapScaleY = 110.0 / self.world.tiles.h

		for x, row in enumerate(tMap):
			for y, value in enumerate(row):
				#if value == 0:
				#	minimapSurf.fill((43,80,135),pygame.Rect(x, y,1,1))
				#elif value == 2:
				#	minimapSurf.fill((129,118,58), pygame.Rect(x, y, 1,1))
				#elif value == 4:
				#	minimapSurf.fill((64,104,18), pygame.Rect(x,y,1,1))
				#elif value == 6:
				#	minimapSurf.fill((199,200,208), pygame.Rect(x, y, 1,1))
				#else:
				#	minimapSurf.fill((0,0,0,0), pygame.Rect(x, y, 1, 1))
				if value == 0:
					minimapSurf.fill((0,0,0,0), pygame.Rect(x * self.minimapScaleX, y * self.minimapScaleY,
						1 * round(self.minimapScaleX) if round(self.minimapScaleX) > 1 else 1,
						1 * round(self.minimapScaleY) if round(self.minimapScaleY) > 1 else 1))
				else:
					minimapSurf.fill((0,70,96), pygame.Rect(x * self.minimapScaleX, y * self.minimapScaleY,
						1 * round(self.minimapScaleX) if round(self.minimapScaleX) > 1 else 1,
						1 * round(self.minimapScaleX) if round(self.minimapScaleX) > 1 else 1))
		minimap.setSurface(minimapSurf)


		# -- MiniMap Fog of War --
		self.minimapFog = Image(110, 110, (140, 48))
		self.elements.append(self.minimapFog)
		minimapFogSurf = pygame.Surface((110, 110), pygame.SRCALPHA).convert_alpha()
		minimapFogSurf.fill((0,0,0,255))
		self.minimapFog.setSurface(minimapFogSurf)

		# -- MiniMap Encounters
		self.minimapEnc = Image(110, 110, (140, 48))
		self.elements.append(self.minimapEnc)
		minimapEncSurf = pygame.Surface((110, 110), pygame.SRCALPHA).convert_alpha()

		for i in self.world.encounters:
			minimapEncSurf.fill((255, 255, 0, 255), pygame.Rect(
				i[1][0] * self.minimapScaleX, i[1][1] * self.minimapScaleY, 2, 2))

		self.minimapEnc.setSurface(minimapEncSurf)


	def draw(self, surface):
		self.surface.fill(self.bgcolor)

		for elem in self.elements:
			self.surface.blit(elem.getSurface(), elem.getPosition())

		self.drawInfoText()
		self.drawMiniText()
		self.drawHelpText()
		self.drawScore()

		self.surface.fill((255,0, 0),
				pygame.Rect((self.world.player.position.x / 32.0 + self.world.tileX / 32.0)*self.minimapScaleX + self.minimap.x,
					(self.world.player.position.y / 32.0 + self.world.tileY / 32.0) * self.minimapScaleY + self.minimap.y, 2,2))

		pygame.draw.rect(self.surface, (255,0,0, 255),
				pygame.Rect( (self.world.tileX / 32.0) * self.minimapScaleX + self.minimap.x,
					(self.world.tileY / 32.0) * self.minimapScaleY + self.minimap.y,
					(surface.get_width() / 32.0) * self.minimapScaleX,
					(surface.get_height() / 32.0) * self.minimapScaleY), 1)

		pygame.draw.circle(self.minimapFog.surface, (0,0,0,0),
				( int((self.world.player.position.x / 32.0 + self.world.tileX / 32.0) * self.minimapScaleX),
					int((self.world.player.position.y / 32.0 + self.world.tileY / 32.0) * self.minimapScaleY)),
				int(10 * self.minimapScaleY))

		surface.blit(self.surface,
				(self.world.game.screen.get_width() / 2 - 800/2,
					self.world.game.screen.get_height() - 185))


	def handleEvent(self, event):
		if event.type == MOUSEBUTTONDOWN:
			pos = event.pos + (1,1)
			rect = pygame.Rect(pos)
			if not self.screenRect.contains(rect):
				return False

			for i, elem in enumerate(self.elements):
				relativePosition = elem.getPosition()
				elemRect = pygame.Rect(
						(self.screenRect.x + relativePosition[0],
						self.screenRect.y + relativePosition[1]),
						elem.getSize())

				if elemRect.contains(pos):
					if elem.onClick(): return True

	def setInfoText(self, text):
		self.infoText = text

	def drawInfoText(self):
		text = self.font.render(self.infoText, 1, (0,90,110, 255))
		#self.surface.blit(text, text.get_rect(centerx=self.surface.width/2, centery=100/2))
		self.surface.blit(text, (260, 48))
		#self.surface.blit(text, (503, 77))

	def setMiniText(self, text):
		self.miniText = text

	def drawMiniText(self):
		for i, line in enumerate(self.miniText.split("\n")):
			text = self.font.render(line, 1, (0,90,110, 255))
			#self.surface.blit(text, text.get_rect(centerx=self.surface.width/2, centery=100/2))
			#self.surface.blit(text, (503, 77 + 18 * i))
			self.surface.blit(text, (260, 48 + 18 + 18 * i))

	def setHelpText(self, text):
		self.helptext = text

	def drawHelpText(self):
		for i, line in enumerate(self.helptext.split("\n")):
			text = self.font.render(line, 1, (0,90,110, 255))
			self.surface.blit(text, (503, 77 + 18 * i))

	def drawScore(self):
		s = 'Score: %s' % self.world.score
		text = self.font.render(s, 1, (0,90,110, 255))
		self.surface.blit(text, (260, 48 + 18 * 5))
		

