local metaObjectLevels = {}
metaObjectLevels[1] = "data/Scenes/Meta/1_1.lua"
metaObjectLevels[2] = "data/Scenes/Meta/2_1.lua"
metaObjectLevels[3] = "data/Scenes/Meta/3_2.lua"
metaObjectLevels[4] = "data/Scenes/Meta/4_2.lua"
metaObjectLevels[5] = "data/Scenes/Meta/5_2.lua"
metaObjectLevels[6] = "data/Scenes/Meta/6_3.lua"
metaObjectLevels[7] = "data/Scenes/Meta/7_3.lua"
metaObjectLevels[8] = "data/Scenes/Meta/8_3.lua"
metaObjectLevels[9] = "data/Scenes/Meta/9_4.lua"
metaObjectLevels[10] = "data/Scenes/Meta/10_4.lua"
metaObjectLevels[11] = "data/Scenes/Meta/11_5.lua"
metaObjectLevels[12] = "data/Scenes/Meta/12_5.lua"
metaObjectLevels[13] = "data/Scenes/Meta/13_5.lua"
metaObjectLevels[14] = "data/Scenes/Meta/14_6.lua"
metaObjectLevels[15] = "data/Scenes/Meta/15_6.lua"
metaObjectLevels[16] = "data/Scenes/Meta/16_7.lua"
metaObjectLevels[17] = "data/Scenes/Meta/17_7.lua"
metaObjectLevels[18] = "data/Scenes/Meta/18_8.lua"
metaObjectLevels[19] = "data/Scenes/Meta/19_8.lua"
metaObjectLevels[20] = "data/Scenes/Meta/20_9.lua"
metaObjectLevels[21] = "data/Scenes/Meta/21_9.lua"
metaObjectLevels[22] = "data/Scenes/Meta/22_9.lua"
metaObjectLevels[23] = "data/Scenes/Meta/23_10.lua"
metaObjectLevels[24] = "data/Scenes/Meta/24_10.lua"
metaObjectLevels[25] = "data/Scenes/Meta/25_10.lua"
metaObjectLevels[26] = "data/Scenes/Meta/26_11.lua"
metaObjectLevels[27] = "data/Scenes/Meta/27_11.lua"
metaObjectLevels[28] = "data/Scenes/Meta/28_11.lua"
metaObjectLevels[29] = "data/Scenes/Meta/29_12.lua"
metaObjectLevels[30] = "data/Scenes/Meta/30_12.lua"
metaObjectLevels[31] = "data/Scenes/Meta/31_12.lua"
metaObjectLevels[32] = "data/Scenes/Meta/32_13.lua"
metaObjectLevels[33] = "data/Scenes/Meta/33_13.lua"
metaObjectLevels[34] = "data/Scenes/Meta/34_13.lua"
metaObjectLevels[35] = "data/Scenes/Meta/35_14.lua"
metaObjectLevels[36] = "data/Scenes/Meta/36_14.lua"

function GetMetaObjectLevelPath(id)
	for i, v in ipairs(metaObjectLevels) do
		if i == id then
			return v
		end
	end
end

function GetMetaObjectLevelId(path)
	for i, v in ipairs(metaObjectLevels) do
		if v == path then
			return i
		end
	end
end

function OnMetaLevelWin()
    local currentProfile = GetProfileManager():GetCurrentProfile()
    currentProfile.found_objects_ = {}
    local r = GetMetaObjectLevelId(play_field:GetName())
    local path = GetLevelByRealId(r)
    local i, r  = GetCurLevelId(path)
	while GetLevelPath(i) ~= "" and r <= GetLevelsCount() do
		local cf = GetProfileManager():GetCurrentProfile().cigars_found
		path = GetLevelPath(i + 1)		
		if path ~= "" then
            for k, w in ipairs(cf) do
                local x, y = GetCurLevelId(path)
                if k == y and not w then
                    play_field:GetLevelScreen():ScreenDestroy()
                    StartMetaLevel(metaObjectLevels[k])					
                    return
                end
            end
        end
		local j = 0
		j, r = GetCurLevelId(path)
		i = i + 1		
	end
	TimerDo(GetGameManager():GetTimer(),
            0.5,
            function()
                local cp = GetProfileManager():GetCurrentProfile()
                local all_cigars_found = true
                for i, v in ipairs(cp.cigars_found) do
                    all_cigars_found = all_cigars_found and v
                end
                if all_cigars_found then
                    cp.exit_to_menu_immediately = true
                else
                    cp.exit_to_menu_immediately = false
                end
                load_screen('data/Lua/Screens/GlobalMap.lua')
                play_field:GetLevelScreen():ScreenDestroy()
            end) 
end

function CreateBackButton()
    local up = CreateAnimation('data/Texture/GUI/MetaGame/back_button.png',0,0,74,74,1,0)
	up:SetHotSpot(37,37);
    local over = CreateAnimation('data/Texture/GUI/MetaGame/back_button.png',0,74,74,74,1,0)
    over:SetHotSpot(37,37);    
    
    local anim = CreateAnimation('data/Texture/GUI/MetaGame/back_w.png',0,0,124,111,1,0)
	anim:SetHotSpot(62,55)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('back_w',anim,964,53,499,0,1,1)
    
	local button = CreateButton(up, over, over, "CloseMetaGame", play_field:GetLevelScreen())
	button:AttachTo(play_field)
	button:SetX(972)
	button:SetY(55)
	button:SetZ(500)
	play_field.back_button = button
	--button:SetRect(-17, -14, 34, 28)
    return button
end

function StartMetaLevel(path)
    load_level(path)
    play_field.meta_level = true
	play_field:GetLevelScreen():GetTaskList():SetExtraTaskText(GetString("CIGAR_TASK"))
	play_field:GetLevelScreen():GetTaskList():CenterExtraTask()
	cigar_button:Stop()
	
	CreateBackButton()
	
	OnNotebookClosed = function()
        OnMetaLevelWin()
    end
end