Lights = {}
LightTimer = 0

function CreateDarkness()
	return love.graphics.newCanvas(game.width/game.scale, game.height/game.scale)
end

function CreateLight(x,y,range,flicker,color,lum)
	local o = {}
	o.pos = {
		x = x,
		y = y
	}
	o.range = range
	o.lum = lum or 1
	o.color = color or {1,1,1}
	o.flicker_value = flicker or 2
	o.flicker = 0
	o.dim = 0
	o.flicker_speed = flicker_speed or 60/12
	o.flicker_time = 0

	table.insert(Lights,o)
	o.id = #Lights
	return o
end

function KillLight(light)
	if light.id ~= nil then
		for _, e in pairs(Lights) do
			if e.id > light.id then
				e.id = e.id - 1
			end
		end
		table.remove(Lights,light.id)
	end
	light = nil
end

function SetDarkness()
	love.graphics.setCanvas(Canvas.Darkness)

	love.graphics.setColor(0,0,0,1)
	love.graphics.rectangle("fill",0,0,game.width	,game.height)

	love.graphics.setCanvas()
end

function DoLights()

	love.graphics.setCanvas(Canvas.Darkness)

	for _, light in pairs(Lights) do
	light.flicker_time = light.flicker_time + 1

	if light.flicker_time >= light.flicker_speed then
		light.flicker_time = light.flicker_time - light.flicker_speed
			light.flicker = 0 + math.random(0,1)
			light.flicker = math.min(math.max(light.flicker, -light.flicker_value),light.flicker_value)
		end
	end

	love.graphics.setBlendMode("replace")
	for _, light in pairs(Lights) do
		if light.range ~= 0 then
			love.graphics.setColor(light.color[1],light.color[2],light.color[3],1)
			local position = {
				x = (light.pos.x - Camera.pos.x) / game.scale,
				y = (light.pos.y - Camera.pos.y) / game.scale
			}
			local range = (1 + light.range + light.flicker) / game.scale
			love.graphics.circle(
				"fill",
				position.x,
				position.y,
				range
			)
		end
	end
	love.graphics.setColor(0,0,0,0)
	for _, light in pairs(Lights) do
		if light.range ~= 0 then
			local position = {
				x = (light.pos.x - Camera.pos.x) / game.scale,
				y = (light.pos.y - Camera.pos.y) / game.scale
			}
			local range = (light.range + light.flicker) / game.scale
			love.graphics.circle(
					"fill",
					position.x,
					position.y,
					range
				)
		end
	end

	love.graphics.setBlendMode("alpha")
	love.graphics.setColor(0,0,0,1)
	Shaders.InsideLight:send("game_scale", game.scale)

	for _, light in pairs(Lights) do
		if light.range ~= 0 then
			local position = {
				x = (light.pos.x - Camera.pos.x) / game.scale,
				y = (light.pos.y - Camera.pos.y) / game.scale
			}
			local range = (light.range + light.flicker) / game.scale
			Shaders.InsideLight:send("light_color", light.color)
			Shaders.InsideLight:send("light_pos", {position.x*game.scale, position.y*game.scale})
			Shaders.InsideLight:send("range", range)
			love.graphics.setShader(Shaders.InsideLight)
			love.graphics.circle(
				"fill",
				position.x,
				position.y,
				range
			)
			love.graphics.setShader()
		end
	end

	love.graphics.setCanvas()

end

function DoBorder()
	love.graphics.setCanvas(Canvas.Darkness)
	love.graphics.setCanvas()
end

function DrawDarkness()
	love.graphics.draw(Canvas.Darkness, 0, 0, 0, 1/game.scale)
end
