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

local positions = { [1] = 38, [2] = 170, [3] = 302, [4] = 592, [5] = 724, [6] = 856}
local level = 460

--keys for player 1:
local left1 	= "a"
local right1 	= "d"
local basic1 	= "c"
local skilla1 	= "v"
local skillb1 	= "b"
local ult1 		= "f"

--keys for player 2:
local left2 = "j"
local right2 = "l"
local basic2 	= "up"
local skilla2 	= "left"
local skillb2 	= "right"
local ult2 		= "down"

local damage = 5
local delay = 0.5
local cooldown = 4	
local target = 2
local duration = 1
local punt = true

local habDelay = 0
local setDelay = 0


local life1, drawLife1
local life2, drawLife2
local stamina1, drawStamina1
local stamina2, drawStamina2
local recarga

System = Class{
	init = function(self, num, tile0, tile1, bg)
		self.players = {}
		self.tiles = {}
		self.skills = {}
		self.background = bg
		self.luces 		 = love.graphics.newImage("assets/images/luces.png")
		local g = anim8.newGrid(1024,720, self.luces:getWidth(), self.luces:getHeight())
		self.lucesAnim = anim8.newAnimation(g('1-30',1), 0.16)

		self.skills[1] = Skill(1, "Basic", 	5, 0.4, 0.4, 0.8, 10) 
		self.skills[2] = Skill(2, "3b-4", 	10, 0.2, 1.2, 7.2, 50)
		self.skills[3] = Skill(3, "Reverb", 0, 0.2, 0.4, 2.4, 20)
		self.skills[4] = Skill(4, "Null", 	0, 0.4, 1.2, 6.4, 30)

		life1 = 100
		life2 = 100
		stamina1 = 100
		stamina2 = 100
		recarga = 0.1

		for i=0, num, 1 do
			self.players[i] = Player(i, 100, 100, 0.1)
		end

		for i, v in ipairs(positions) do
			if (i-1 < 3 ) then
				self.tiles[i] = Tile(i-1, v, level, tile0)
			else
				self.tiles[i] = Tile(i-1, v, level, tile1)
			end
		end


	end;

	load = function(self)
		for index, v in ipairs(self.players) do
			if (index == 1) then
				v:load(1, index)
			end

			if (index == 2) then
				v:load(4, index)
			end

		end

		life1 = self.players[1]:getLife()
		life2 = self.players[2]:getLife()


	end;

	draw = function(self)
		love.graphics.draw(self.background, 0, 0)
		love.graphics.setColor(255, 255, 255, 30)
		self.lucesAnim:draw(self.luces, 0, 0)
		love.graphics.setColor(255, 255, 255, 255)
		for i, v in ipairs(self.tiles) do
			v:draw()
		end
		for i, v in ipairs(self.players) do
			v:draw()
		end
--Lado izquierdo
    love.graphics.setBackgroundColor(255, 255, 255, 255)

	love.graphics.setColor(0,200,0,255)
	drawLife1 = (love.graphics.getWidth()/2 - 50) * (life1 / 100)
	love.graphics.rectangle('fill', 25,(love.graphics.getHeight() - 40), drawLife1, 20)
	
	--love.graphics.setColor(0,0,0,255)
	--love.graphics.rectangle('line', 25, (love.graphics.getHeight() - 40), (love.graphics.getWidth()/2 - 50), 20)
	love.graphics.reset()
	love.graphics.draw(love.graphics.newImage("assets/images/bar0102.png"),18.5, (love.graphics.getHeight() - 49))

--Lado derecho
	love.graphics.setColor(0,200,0,255)
	drawLife2 = (love.graphics.getWidth()/2 - 50) * (life2 / 100)
	love.graphics.rectangle('fill', (love.graphics.getWidth() - 25), (love.graphics.getHeight() - 40), -drawLife2, 20)
	
	--love.graphics.setColor(0,0,0,255)
	--love.graphics.rectangle('line', (love.graphics.getWidth() - 25), (love.graphics.getHeight() - 40), -(love.graphics.getWidth()/2 - 50), 20)	

	love.graphics.reset()
	love.graphics.draw(love.graphics.newImage("assets/images/bar0101.png"), (love.graphics.getWidth()/2 + 18.5), (love.graphics.getHeight() - 49))

--Lado izquierdo
	love.graphics.setColor(0,0,200,255)
	drawStamina1 = (love.graphics.getWidth()/2 - 120) * (stamina1 / 100)
	love.graphics.rectangle('fill', 25,(love.graphics.getHeight() - 55), drawStamina1, 10)
	
	--love.graphics.setColor(0,0,0,255)
	--love.graphics.rectangle('line', 25, (love.graphics.getHeight() - 55), (love.graphics.getWidth()/2 - 120), 10)
	love.graphics.reset()
	love.graphics.draw(love.graphics.newImage("assets/images/bar0201.png"),18.5, (love.graphics.getHeight() - 66))

--Lado derecho
	love.graphics.setColor(0,0,200,255)
	drawStamina2 = (love.graphics.getWidth()/2 - 120) * (stamina2 / 100)
	love.graphics.rectangle('fill', (love.graphics.getWidth() - 25), (love.graphics.getHeight() - 55), -drawStamina2, 10)
	
	--love.graphics.setColor(0,0,0,255)
	--love.graphics.rectangle('line', (love.graphics.getWidth() - 25), (love.graphics.getHeight() - 55), -(love.graphics.getWidth()/2 - 120), 10)
	love.graphics.reset()
	love.graphics.draw(love.graphics.newImage("assets/images/bar0202.png"),(love.graphics.getWidth()/2 + 88), (love.graphics.getHeight() - 66))		
	end;

	update = function(self, dt)

		self.lucesAnim:update(dt)

		if setDelay == 1 then
			habDelay = habDelay + dt
		end

		for i, v in ipairs(self.players) do
			if v:getId() == 1 then
				v:update(self.players[2], dt)
			else
				v:update(self.players[1], dt)
			end
		end

		if (life1 <= 0) then
			life1 = 0
		end

		if (life2 <= 0) then
			life2 = 0
		end	

		if (stamina1 <= 0) then
			stamina1 = 0
		end

		if (stamina2 <= 0) then
			stamina2 = 0
		end

		if (stamina2 < 100 or stamina1 < 100) then
			if stamina1 < 100 then
			 	stamina1 = stamina1 + recarga
			end

			if stamina2 < 100 then
			 	stamina2 = stamina2 + recarga
			end
		end
	end;

	loadTiles = function(self)
		for index, value in ipairs(self.tiles) do
			self.tiles[index] = require "tile"
		end
	end;

	move = function(self, key)

		--[[if (key == 'k') then
			life1 = life1 - 10
			life2 = life2 - 40
			stamina1 = stamina1 - 30
		end]]--

		if (key == left1) then
			self.players[1]:move(-1)
		end
		if (key == right1) then
			self.players[1]:move(1)
		end

		if (key == left2) then
			self.players[2]:move(-1)
		end
		if (key == right2) then
			self.players[2]:move(1)
		end


		if(key == basic1) then

			if (self.players[1]:getTimer1() > self.skills[1]:getCooldown()) then
				self.players[1]:setTimer1(0)
				self.players[1]:changeState(2)
				target = self.players[1]:getTile()+3
			--	setDelay = 1

				--if (habDelay >= 0.3) then
				--	setDelay = 0
				--	habDelay = 0
					if (self.players[2]:getTile() == target) then --Este if debería ejecutarse después del delay
						self.players[2]:changeState(5)
						life2 = life2 - self.skills[1]:getDamage()
						self.players[2]:takeDamage(self.skills[1]:getDamage())
					end
				--end
			end

		end

		if(key == basic2) then
			if (self.players[2]:getTimer1() > self.skills[1]:getCooldown()) then
				self.players[2]:setTimer1(0)
				self.players[2]:changeState(2)
				target = self.players[2]:getTile() - 3

				--setDelay = 1
					
			--	if (habDelay >= 0.3) then
					--setDelay = 0
					--habDelay = 0
					if (self.players[1]:getTile() == target) then --Este if debería ejecutarse después del delay
						self.players[1]:changeState(5)
						life1 = life1 - self.skills[1]:getDamage()
						self.players[1]:takeDamage(self.skills[1]:getDamage())
					end
				--end
			end
		end

	end;

	teachSkill = function(self, ip, skill)
		self.players[ip]:learn(skill)
	end;

	getSkills = function(self)
		return self.skills
	end;
}

