require "skill"
require "skillselection"
require "cast"
ripple = require "libs.ripple"

Class = require "libs.hump.class"
local anim8 = require "libs.anim8"

local positions = { [0] = -40, [1] = 80, [2] = 200, [3] = 542, [4] = 674, [5] = 806}
local level = 400
local high = 180
local origin = level - high
local offset = 15
local idle, idleAnim
local damage, damageAnim
local ulti, ultiAnim
local defense, defenseAnim
local basic, basicAnim
local death, deathAnim

local setHabTimer1, setHabTimer2, setHabTimer3, setHabTimer4
local habTimer1, habTimer2, habTimer3, habTimer4

local setTimer, animtimer

local playerstates = {alive = 1, basicAttacking = 2, ultiAttacking = 3, defending = 4, takingDamage = 5, dead = 6 }

Player = Class{

	init = function(self, i, lps, en, recov)
 		self.playerstates = {alive = 1, basicAttacking = 2, ultiAttacking = 3, defending = 4, takingDamage = 5, dead = 6 }
		self.id = i
		self.name = "Player" .. i
		self.lifepoints = lps
		self.energy = en
		self.recovery = recov
		self.numskills = 0
		self.skills = {}
		self.position = {}
		self.timer = 0
		self.movable = true
		self.movementDelay = 0.4
		self.position.y = origin
		self.contador = 1
		self.lastSkill = ""
		self.state = self.playerstates.alive
		self.timer1 = 0
	end;

	load = function(self, t, id)

		tags = {sfx = ripple.newTag(), master = ripple.newTag()}
		sfxs = {
		ataque = ripple.newSound("assets/sounds/Ataque.ogg", {
			bpm = 120,
			length = '0.346s',
			loop = false,
			tags = {tags.sfx, tags.master},
			}),
		daño = ripple.newSound("assets/sounds/Daño.ogg", {
			bpm = 120,
			length = '0.5s',
			loop = false,
			tags = {tags.sfx, tags.master},
			})
		}
		tags.master:setVolume(.5)
 
		self.position.x = positions[t] + offset
		self.tile = t

		self.setTimer = 0
		self.animtimer = 0

		if (id == 1) then
			self.basic = love.graphics.newImage('assets/images/p1_basic.png')
	    	local g = anim8.newGrid(290,272,self.basic:getWidth(),self.basic:getHeight())
			self.basicAnim = anim8.newAnimation(g('1-6',1), 0.16)
			self.basicAnim:flipH()

			self.damage = love.graphics.newImage('assets/images/p1_damage.png')
	    	local g2 = anim8.newGrid(290,272,self.damage:getWidth(),self.damage:getHeight())
			self.damageAnim = anim8.newAnimation(g2('1-6',1), 0.16)
			self.damageAnim:flipH()

			self.defend = love.graphics.newImage('assets/images/p1_defend.png')
	    	local g3 = anim8.newGrid(290,272,self.defend:getWidth(),self.defend:getHeight())
			self.defendAnim = anim8.newAnimation(g3('1-6',1), 0.16)
			self.defendAnim:flipH()

			self.idle = love.graphics.newImage('assets/images/p1_idle.png')
	    	local g4 = anim8.newGrid(280,262,self.idle:getWidth(),self.idle:getHeight())
			self.idleAnim = anim8.newAnimation(g4('1-12',1), 0.16)
			self.idleAnim:flipH()

			self.ulti = love.graphics.newImage('assets/images/p1_ulti.png')
	    	local g5 = anim8.newGrid(310,310,self.ulti:getWidth(),self.ulti:getHeight())
			self.ultiAnim = anim8.newAnimation(g5('1-6',1), 0.16)
			self.ultiAnim:flipH()	

			self.death = love.graphics.newImage('assets/images/p1_lose.png')
			local g6 = anim8.newGrid(220, 272, self.death:getWidth(),self.death:getHeight())
			self.deathAnim = anim8.newAnimation(g6('1-9',1), 0.16, 'pauseAtEnd')
			self.deathAnim:flipH()	


		else
			self.basic = love.graphics.newImage('assets/images/p2_basic.png')
	    	local g = anim8.newGrid(220,272,self.basic:getWidth(),self.basic:getHeight())
			self.basicAnim = anim8.newAnimation(g('1-6',1), 0.16)

			self.damage = love.graphics.newImage('assets/images/p2_damage.png')
	    	local g2 = anim8.newGrid(220,272,self.damage:getWidth(),self.damage:getHeight())
			self.damageAnim = anim8.newAnimation(g2('1-6',1), 0.16)

			self.defend = love.graphics.newImage('assets/images/p2_defend.png')
	    	local g3 = anim8.newGrid(220,272,self.defend:getWidth(),self.defend:getHeight())
			self.defendAnim = anim8.newAnimation(g3('1-6',1), 0.16)

			self.idle = love.graphics.newImage('assets/images/p2_idle.png')
	    	local g4 = anim8.newGrid(220,272,self.idle:getWidth(),self.idle:getHeight())
			self.idleAnim = anim8.newAnimation(g4('1-6',1), 0.16)

			self.ulti = love.graphics.newImage('assets/images/p2_ulti.png')
	    	local g5 = anim8.newGrid(220,272,self.ulti:getWidth(),self.ulti:getHeight())
			self.ultiAnim = anim8.newAnimation(g5('1-10',1), 0.16)

			self.death = love.graphics.newImage('assets/images/p2_lose.png')
			local g6 = anim8.newGrid(220, 272, self.death:getWidth(),self.death:getHeight())
			self.deathAnim = anim8.newAnimation(g6('1-10',1), 0.16, 'pauseAtEnd')

		end
		end;

	draw = function(self)
		love.graphics.setColor(255, 255, 255)

		if self.state == 1 then
			self.idleAnim:draw(self.idle, self.position.x, self.position.y)
		end

		if self.state == 2 then
			self.basicAnim:draw(self.basic, self.position.x, self.position.y)
		end		

		if self.state == 3 then
			self.ultiAnim:draw(self.ulti, self.position.x, self.position.y)
		end		

		if self.state == 4 then
			self.defendAnim:draw(self.defend, self.position.x, self.position.y)
		end		

		if self.state == 5 then
			self.damageAnim:draw(self.damage, self.position.x, self.position.y)
		end

		if self.state == 6 then
			self.deathAnim:draw(self.death, self.position.x, self.position.y)
		end		

		--love.graphics.print(self.name .. " (" .. self.numskills .. ")", self.position.x+80, self.position.y-5)
		--love.graphics.print(self.lastSkill, self.position.x+80, self.position.y-15)
	end;

	update = function(self, otherPlayer, dt)
		self.timer1 = self.timer1 + dt
		self.otherPlayer = otherPlayer
		if (self.movable == false) then
			self.timer = self.timer + dt
			if (self.timer > self.movementDelay) then
				self.timer = 0
				self.movable = true
			end
		end

		if self.setTimer == 1 then
			self.animtimer = self.animtimer + dt
		end


		if self.state == 1 then
			self.idleAnim:resume(dt)
			self.idleAnim:update(dt)
		end

		if self.state == 2 then
			self.basicAnim:resume(dt)
			self.basicAnim:update(dt)
		end

		if self.state == 3 then
			self.ultiAnim:resume(dt)
			self.ultiAnim:update(dt)
		end

		if self.state == 4 then
			self.defendAnim:resume(dt)
			self.defendAnim:update(dt)
		end		

		if self.state == 5 then
			self.damageAnim:resume(dt)
			self.damageAnim:update(dt)
		end

		if self.state == 6 then
			self.deathAnim:resume(dt)
			self.deathAnim:update(dt)
		end


		if self.state < 6 then
			if (id == 1) then
				if self.animtimer > 0.96 then
	  				self.animtimer = 0
					self.defendAnim:gotoFrame(1,1)
					self.state = 1
					self.setTimer = 0
				end

				if self.animtimer > 1.6 then
					self.animtimer = 0
					self.setTimer = 0
					self.state = 1
					self.ultiAnim:gotoFrame(1,1)
				end
			else

	 			 if self.animtimer > 0.96 and self.state ~= 3 then
	  				self.animtimer = 0
					self.defendAnim:gotoFrame(1,1)
					self.state = 1
					self.setTimer = 0
				end

				if self.animtimer > 1.6 then
					self.animtimer = 0
					self.setTimer = 0
					self.state = 1
					self.ultiAnim:gotoFrame(1,1)
				end
			end
		else
			if timer > 3 then
				love.event.quit("restart")
			end
		end
	end;

	move = function(self, dir)
		if (self.movable) then
			local destiny = self.tile + dir
			if (destiny>-1 and destiny <6) then
				if (self.id == 1 and destiny < 3) then
					self.position.x = positions[destiny] + offset
					self.tile = destiny
					self.movable = false
				end

				if (self.id == 2 and destiny > 2) then
					self.position.x = positions[destiny] + offset
					self.tile = destiny
					self.movable = false
				end
			end
		end
	end;

	getLife = function(self)
		return 100
	end;

	getStamina = function(self)
		return self.energy
	end;

	getTile = function(self)
		return self.tile
	end;

	setId = function(self, i)
		self.id = i
	end;

	getId = function(self)
		return self.id
	end;

	learn = function(self, sk)
		self.skills[self.contador] = sk
		self.numskills = self.numskills + 1
		self.contador = self.contador + 1
	end;

	cast = function(self, id, other)
		self.cast = Cast(self, self.skills[id], other)
		self.energy = self.energy - skills[id]:getCost()
		self.lastSkill = self.skills[id]:getName()
	end;

	takeDamage = function(self, da)
		--animación de daño
		self.lifepoints = self.lifepoints - da
	end;

	getTimer1 = function(self)
	return self.timer1
	end;

	getTimer2 = function(self)
	return self.timer2
	end;

	getTimer3 = function(self)
	return self.timer3
	end;

	getTimer4 = function(self)
	return self.timer4
	end;
	

	setTimer1 = function(self, v)
	self.timer1 = v
	end;

	setTimer2 = function(self, v)
	self.timer2 = v
	end;


	setTimer3 = function(self, v)
	self.timer3 = v
	end;


	setTimer4 = function(self, v)
	self.timer4 = v
	end;

	changeState = function(self, v)
		if v == 1 then
			self.state = self.playerstates.alive
		end

		if v == 2 then
			self.state = self.playerstates.basicAttacking
			sfxs.ataque:play()
		end

		if v == 3 then
			self.state = self.playerstates.ultiAttacking
		end

		if v == 4 then
			self.state = self.playerstates.defending
		end

		if v == 5 then
			self.state = self.playerstates.takingDamage
			sfxs.daño:play()
		end

		if v == 6 then
			self.state = self.playerstates.dead
		end

		self.setTimer = 1
	end

}

return player