require 'constants'

function love.run()
	if love.load then love.load(love.arg.parseGameArguments(arg), arg) end

	-- We don't want the first frame's dt to include time taken by love.load.
	if love.timer then love.timer.step() end

	local dt = 0

	-- Main loop time.
	return function()
		-- Process events.
		if love.event then
			love.event.pump()
			for name, a,b,c,d,e,f in love.event.poll() do
				if name == "quit" then
					if not love.quit or not love.quit() then
						return a or 0
					end
				end
				love.handlers[name](a,b,c,d,e,f)
			end
		end

		-- Update dt, as we'll be passing it to update
		if love.timer then dt = love.timer.step() end

		-- Call update and draw
		if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled

		if love.graphics and love.graphics.isActive() then
			love.graphics.origin()
			love.graphics.clear(love.graphics.getBackgroundColor())

			if love.draw then love.draw() end

			love.graphics.present()
		end

		if love.timer then love.timer.sleep(0.001) end
	end
end

function love.load()
	Object = require("libs/classic")
	require("player")
	require("bullet")

	WF = require("libs/windfield")
	STI = require("libs/sti")

	require("Game/UpdateGame")
	require("Menu/UpdateMenu")
	require("Pause/UpdatePause")

	require("Game/DrawGame")
	require("Menu/DrawMenu")
	require("Pause/DrawPause")

	require("Game/GameKeyPressed")
	require("Menu/MenuKeyPressed")
	require("Pause/PauseKeyPressed")

	require("mapsloader")

	--WindField
	World = WF.newWorld(0, 0) --no gravity
	World:setQueryDebugDrawing(true) -- Draws the area of a query for 10 frames
	World:addCollisionClass("Player1")
	World:addCollisionClass("Bullet1")

	World:addCollisionClass("Player2")
	World:addCollisionClass("Bullet2")

	World:addCollisionClass("Wall")
	World:addCollisionClass("New") -- Used to make sure the bullet doesn't collide with the player that shot it

	--STI Map
	loadMap(1)

	--Fonts used in the game
	GameFont = love.graphics.newFont("assets/Daydream.ttf", 60)
	DebugFont = love.graphics.newFont("assets/Daydream.ttf", 12)
	MenuFont = love.graphics.newFont("assets/Daydream.ttf", 45)

	--Game consts
	HEALTH = 3
	DELAY = 0.5
	MAX = 6 --MAX number of bullets|

	--Bullet lists
	Bullets1 = {}
	Bullets2 = {}
	
	DebugFlag = false
	EnableKeyPress1 = true
	KeyPressTime1 = 0
	KeyDelay1 = DELAY

	EnableKeyPress2 = true
	KeyPressTime2 = 0
	KeyDelay2 = DELAY

	local playerSpeed = 12000
	UserPlayer1 = Player(1, 1000, 100, HEALTH, "assets/player1.png", playerSpeed)
	UserPlayer2 = Player(2, 200, 300, HEALTH, "assets/player2.png", playerSpeed)

	-- Music streaming
	musicMenu = love.audio.newSource("music/menu.mp3", "stream")
	musicBattle = love.audio.newSource("music/battle.mp3", "stream")
	musicStory = love.audio.newSource("music/story.mp3", "stream")

	musicPause = musicBattle:clone()
	musicPause:setFilter{
		type = 'lowpass',
		volume = 0.3,
		highgain = 0.4,
	}

end

function love.keypressed(key)
	if _G.GAMESTATE == "MENU" then
		MenuKeyPressed(key)
	end
	if _G.GAMESTATE == "PAUSE" then
		PauseKeyPressed(key)
	end
	if _G.GAMESTATE == "GAME" then
		GameKeyPressed(key)
	end

end

function love.update(dt)
	if _G.GAMESTATE == "MENU" then
		UpdateMenu(dt)
		-- Handle music
		if not musicMenu:isPlaying() then
			musicMenu:setVolume(0.5)
			love.audio.play(musicMenu)
		end
	end
	if _G.GAMESTATE == "PAUSE" then
		UpdatePause(dt)
		-- Handle music
		if not musicPause:isPlaying() then
			--musicBattle:setVolume(0)
			--musicPause:setVolume(0.5)
			love.audio.play(musicBattle)
			love.audio.play(musicPause)
		end
	end
	if _G.GAMESTATE == "GAME" then
		UpdateGame(dt)
		-- Handle music
		if not musicBattle:isPlaying() then
			--musicBattle:setVolume(0.5)
			--musicPause:setVolume(0)
			love.audio.play(musicBattle)
			love.audio.play(musicPause)
		end
	end
end

function love.draw()
	if _G.GAMESTATE == "MENU" then
		DrawMenu()
	end
	if _G.GAMESTATE == "PAUSE" then
		DrawPause()
	end
	if _G.GAMESTATE == "GAME" then
		DrawGame()
		if DebugFlag then
			love.graphics.setFont(DebugFont)
			love.graphics.print("Debug Mode", 1200, 850)
			love.graphics.print("" .. GAMESTATE, 200, 200)
		end
	end

end
