dofile('data/Lua/Constants.lua')
dofile('data/Lua/Helpers.lua')
dofile('data/Lua/HelpersMinigames.lua')
dofile('data/Lua/HelpersComics.lua')
dofile('data/Scenes/Comics/ComicsLazer.lua')
dofile('data/Scenes/Comics/HelpersComicsOneSMS.lua')
dofile('data/Scenes/Comics/HelpersComicsOneReplicHeroes.lua')
dofile('data/Lua/HelpersGUI.lua')
dofile('data/Lua/FunctionDispatcher.lua')
dofile('data/Lua/LevelBasicPolicies.lua')
dofile('data/Lua/PhotoZone.lua')
dofile('data/Lua/ZoomZone.lua')
dofile('data/Lua/ReferenceCounter.lua')
dofile('data/Lua/Console.lua')
dofile('data/Lua/ProfileManager.lua')
dofile('data/Lua/Utilities.lua')
dofile('data/Lua/GameState.lua')
dofile('data/Lua/InitSound.lua')
dofile('data/Lua/InfoItemZone.lua')
dofile('data/Lua/InteractiveHelp.lua')
dofile('data/Lua/ActiveZone.lua')

dofile('data/Scenes/Animation/anim_sea_gull.lua')
dofile('data/Scenes/Animation/anim_rat_eating.lua')
dofile('data/Scenes/Animation/anim_rat_run.lua')
dofile('data/Scenes/Animation/anim_minigame_complete.lua')

local date = os.date('*t')
math.randomseed(os.clock() + date.sec + date.min + date.hour + date.day + date.month + date.year)

-----------------------------------------------------------------------------------------
-- Init levels
local levels = {}
local table_buffer = {}
levels[0] = ''
levels[1] = 'data/Scenes/1_booth_.lua'
levels[2] = 'data/Scenes/2_prow_.lua'
levels[3] = 'data/Scenes/3_ship_galley_.lua'
levels[4] = 'data/Scenes/4_ferruginous_ship_.lua'
levels[5] = 'data/Scenes/5_ferruginous_ship_inside_.lua'
levels[6] = 'data/Scenes/13_balkony_.lua'
levels[7] = 'data/Scenes/9_train_.lua'
levels[8] = 'data/Scenes/6_rock_skull_day_.lua'
levels[9] = 'data/Scenes/18_arbour_.lua'
levels[10] = 'data/Scenes/10_like_.lua'
levels[11] = 'data/Scenes/11_car_.lua'
levels[12] = 'data/Scenes/23_statues.lua'
levels[13] = 'data/Scenes/survey.lua'
levels[13] = 'data/Scenes/12_arm_chair_.lua'
levels[14] = 'data/Scenes/8_boat_.lua'
levels[15] = 'data/Scenes/22_beach.lua'
levels[16] = 'data/Scenes/17_tank_.lua'
levels[17] = 'data/Scenes/16_straw_umbrella_.lua'
levels[18] = 'data/Scenes/14_skin_diver_.lua'
levels[19] = 'data/Scenes/15_under_water_.lua'    
levels[20] = 'data/Scenes/19_skeleton_.lua'    
levels[21] = 'data/Scenes/21_helicopter_.lua'
levels[22] = 'data/Scenes/27_wall_.lua'
levels[23] = 'data/Scenes/32_mosque_.lua'
levels[24] = 'data/Scenes/33_entrance_temple_.lua'
levels[25] = 'data/Scenes/29_firetemple_2.lua'
levels[26] = 'data/Scenes/7_house_1.lua'
levels[27] = 'data/Scenes/20_house_2.lua'
levels[28] = 'data/Scenes/24_airplane_.lua'
levels[29] = 'data/Scenes/25_krim_.lua'
levels[30] = 'data/Scenes/30_entrance_cave_.lua'
levels[31] = 'data/Scenes/31_cave_.lua'
levels[32] = 'data/Scenes/28_buddha_.lua'
levels[33] = 'data/Scenes/34_temple_1_.lua'
levels[34] = 'data/Scenes/35_temple_2_.lua'
levels[35] = 'data/Scenes/36_temple_3_.lua'
levels[36] = 'data/Scenes/26_tractor_.lua'

function PrepareTableBuffer()
    if table.getn(table_buffer) ~= 0 then
        return
    end
    for i, v in pairs(levels) do
        table.insert(table_buffer, i)
    end -- for
    table.sort(table_buffer)
end

function GetLevelToLoad()
    PrepareTableBuffer()
	
	return levels[1]
end

function GetCurLevelId(cur_level)
    PrepareTableBuffer()
    for i, v in pairs(levels) do
        if cur_level == v then
            -- тут делаем поиск по таблице буферных индексов
            for k, w in pairs(table_buffer) do
                if i == w then
					return k, w
                end
            end
        end
    end
    
    assert('No exist next level')
end

function GetFirstLevelPath()
    return levels[1]
end

function GetLevelPathByID(id)
	for i, path in pairs(levels) do
		if i == id then
			return path
		end
	end
	return nil
end

function StartLevelById(id)
    local id = tonumber(id)
	local path = GetLevelPathByID(id)
    if path == nil then
		Console:Log("There is no level with id ".. id)
	else
		StartLevel(path)
	end
end

function FinishLevel(cur_level)
    DestroyCurrentLevel()
    if StartNextLevel(cur_level) then
        return
    end
end

function StartNextLevel(cur_level)
    local level = GetNextLevel(cur_level)
	if not level then
		return false
    end
    
    StartLevel(level)
	return true
end

function GetNextLevel(cur_level)
	local k, r = GetCurLevelId(cur_level)
    if k == table.maxn(table_buffer) then
        return levels[1]
	else
	    local level_id = levels[ table_buffer[k + 1] ]
	    if level_id ~= "" then
	        return level_id
	    else
	        return nil
	    end
	end	
end

function AppFocusLost()
    local pf = play_field	

    if  pf and pf:IsPaused() == false and
        IsExistMiniGame() == false
    then
        CreateInGameMenu()
        GetGameApp():SetDontSuspend(true)
    end
    
    if IsExistMiniGame() and game_state.screen_mini_game:IsPaused() == false then
        local screen = CreateInGameMenu()
        screen:SetXY(-200, 0)
        GetGameApp():SetDontSuspend(true)
    end
end

function AppFocusGain()
    if play_field then
        GetGameApp():SetDontSuspend(false)
    end
end

function DisplayMessage(title, message)
    local current_screen = GetGameManager():GetCurrentScreen()
    if current_screen and current_screen:GetName() == 'data/Lua/Screens/DialogMessage.lua' then
        return
    end

    local screen = load_screen('data/Lua/Screens/DialogMessage.lua')
    screen:SetLayer(140)
    SetCaptions(screen, title, message)
end

function PrecacheAnimationResources()
     GetTexture('data/Texture/Animations/spider_move/blic.png')
     GetTexture('data/Texture/Animations/spider_move/eyes.png')
     GetTexture('data/Texture/Animations/spider_move/lapa.png')
     GetTexture('data/Texture/Animations/spider_move/pautikka.png')
     GetTexture('data/Texture/Animations/spider_move/telo.png')
     
     GetTexture('data/Texture/Animations/sea_gull/ch_1.png')
     GetTexture('data/Texture/Animations/sea_gull/ch_2.png')
     GetTexture('data/Texture/Animations/sea_gull/ch_3.png')
     GetTexture('data/Texture/Animations/sea_gull/ch_4.png')
     GetTexture('data/Texture/Animations/sea_gull/ch_5.png')
     
     GetTexture('data/Texture/Animations/rat_eating/r_hand_front.png')
     GetTexture('data/Texture/Animations/rat_eating/r_hand_front2.png')
     GetTexture('data/Texture/Animations/rat_eating/r_head.png')
     GetTexture('data/Texture/Animations/rat_eating/r_head2.png')
     GetTexture('data/Texture/Animations/rat_eating/r_leg.png')
     GetTexture('data/Texture/Animations/rat_eating/r_tail1.png')
     GetTexture('data/Texture/Animations/rat_eating/r_tail2.png')
     GetTexture('data/Texture/Animations/rat_eating/r_torse1.png')
     GetTexture('data/Texture/Animations/rat_eating/r_torse2.png')
     GetTexture('data/Texture/Animations/rat_eating/r_torse3.png')
     GetTexture('data/Texture/Animations/rat_eating/root.png')
     GetTexture('data/Texture/Animations/rat_eating/shadow.png')
     
     GetTexture('data/Texture/Animations/fire/fire.png')
     
     GetTexture('data/Texture/Animations/sea/blic_sea.png')
end

function PrecacheMenuResources()
    GetTexture('data/Texture/Main_menu/bg.jpg')
    GetTexture('data/Texture/Main_menu/logo_small.png')
    
    GetTexture('data/Texture/Main_menu/play.png')
    GetTexture('data/Texture/Main_menu/options.png')
    GetTexture('data/Texture/Main_menu/credits.png')
    GetTexture('data/Texture/Main_menu/quit.png')
    GetTexture('data/Texture/Main_menu/change_p.png')
    
    GetTexture('data/Texture/Main_menu/anim/most.png')
    GetTexture('data/Texture/Main_menu/anim/most2.png')
end

function PrecacheGameResources()
    GetTexture('data/Texture/Minigames/1_Booth_Cards/card_1.dds')
    GetTexture('data/Texture/Minigames/1_Booth_Cards/card_2.dds')
    GetTexture('data/Texture/Minigames/1_Booth_Cards/card_3.dds')
    GetTexture('data/Texture/Minigames/1_Booth_Cards/card_4.dds')
    GetTexture('data/Texture/Minigames/1_Booth_Cards/card_5.dds')
end

    