level_modes = 
{
	quest          = 'quest',
	hidden_object  = 'hidden_object',
	mini_game      = 'mini_game',
	hidden_picture = 'hidden_picture',
	featured       = 'featured',
}

function registerLevel(short_alias, name, path, resource_path, mode, parent_level_name)
	if not levels then
		levels = {}
	end
	if not levels_index_map then
		levels_index_map = {}
	end

	assert(not levels[name])

	levels[name]                    = {}
	levels[name].path               = path
	levels[name].resource_path      = resource_path.textures
	levels[name].videos_path        = resource_path.videos
	levels[name].sounds_path        = resource_path.sounds
	levels[name].mode               = mode
	levels[name].parent_level_name  = parent_level_name
	levels[name].short_alias        = short_alias
	-- if IsPreloadManagerEnabled() then
	--     levels[name].resources          = GResourcePreloadManager:ParseFolder(resource_path.textures)
	-- end

	levels_index_map[short_alias] = name
end

local isRegisterScenes = false

function RegisterScenes()
	if isRegisterScenes then
		return
	end
	isRegisterScenes = true

	local edition_types = 
					{
						sample    = 0,
						collector = 1
					}

	local function RegisterLevelEx(level_info, level_type, edition_type)
		local INFO_NAME   = 1
		local INFO_PARENT = 2
		local INFO_MODE   = 3

		local sc_p
		local mg_p
		local res_sc_p
		local res_mg_p
		local sounds_sc_p
		local sounds_mg_p

		local tex_dir = {
			[edition_types.sample] = 
					{
						[level_modes.quest] = 'data/Textures/Scenes/',
						[level_modes.hidden_object] = 'data/Textures/HO/',
						[level_modes.hidden_picture] = 'data/Textures/HP/',
						[level_modes.mini_game] = 'data/Textures/Minigames/',
						[level_modes.featured] = 'data/Textures/Scenes/',
					},
			[edition_types.collector]  = 
					{
						[level_modes.quest] = 'data/ExtraContent/Textures/Scenes/',
						[level_modes.hidden_object] = 'data/ExtraContent/Textures/HO/',
						[level_modes.hidden_picture] = 'data/ExtraContent/Textures/HP/',
						[level_modes.mini_game] = 'data/ExtraContent/Textures/Minigames/',
						[level_modes.featured] = 'data/ExtraContent/Textures/Scenes/',
					}
			}


		local src_dir = {
			[edition_types.sample] = 
					{
						[level_modes.quest] = 'data/Scripts/Scenes/',
						[level_modes.hidden_object] = 'data/Scripts/HO/',
						[level_modes.hidden_picture] = 'data/Scripts/HP/',
						[level_modes.mini_game] = 'data/Scripts/Minigames/',
						[level_modes.featured] = 'data/Scripts/Scenes/',
					},
			[edition_types.collector]  = 
					{
						[level_modes.quest] = 'data/ExtraContent/Scripts/Scenes/',
						[level_modes.hidden_object] = 'data/ExtraContent/Scripts/HO/',
						[level_modes.hidden_picture] = 'data/ExtraContent/Scripts/HP/',
						[level_modes.mini_game] = 'data/ExtraContent/Scripts/Minigames/',
						[level_modes.featured] = 'data/ExtraContent/Scripts/Scenes/',
					}
			}


		local video_dir = {
			[edition_types.sample] = 
					{
						[level_modes.quest] = 'data/Video/Scenes/',
						[level_modes.hidden_object] = 'data/Video/HO/',
						[level_modes.hidden_picture] = 'data/Video/HP/',
						[level_modes.mini_game] = 'data/Video/Minigames/',
						[level_modes.featured] = 'data/Video/Scenes/',
					},
			[edition_types.collector]  = 
					{
						[level_modes.quest] = 'data/ExtraContent/Video/Scenes/',
						[level_modes.hidden_object] = 'data/ExtraContent/Video/HO/',
						[level_modes.hidden_picture] = 'data/ExtraContent/Video/HP/',
						[level_modes.mini_game] = 'data/ExtraContent/Video/Minigames/',
						[level_modes.featured] = 'data/ExtraContent/Video/Scenes/',
					}
			}


		local sound_dir = {
			[edition_types.sample] = 
					{
						[level_modes.quest] = 'data/Sounds/Scenes/',
						[level_modes.hidden_object] = 'data/Sounds/HO/',
						[level_modes.hidden_picture] = 'data/Sounds/HP/',
						[level_modes.mini_game] = 'data/Sounds/Minigames/',
						[level_modes.featured] = 'data/Sounds/Scenes/',
					},
			[edition_types.collector]  = 
					{
						[level_modes.quest] = 'data/ExtraContent/Sounds/Scenes/',
						[level_modes.hidden_object] = 'data/ExtraContent/Sounds/HO/',
						[level_modes.hidden_picture] = 'data/ExtraContent/Sounds/HP/',
						[level_modes.mini_game] = 'data/ExtraContent/Sounds/Minigames/',
						[level_modes.featured] = 'data/ExtraContent/Sounds/Scenes/',
					}
			}

		sc_p        = src_dir[edition_type][level_type]
		res_sc_p    = tex_dir[edition_type][level_type]
		videos_sc_p = video_dir[edition_type][level_type]
		sounds_sc_p = sound_dir[edition_type][level_type]

		local level_postfix    = {[level_modes.quest]='',      [level_modes.hidden_object]='ho',  [level_modes.mini_game]='mg', [level_modes.hidden_picture]='hp', [level_modes.featured]='' }
		local name_postfix     = {[level_modes.quest]='',      [level_modes.hidden_object]='_HO', [level_modes.mini_game]='',   [level_modes.hidden_picture]='_HP', [level_modes.featured]=''}
		local res_dir          = {[level_modes.quest]='',      [level_modes.hidden_object]='',    [level_modes.mini_game]='',   [level_modes.hidden_picture]='', [level_modes.featured]='ft'}
		local level_ce_postfix = {[edition_types.sample]='', [edition_types.collector]='_ce'}

		if  level_type == level_modes.hidden_object or
			level_type == level_modes.hidden_picture
		then
			level_info[INFO_PARENT] = level_info[INFO_PARENT] or level_info[INFO_NAME]
		end

		local base_name = level_info[INFO_NAME]
		local level_index = string.gsub(base_name, '^0*(%d+).*', '%1') .. level_postfix[level_type]
		local parent_name = level_info[INFO_PARENT] and level_info[INFO_PARENT] or nil
		local mode = level_info[INFO_MODE] or level_type

		if levels_index_map then
			local index_post = 1
			local qt_start_char = string.byte('a')
			local index_post_char
			local index = level_index

			while( levels_index_map[index .. level_ce_postfix[edition_type]] ) do
				index_post_char = level_type==level_modes.quest and string.char(qt_start_char+index_post-1) or tostring(index_post)
				index = level_index .. index_post_char

				index_post = index_post+1
			end

			level_index = index .. level_ce_postfix[edition_type]
		end

		local level_name = 'l_'..base_name .. name_postfix[level_type]
		
		local resource_path = {
				textures = res_sc_p    .. base_name .. '/' .. res_dir[level_type],
				videos   = videos_sc_p .. base_name .. '/' .. res_dir[level_type],
				sounds   = sounds_sc_p .. base_name .. '/' .. res_dir[level_type],
			}

		registerLevel(level_index, level_name, sc_p..base_name..'/'..base_name .. name_postfix[level_type] ..'.lua', resource_path, mode, parent_name)

		levels[level_name].level_type = level_type
		levels[level_name].collectors_edition = edition_type==edition_types.collector
	end

	local function registerLevels(levels, level_type, edition_type)
		for i,level_info in ipairs(levels) do
			RegisterLevelEx(level_info, level_type, edition_type)
		end
	end

	-- register all levels
	local quest_levels =
	{
	    {'00_Boat'},
	    {'01_Boat_crash_site'},
	    {'02_Mountain_path'},
	    {'03_Panorama'},
	    {'04_Gazebo'},
	    {'05_Forest_path'},
	    {'06_Town'},
	    {'07_Cantellos_shop'},
	    {'08_Lauras_room'},
	    {'09_Andreas_house'},
	    {'10_Dead_ship_shore'},
	    {'11_Drawbridge'},
	    {'12_Shipyard'},
	    {'13_Hydroplane'},
	    {'14_Captains_room'},
	    {'16_Forest_path'},
	    {'25_Hidden_laboratory'},
	    {'51_Forest_path'},
	    {'52_Forest_path'},
	    {'53_Forest_path'},
	}
	
	local ho_levels =
	{		
		{'04_Gazebo', '04_Gazebo'},
		{'08_Lauras_room', '08_Lauras_room'},
		{'10_Dead_ship_shore', '10_Dead_ship_shore'},
		{'12_Shipyard', '12_Shipyard'},	
		{'18_Forest_lake','16_Forest_path'},
	}

	local hp_levels =
	{	
		{'20_Water_wheel_Blacksmith', '04_Gazebo'},
	}

	local mg_levels = 
	{
		{ '04_Guitar', 	'04_Gazebo' },
		{ '06_Wall',	'06_Town',					level_modes.quest},
		{ '07_Jimmy',	'07_Cantellos_shop'	},
		{ '10_Chest',	'10_Dead_ship_shore' },
		{ '11_Bridge', '11_Drawbridge' },
		{ '14_Radar',    '04_Gazebo',level_modes.quest},
		{ '15_Ship',	'14_Captains_room'},
		{ '20_Lever',	'14_Captains_room', level_modes.quest},
		{'23_Gate',    '04_Gazebo',level_modes.quest},
		{'29_Star', '04_Gazebo' },
		{'33_Time',		'04_Gazebo'}
	}

	local featured_levels = 
	{
		{'Room'},
    }

	registerLevels(quest_levels, 	level_modes.quest, edition_types.sample)
	registerLevels(ho_levels,    	level_modes.hidden_object, edition_types.sample)
	registerLevels(hp_levels,    	level_modes.hidden_picture, edition_types.sample)
	registerLevels(mg_levels,    	level_modes.mini_game, edition_types.sample)
	registerLevels(featured_levels,	level_modes.featured, edition_types.sample)


	if isCollectorsEdition() then
		local quest_levels =
		{
		    {'01_CE_Shipyard'},
            {'02_CE_Town'},
            {'03_CE_Cantellos_shop'},
            {'04_CE_Lauras_room'},
            {'05_CE_Bazar'},
            {'06_CE_Andreas_house'},
            {'07_CE_Excavation_site'},
            {'08_CE_Time_device'},
            {'09_CE_Labyrinth'},
            {'10_CE_Field_near_temple'},
            {'11_CE_Temple'},
            {'12_CE_Altar_chambers'},
            {'13_CE_Grotto'},
		}

		local ho_levels =
		{
			{'03_CE_Cantellos_shop', '03_CE_Cantellos_shop'},
			{'05_CE_Bazar', 		 '05_CE_Bazar'},
			{'11_CE_Temple', 		 '11_CE_Temple'},
		}

		local hp_levels =
		{

		}

		local mg_levels =
		{
			-- {'02_CE_Bugs',		'02_CE_Tree',			level_modes.quest},
		}

		registerLevels(quest_levels, level_modes.quest, edition_types.collector)
		registerLevels(ho_levels,    level_modes.hidden_object, edition_types.collector)
		registerLevels(hp_levels,    level_modes.hidden_picture, edition_types.collector)
		registerLevels(mg_levels,    level_modes.mini_game, edition_types.collector)
	end
end