require "system"
require "menu"
require "skillselection"
local anim8 = require "libs.anim8"
ripple = require "libs.ripple"

local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
local tile_mesures = { w = 130, h = 60}
local gamestates = { menu = 0, skillSelectionOne = 1, skillSelectionTwo = 2,  battle = 3, gameOver = 4}
local state
local system
local menu
local skillselectOne
local skillselectTwo
local sw

tags = {sfx = ripple.newTag(), music = ripple.newTag(), master = ripple.newTag() }
musicThingy = {
backgroundMusic = ripple.newSound("assets/sounds/loop.ogg", {
	bpm = 120,
	length = '128s',
	loop = true,
	tags = {tags.music, tags.master},
	}),
backgroundIntro = ripple.newSound("assets/sounds/intro.ogg", {
	bpm = 120,
	length = '15.99s',
	loop = false,
	tags = {tags.music, tags.master},
	}),
menu = ripple.newSound("assets/sounds/menu.ogg", {
	bpm = 120,
	length = '16s',
	loop = true,
	tags = {tags.music, tags.master},
	})
}
tags.master:setVolume(.5)
credits_bg	 = love.graphics.newImage("assets/images/credits_bg.png")

function love.load()
	menu_bg	 	 = love.graphics.newImage("assets/images/menu_bg.png")
	skill_bg	 = love.graphics.newImage("assets/images/skill_bg.png")
	battle_bg	 = love.graphics.newImage("assets/images/battle_bg2.png")
	tile0 = love.graphics.newImage("assets/images/tile0.png")
	tile1 = love.graphics.newImage("assets/images/tile1.png")
	sw = false
	menu 			= Menu(menu_bg)
	system 			= System(2, tile0, tile1, battle_bg)
	system:load()

	ski = system:getSkills() --La lista hasta este punto va bien

	skillselectOne 	= SkillSelection(skill_bg, ski)
	skillselectTwo 	= SkillSelection(skill_bg, ski)

	state = gamestates.menu

	musicThingy.menu:play()
end

function love.draw()
	if (state == gamestates.menu) then 
		menu:draw()
	end

	if (state == gamestates.skillSelectionOne) then
		love.graphics.setColor(150, 50, 50)
		skillselectOne:draw()

	end

	if (state == gamestates.skillSelectionTwo) then
		love.graphics.setColor(50, 50, 150)
		skillselectTwo:draw()
	end


	if (state == gamestates.battle) then
		love.graphics.setColor(255, 255, 255)
		system:draw()
	end

	if (state == gamestates.gameOver) then
		love.graphics.draw(credits_bg, 0, 0)
	end 
end

function love.update(dt)
	if (state == gamestates.menu) then 
		for _, m in pairs(musicThingy) do
    		m:update(dt)
  		end
	end
	if (state == gamestates.skillSelectionOne) then
		if (false) then
			state = gamestates.skillSelectionTwo
		end
	end
	if (state == gamestates.skillSelectionTwo) then
		if (false) then
			state = gamestates.battle
		end
	end
	if (state == gamestates.battle) then
		system:update(dt)
		for _, m in pairs(musicThingy) do
    		m:update(dt)
  		end
	end
	if (state == gamestates.gameOver) then
	end
end

function love.keypressed(key)
	if (key == "escape") then
		love.event.quit()
	end

	if (state == gamestates.menu) then 
		if (key == "return") then
			state = gamestates.skillSelectionOne
		end
		if (key == "c") then
			state = gamestates.gameOver
		end
	end

	if (state == gamestates.skillSelectionOne) then
		if (key == "return") then
			for i, v in ipairs(skillselectOne:getSkillset()) do
				system:teachSkill(1, v)
				test = v:getName()
			end
			state = gamestates.skillSelectionTwo
		end
	end

	if (state == gamestates.skillSelectionTwo) then
		if (key == "return") then
			for i, v in ipairs(skillselectTwo:getSkillset()) do
				system:teachSkill(2, v)
			end
			state = gamestates.battle
			if (state == gamestates.battle) then
				musicThingy.menu:stop()
				musicThingy.backgroundIntro:play()
				musicThingy.backgroundIntro.onEnd = function() musicThingy.backgroundMusic:play() end
			end
		end
	end

	if (state == gamestates.battle) then
		system:move(key)
	end

	if (state == gamestates.gameOver) then
		if (key == "return") then
			state = gamestates.battle
				if (state == gamestates.battle) then
				musicThingy.menu:stop()
				musicThingy.backgroundIntro:play()
				musicThingy.backgroundIntro.onEnd = function() musicThingy.backgroundMusic:play() end
			end
		end
	end
end


