import pygame,numpy,mapgen
import constants
from pygame.constants import *
from settings import *
from tiles import Tiles
from background import Background
import random


class Encounter():
	def __init__(self, world):
		self.world = world

		screenW, screenH = self.world.game.screen.get_size()
		self.tSize = settings_tiles['tilesize']

		self.sizeW = settings_encounters['xsize']
		self.sizeH = settings_encounters['ysize']

		if settings_encounters['moving_background']:
			if settings_encounters['parallax_enable']:
				self.background1 = Background(settings_encounters['background1'])
				self.background2 = Background(settings_encounters['background1'])
			self.background3 = Background(settings_encounters['background1'], 1)

		self.map = mapgen.encounter_map(self.sizeW,self.sizeH,cave=1)

		# FIXME might want to actually use the goal..

		self.flufflist, self.map = mapgen.goal_location(self.map, self.world.physics.gravity_orientation)
		self.artifact = self.flufflist[0]

		self.flufftype = []
		self.flufftype.append((0, self.artifact))
		for i in range(len(self.flufflist) - 2):
			self.flufftype.append((random.randint(1,2), self.flufflist[i]))

		#FIX THIS
		if self.world.world.tiles.tMap[self.world.currentEncounter[1][0],self.world.currentEncounter[1][1]] == 6:
			self.encType = 'cave'
		else:
			self.encType = 'cave'

		self.scorebg = pygame.image.load("scorebg.png")
		self.itembar = pygame.image.load("itembar.png")

		self.recentitem = ''
		self.tiles = Tiles((screenW, screenH),
				self.world.physics,
				self.map,"encounter",
				self.encType,(self.world.currentEncounter[1][0],
					self.world.currentEncounter[1][1]),self.world.world.tiles, artifact = self.artifact, flufftype = self.flufftype)
		self.tileX, self.tileY = 0, 0
		self.map = self.tiles.tMap



	def DebugMap(self):
		map = numpy.zeros( (self.sizeW, self.sizeH), dtype = int )
		for x, row in enumerate(map):
			if x < 4 or x > len(row - 6): continue
			for y, value in enumerate(row):
				if y > 4 and y < len(row) - 6: continue
				if y <= 4:
					map[x, y] = 4
				else:
					map[x, y] = 4
		return map

	def draw(self, surface):
		
		surface.blit(self.tiles.getMap(*self.world.game.screen.get_size()), (0,0))
		self.drawgui(surface)
		if settings_encounters['moving_background']:
			if settings_encounters['parallax_enable']:
				self.background1.draw(surface)
				self.background2.draw(surface)
			self.background3.draw(surface)

	def drawgui(self,surface):
		surf = pygame.Surface((160, 32))
		surf.blit(self.scorebg,(0,0))
		s = 'Score: %s' % self.world.world.score
		text = self.world.world.gui.font.render(s, 1, (0,120,150, 255))
		surf.blit(text, (10,10))
		surface.blit(surf,(self.world.game.screen.get_size()[0]-180,20))	

		surf = pygame.Surface((320, 32))
		surf.blit(self.itembar,(0,0))
		s = 'Got: %s' % self.recentitem
		text = self.world.world.gui.font.render(s, 1, (0,120,150, 255))
		surf.blit(text, (10,10))
		surface.blit(surf,(20,20))	

	def got_item(self, item):
		self.recentitem = item
