class Player(Collider.Collider):
	'''
		Player class. images is a list of images for each direction. We may need
		to alter this to support animation.
	'''

	def __init__(self, gameState):
		super(Player,self).__init__()

		# load all images
		# up, down, left, right
		self.images = [0] * 4
		self.images[UP],    self.rect = util.loadImage(PLAYER_IDLE_UP)
		self.images[DOWN],  self.rect = util.loadImage(PLAYER_IDLE_DOWN)
		self.images[LEFT],  self.rect = util.loadImage(PLAYER_IDLE_LEFT)
		self.images[RIGHT], self.rect = util.loadImage(PLAYER_IDLE_RIGHT)

		self.direction = UP

		# assign image and position
		self.setImage(self.images[self.direction])
		self.setPos(START_X, START_Y)

		# load sword
		self.swordLeft = pygame.sprite.Sprite()
		self.swordLeft.image, self.swordLeft.rect  = util.loadImage(SWORD_LEFT)
		self.swordUp = pygame.sprite.Sprite()
		self.swordUp.image, self.swordUp.rect = util.loadImage(SWORD_UP)
		self.swordRight = pygame.sprite.Sprite()
		self.swordRight.image, self.swordRight.rect = util.loadImage(SWORD_RIGHT)
		self.swordDown = pygame.sprite.Sprite()
		self.swordDown.image, self.swordDown.rect = util.loadImage(SWORD_DOWN)

		self.gameState = gameState

		#health
		self.health = 10
		self.alive = True

	# Orient player with mouse
	def orient(self, mousePos):
		self.dirVec = mousePos - Vector2(self.getPos())
		angle = math.atan2(self.dirVec.x, self.dirVec.y)
		mag = math.fabs(angle)

		# if we're facing to the right
		if mag < math.pi / 4:
			self.setDir(DOWN)
		# move left
		elif mag > 3 * math.pi / 4:
			self.setDir(UP)
		# either up or down
		else:
			if angle < 0:
				self.setDir(LEFT)
			else:
				self.setDir(RIGHT)

	def setDir(self, newDir):
		self.direction = newDir
		self.image = self.images[self.direction]

	# TODO: FIX THIS
	def swingSword(self):
		'''
			When left mouse is pressed, sword is pushed out
		'''

		if self.direction == UP:
			self.swordUp.rect.bottomleft = self.rect.topleft
			self.swordUp.add(self.gameState.playerGroup)
		elif self.direction == DOWN:
			self.swordDown.rect.topleft = self.rect.bottomleft
			self.swordDown.add(self.gameState.playerGroup)
		elif self.direction == LEFT:
			self.swordLeft.rect.topright = self.rect.topleft
			self.swordLeft.add(self.gameState.playerGroup)
		elif self.direction == RIGHT:
			self.swordRight.rect.topleft = self.rect.topright
			self.swordRight.add(self.gameState.playerGroup)

	# TODO: Add to this
	def shootBow(self):
		'''
		When right mouse is pressed, arrow is fire infront of character
	'''
		print "Arrow Fired"

	# TODO: Add to this
	def useMagic(self):
		'''
		When space bar is pressed, magic is thrown towards the mouse pointer
		Or infront of the character *Choice*
		'''
		pos = self.rect.center
		self.magi = Magic(pos[0], pos[1], self.dirVec, self)

		self.magi.add(self.gameState.weaponGroup)

	def update(self, clock, player, enemies, surfaces):
		from config import keyboard, keymap

		#am i alive?
		if (self.health <= 0):
			self.alive = False

		vel = Vector2(0, 0);
		if keyboard.down(keymap.UP):
			vel.y -= 1
		elif keyboard.down(keymap.DOWN):
			vel.y += 1
		if keyboard.down(keymap.LEFT):
			vel.x -= 1
		elif keyboard.down(keymap.RIGHT):
			vel.x += 1
		self.vel = vel.normalized() * PLAYER_SPEED

		super(Player, self).update(clock, player, enemies, surfaces)

		if keyboard.downup(keymap.MAGIC):
			self.useMagic()

		# Check to see if we have touched edge of the screen
		if self.rect.left < config.TILEX * 2:
			self.gameState.nextMap("left", self.getPos())
		elif self.rect.right > config.WIDTH - config.TILEX * 2:
			self.gameState.nextMap("right", self.getPos())
		elif self.rect.top < 0:
			self.gameState.nextMap("up", self.getPos())
		elif self.rect.bottom > config.HEIGHT:
			self.gameState.nextMap("down", self.getPos())

	def getHealth(self):
		return self.health

	def damaged(self, damage):
		self.health -= damage

	def attack(self, target, damage):
		target.damaged(damage)
class Player(Actor.Actor):
	'''
		Player class. images is a list of images for each direction. We may need
		to alter this to support animation.
	'''
	def __init__(self,gameState):
		super(Player,self).__init__()
		
		# load all images
		# up, down, left, right
		self.images = [0] * 4
		self.images[0], self.rect = util.loadImage(PLAYER_IDLE_UP, -1)
		self.images[1], self.rect = util.loadImage(PLAYER_IDLE_DOWN, -1)
		self.images[2], self.rect = util.loadImage(PLAYER_IDLE_LEFT, -1)
		self.images[3], self.rect = util.loadImage(PLAYER_IDLE_RIGHT, -1)
		
		# 0 = up, 1 = down, 2 = left, 3 = right
		self.direction = 0
		
		# assign image and position
		self.setImage(self.images[self.direction])
		self.setPos(START_X, START_Y)

		# load sword
		self.swordLeft        = pygame.sprite.Sprite()
		self.swordLeft.image,self.swordLeft.rect  = util.loadImage("swordSwingLeft.png")
		self.swordUp          = pygame.sprite.Sprite()
		self.swordUp.image,self.swordUp.rect    = util.loadImage("swordSwingUp.png")
		self.swordRight       = pygame.sprite.Sprite()
		self.swordRight.image,self.swordRight.rect = util.loadImage("swordSwingRight.png")
		self.swordDown        = pygame.sprite.Sprite()
		self.swordDown.image,self.swordDown.rect  = util.loadImage("swordSwingDown.png")
	
		self.gameState = gameState
	
	# Orient player with mouse
	def orient(self, mousePos):
		loc = mousePos - Vector2(self.getPos())
		angle = math.atan2(loc[1],loc[0])
		mag = math.fabs(angle)
		
		# if we're facing to the right
		if mag < math.pi / 4:
			self.setDir(3)
		# move left
		elif mag > 3*math.pi / 4:
			self.setDir(2)
		# either up or down
		else:
			if angle < 0:
				self.setDir(0)
			else:
				self.setDir(1)
	
	def setDir(self, newDir):
		self.direction = newDir
		self.image = self.images[self.direction]
	
	def collideWall(self, wall):
		# collision on the top of charcter
		if self.rect.top < wall.rect.bottom and self.rect.bottom > wall.rect.top and self.vel[1]<0:
			self.vel -= Vector2(0,0)
			self.rect.top = wall.rect.bottom
		# collision on the bottom of character
		if self.rect.bottom > wall.rect.top and self.rect.top < wall.rect.bottom and self.vel[1]>0:
                        self.vel -= Vector2(0,0)
                        self.rect.bottom = wall.rect.top
                # collision on the right side of character
                if self.rect.right > wall.rect.left and self.rect.left < wall.rect.right and self.vel[0]>0:
                        self.vel -= Vector2(0,0)
                        self.rect.right = wall.rect.left
                # collision on the left side of character
                if self.rect.left < wall.rect.right and self.rect.right > wall.rect.left and self.vel[0]<0:
                        self.vel -= Vector2(0,0)
                        self.rect.left = wall.rect.right
	
	def move(self, m):
		'''
			Press a key and add to our velocity vector
		'''
		if m == -1:
			self.vel = Vector2(0,0)
		elif m == 0:
			self.vel += Vector2(0,-1) * PLAYER_SPEED
		elif m == 1:
			self.vel += Vector2(0,1) * PLAYER_SPEED
		elif m == 2:
			self.vel += Vector2(-1,0) * PLAYER_SPEED
		elif m == 3:
			self.vel += Vector2(1,0) * PLAYER_SPEED
	
	def unMove(self, m):
		'''
			Once a key is released, remove that from velocity vector.
		'''
		if m == 0:
			self.vel -= Vector2(0,-1) * PLAYER_SPEED
		elif m == 1:
			self.vel -= Vector2(0,1) * PLAYER_SPEED
		elif m == 2:
			self.vel -= Vector2(-1,0) * PLAYER_SPEED
		elif m == 3:
			self.vel -= Vector2(1,0) * PLAYER_SPEED
	
	# TODO: FIX THIS
	def swingSword(self):
		'''
			When left mouse is pressed, sword is pushed out
		'''
		if self.direction == 0:
		 	self.swordUp.rect.bottomleft = self.rect.topleft
			self.swordUp.add(self.gameState.playerGroup)
		elif self.direction == 1:
		 	self.swordDown.rect.topleft = self.rect.bottomleft
			self.swordDown.add(self.gameState.playerGroup)
		elif self.direction == 2:
		 	self.swordLeft.rect.topright = self.rect.topleft
			self.swordLeft.add(self.gameState.playerGroup)
		elif self.direction == 3:
		 	self.swordRight.rect.topleft = self.rect.topright
			self.swordRight.add(self.gameState.playerGroup)

    	# TODO: Add to this
    	def shootBow(self):
        	'''
        	When right mouse is pressed, arrow is fire infront of character
		'''
        	print "Arrow Fired"
        
   	# TODO: Add to this
   	def useMagic(self):
        	'''
        	When space bar is pressed, magic is thrown towards the mouse pointer
        	Or infront of the character *Choice*
        	'''
        	pos = self.rect.center
        	self.magi = Magic(pos[0],pos[1])

        	self.magi.add(self.gameState.playerGroup)
        	
	
	def update(self):
		super(Player,self).update()

		# Check to see if we have touched edge of the screen
		if self.rect.left < TILEX * 2:
			self.gameState.nextMap("left", self.getPos())
		elif self.rect.right > WIDTH - (TILEX*2):
			self.gameState.nextMap("right", self.getPos())
		elif self.rect.top < 0:
			self.gameState.nextMap("up", self.getPos())
		elif self.rect.bottom > HEIGHT:
			self.gameState.nextMap("down", self.getPos())