-- LevelManager

level_mgr = {}
level_mgr.levels = {}
level_mgr.active_level = nil

level_mgr.no_fade = true
level_mgr.zorder_offset = 2
--level_mgr.zorder_last = 0

----------------------------------------------------------------------------------------------------
function level_mgr:load_level(name, zorder)

	if level_mgr:get_layer(name) ~= nil then
		return level_mgr:get_layer(name)
	end
	
	if zorder == nil then
		zorder = ZORDER_LEVEL0 + #level_mgr.levels + 1
		--level_mgr.zorder_last = zorder
	end
	
	local layer = render_layer:new(name)
	layer_mgr():addLayer(layer:get_parent(), zorder)
	layer_mgr():addLayer(layer.p_layer, zorder+1)
	table.insert(level_mgr.levels, layer)
	layer:get_parent():init()
	layer.zorder = zorder
	
	layer:get_parent():setVisible(false)
	layer:get_parent():setActive(false)
	
	return layer
	
end

----------------------------------------------------------------------------------------------------
function level_mgr:switch_to_level(level_name)

	if level_mgr.no_fade == false then
		fade_panel:fade_in()
		timer_mgr:add_timer(
            function()
                level_mgr:load_level(level_name)
                level_mgr:set_level(level_name)
                if isEnableDebugInfo then
                    DebugInfo:update()
                end
                fade_panel:fade_out()
            end, 1, 1)
	else
		level_mgr:set_level(level_name)
		if isEnableDebugInfo then
		    DebugInfo:update()
        end
	end	

end

----------------------------------------------------------------------------------------------------
function level_mgr:get_layer(name)

	for i = 1, #self.levels do
		if self.levels[i]:get_parent():getName() == name then
			return self.levels[i]
		end
	end
	
	return nil
	
end

----------------------------------------------------------------------------------------------------
function level_mgr:find_element(name)
	
	for i = 1, #self.levels do
		local el = self.levels[i]:get_parent():getElement(name)
		if el ~= nil then
			return el
		end
	end
	
	return nil

end


----------------------------------------------------------------------------------------------------
function level_mgr:set_level(name)

	local x = os.clock()

    --local loading = level_mgr:load_level('Loading1')
    --loading:get_parent():setVisible(true)

    local new_level = level_mgr:get_layer(name)

    if new_level == nil then		
        new_level = level_mgr:load_level(name)
    end
    
    if level_mgr.active_level ~= nil then
        level_mgr.active_level:set_visible(false)
        level_mgr.active_level:set_active(false)
        exec_string(level_mgr.active_level.name..":onHide()")
    end

    new_level:set_visible(true)
    new_level:set_active(true)
    --loading:get_parent():setVisible(false)

    
    
    level_mgr.active_level = new_level
    exec_string(new_level.name..":onShow()")

    trace(string.format('Layer "'..name..'" creation time: %.2f', os.clock() - x))
end


--table.remove(common.hintable_elements, table.find(common.hintable_elements, name))