import pygame, player, tile

class Human(player.Player):
	
	TRAY_COLOR = (110, 92, 80)
	TRAY_LEFT = 100
	TRAY_TOP = 550
	TRAY_FIRSTLEFT = TRAY_LEFT + tile.Tile.SQUARE_BORDER + tile.Tile.SQUARE_SIZE * .5
	TRAY_FIRSTTOP = TRAY_TOP + tile.Tile.SQUARE_BORDER
	

	def __init__(self, name, theBoard, theBag, theHeuristic = None):
		player.Player.__init__(self, name, theBoard, theBag, 10.0, theHeuristic)
		self.hand = -1
	
	def pickup(self, x, y):
		index = self.getTrayIndex(x, y)
		
		if index != -1 and index < len(self.tray):
			if self.hand == -1:
				
				self.hand = index
				return self.tray[index]
			else:

				self.tray[index], self.tray[self.hand] = self.tray[self.hand], self.tray[index]


		self.hand = -1
		return None

	def placeTentative(self):		
		if self.hand != -1:
			del self.tray[self.hand]
			self.hand = -1

	def getTrayIndex(self, x, y):
		
		x -= Human.TRAY_FIRSTLEFT
		y -= Human.TRAY_FIRSTTOP

		if x >= 0 and y >= 0 and y <= tile.Tile.SQUARE_SIZE:
		
			if x % (tile.Tile.SQUARE_SIZE + tile.Tile.SQUARE_BORDER) < tile.Tile.SQUARE_SIZE - tile.Tile.SQUARE_BORDER:
				index = (int)(x / (tile.Tile.SQUARE_SIZE + tile.Tile.SQUARE_BORDER))
				
				if index < len(self.tray):
					return index
		return -1

	def drawTray(self, DISPLAYSURF):
		

		pygame.draw.rect(DISPLAYSURF, Human.TRAY_COLOR, (Human.TRAY_LEFT, Human.TRAY_TOP, 
						(tile.Tile.SQUARE_SIZE + tile.Tile.SQUARE_BORDER)*8, 
						tile.Tile.SQUARE_SIZE + tile.Tile.SQUARE_BORDER*2))
			

		i = 0
		for t in self.tray:
			top = Human.TRAY_FIRSTTOP
			left = (Human.TRAY_FIRSTLEFT + (i * (tile.Tile.SQUARE_SIZE + tile.Tile.SQUARE_BORDER)))
			
			if i == self.hand:
				highlight = True
			else:
				highlight = False
			
			t.draw(left, top, DISPLAYSURF, highlight)	
			i += 1


	
