-- Load some default values for our rectangle.
require("playBub")
require("envComp")
require("helpFun")
require("levelsData")
require("UI")
wf = require("libs.windfield")
WinW = 1000
WinH = 800

--{"Game","MainMenu","LevelMenu","Pause","Fini"}
State = "MainMenu"
function love.load()
	font = love.graphics.newFont(30)
	cyan = { 37 / 255, 253 / 255, 233 / 255 }
	love.window.setMode(800, 600, { resizable = true })
	love.graphics.setDefaultFilter("nearest", "nearest")
	world = wf.newWorld(0, 0, true)
	world:addCollisionClass("Bubble")
	world:addCollisionClass("Wall")
	world:addCollisionClass("Tube")
	world:addCollisionClass("Spike", { ignores = { "Bubble" } })
	world:addCollisionClass("Turner", { ignores = { "Bubble" } })
	world:addCollisionClass("PetBubble", { ignores = { "Bubble" } })
	--Images
	PlayBubImg = love.graphics.newImage("asset/playBub.png")
	PlayBubAnim = { love.graphics.newImage("asset/playBub1.png"), love.graphics.newImage("asset/playBub2.png")
 }
	PetBubImg = love.graphics.newImage("asset/petBub.png")

	TubeEImg = love.graphics.newImage("asset/tubeE.png")
	TubeSImg = love.graphics.newImage("asset/tubeS.png")
	TubeNImg = love.graphics.newImage("asset/tubeN.png")
	TubeWImg = love.graphics.newImage("asset/tubeW.png")

	TurnerAnim = { love.graphics.newImage("asset/turner1.png"), love.graphics.newImage("asset/turner2.png")
 }
	--horizontal img
	ImgHs = {}
	ImgHs.Wall = love.graphics.newImage("asset/WallH.png")
	ImgHs.Spike = love.graphics.newImage("asset/SpikeH.png")
	for key, value in pairs(ImgHs) do
		value:setWrap("repeat", "clampzero")
	end

	--vertical img
	ImgVs = {}
	ImgVs.Wall = love.graphics.newImage("asset/WallV.png")
	ImgVs.Spike = love.graphics.newImage("asset/SpikeV.png")
	for key, value in pairs(ImgVs) do
		value:setWrap("clampzero", "repeat")
	end
	--Sound
	DeadSund = love.audio.newSource("asset/pop.mp3", "static")
	MoveSund = love.audio.newSource("asset/bloup.mp3", "static")
	BkgMusic = love.audio.newSource("asset/Peace.mp3", "stream")
	BkgMusic:setVolume(0.1)

	board = {
		world:newLineCollider(0, 0, WinW, 0),
		world:newLineCollider(0, 0, 0, WinH),
		world:newLineCollider(WinW, 0, WinW, WinH),
		world:newLineCollider(0, WinH, WinW, WinH),
	}
	for i, wall in pairs(board) do
		wall:setType("static")
	end
	Logs = {}

	addLevel()
	--Load Game
	resetLevel()
end

AnimT = 0
-- Increase the size of the rectangle every frame.
function love.update(dt)
	--Music
	if not BkgMusic:isPlaying() then
		BkgMusic:play()
	end
	--  WinH=love.graphics.getHeight()
	--  WinW=love.graphics.getWidth()
	if State == "Game" then
		AnimT = AnimT + 5 * dt
		world:update(dt)
		fan:upd(dt)
		walls_upd(dt)
		playBub:upd(dt)
	end

	--UI upd
end

GScale = 1
GoX, GoY = 0, 0
-- Draw a coloured rectangle.
function love.draw()
	local wW, wH = love.graphics.getWidth(), love.graphics.getHeight()
	GScale = math.min(wW / 1000, wH / 800)
	GoX, GoY = (wW - GScale * 1000) / 2, (wH - GScale * 800) / 2
	love.graphics.translate(GoX, GoY)
	love.graphics.scale(GScale)

	--background

	love.graphics.setColor(0.2, 0.3, 0.4)
	love.graphics.rectangle("fill", 0, 0, WinW, WinH)

	--Game Draw
	if State ~= "MainMenu" and State ~= "LevelMenu" then
		-- world:draw()
		playBub:draw()
		drawEnv()
	end

	--UI
	if UI[State] then
		for index, value in ipairs(UI[State]) do
			value:draw()
		end
	end
	--debug
	if Logs then
		for i, value in ipairs(Logs) do
			love.graphics.print(value .. "\n")
		end
	end
end
function love.mousepressed(x, y, btn)
	UI_mouseprsd(UI[State], x, y, btn)
end
function love.keyreleased(key)
	if State == "Game" then
		playBub:keyrls(key)
	end
end
function love.keypressed(key)
	-- Logs.keydown = key
	-- if key == "h" then
	-- 	playBub.collider:applyLinearImpulse(-1000, 0)
	-- end
	-- if key == "l" then
	-- 	playBub.collider:applyLinearImpulse(1000, 0)
	-- end
	-- if key == "j" then
	-- 	playBub.collider:applyLinearImpulse(0, 1000)
	-- end
	-- if key == "k" then
	-- 	playBub.collider:applyLinearImpulse(0, -1000)
	-- end
	if State == "Game" then
		if key == "r" then
			resetLevel()
		end
		if key == "escape" then
			State = "Pause"
		end
	elseif State == "Pause" and key == "escape" then
		State = "Game"
	elseif State == "Fini" and key == "escape" then
		State = "LevelMenu"
	end
end

cLevelData = levels[1]
function resetLevel()
	WinH = cLevelData.winH or 800
	WinW = cLevelData.winW or 1000
	playBub:resetAt(cLevelData.playBubPos[1], cLevelData.playBubPos[2])
	--Fan
	fan:reset()
	--Petite Bubble
	petBubs:reset(cLevelData)
	walls_reset(cLevelData)
end
