dofile('Data/Scripts/Common/InitCommon.lua')

dofile('Data/Scripts/Hint/Hint.lua')
dofile('Data/Scripts/Hint/Glint.lua')

dofile('Data/Scripts/Utils/Constants.lua')
dofile('Data/Scripts/Utils/Profiler.lua')
dofile('Data/Scripts/Utils/Subtitles.lua')
dofile('Data/Scripts/Utils/Helpers/HelpersHiddenObject.lua')
dofile('Data/Scripts/Utils/Helpers/Helpers.lua')
dofile('Data/Scripts/EventDispatcher.lua')

dofile('Data/Scripts/HOLogic/HiddenObjectIndicator.lua')
dofile('Data/Scripts/HOLogic/HiddenPictureIndicator.lua')
dofile('Data/Scripts/HOLogic/HiddenObjectLogicED.lua')
dofile('Data/Scripts/HOLogic/HiddenPictureLogic.lua')

dofile('data/Scripts/Screens/Match3HelpScreen.lua')
dofile('data/Scripts/Screens/Match3HelpScreenSmall.lua')
dofile('data/Scripts/Screens/Match3AbilityInfoScreen.lua')

dofile('Data/Scripts/Screens/GameScene.lua')
dofile('Data/Scripts/Screens/PlayField.lua')
dofile('Data/Scripts/Screens/Inventory.lua')
dofile('Data/Scripts/Screens/ZoomZone.lua')
dofile('Data/Scripts/Screens/HOPanel.lua')
dofile('Data/Scripts/Screens/HPPanel.lua')
dofile('Data/Scripts/Screens/MGPanel.lua')
dofile('Data/Scripts/Screens/ToolFound.lua')
dofile('Data/Scripts/Screens/ToolFoundUnique.lua')
dofile('Data/Scripts/Screens/InformScreen.lua')
dofile('Data/Scripts/Screens/Panel.lua')
dofile('Data/Scripts/Screens/BlackBar.lua')
dofile('Data/Scripts/Screens/ComicsPanel.lua')
dofile('Data/Scripts/Screens/CutScene.lua')
dofile('Data/Scripts/Screens/ControlsPanel.lua')
dofile('Data/Scripts/Screens/Note.lua')
dofile('Data/Scripts/Screens/AchievmentUnlocked.lua')
dofile('Data/Scripts/Action/ActionScreen.lua')

dofile('Data/Scripts/Collect/CollectObjectsManager.lua')
dofile('Data/Scripts/Utils/InformManager.lua')

dofile('Data/Scripts/Utils/GameTasksTracker.lua')

dofile('Data/Scripts/Comics/Comics.lua')

dofile('Data/Scripts/Tools/Tools.lua')
dofile('Data/Scripts/Tools/CollectionTools.lua')

dofile('Data/Scripts/RegisterScenes.lua')
dofile('Data/Scripts/Editor.lua')
dofile('Data/Scripts/Console.lua')
dofile('Data/Scripts/InitScreens.lua')
dofile('Data/Scripts/InitFonts.lua')
dofile('Data/Scripts/InitCursor.lua')

dofile('Data/Scripts/Tutorial.lua')
dofile('Data/Scripts/Inform/InformContent.lua')
dofile('Data/Scripts/Achievments/RSLAchievment.lua')

dofile('data/Scripts/Match3/RegisterMatch3.lua')
dofile('data/Scripts/match3/M3Game.lua')
dofile('data/Scripts/Labyrinth/LabyrinthScreen.lua')

function initGame()
	math.randomseed( os.time() )
	
	--WriteError("123456")
	
	-- Инициализация текстов
	StringManager:loadStrings(STRINGS_FILE_PATH, false)
	
    -- Инициализируем курсоры
    initCursor(GetCursorEntity())  --DDD - temporary
    getOnCursorInit():add(initCursor)

    -- Регистрируем все сцены
    RegisterScenes()

    -- Загружаем игрока
    do
        current_player = ProfilesManager:getCurrentProfile()
    end
    
    if isDebugMode() then
        createMainMenu()
    else
        createBigFishScreen()
    end
end

function destroyCurrentScene()
    if current_scene then
        current_scene:getWindow():destroy()
        
        if current_scene:getPlayField() then
            current_scene:getPlayField():destroy()
        end
        
        current_scene = nil
    end
end

function getLevelToLoad(tag)
    tag = tag or DEFAULT_TAG_NAME

    local level_name
    local last_visited_scene = HasGameState(LAST_VISITED_SCENE, nil, tag)
    
    if last_visited_scene then
        level_name = 'l_'..GetGameState(LAST_VISITED_SCENE, tag):get(0)
    else
        local index = (tag == CE_TAG_NAME) and '1_ce' or '0'
        level_name = levels_index_map[index]
    end
    return level_name, last_visited_scene
end

function startGame(tag)
    tag = tag or DEFAULT_TAG_NAME
    
    local level_to_load, last_visited_scene = getLevelToLoad(tag)
    
    startLevel(level_to_load)    
end

function startLevel(level_to_load)
    local levelName = string.sub(level_to_load, 3) 

    if current_scene then
        current_scene:loadPlayField( levelName )
    else
        current_scene = GameScene(levelName)
        PopUpScreensMgr:push(current_scene, false, nil)
    end
end

function exitToMainMenu()
    PopUpScreensMgr:destroyAll()
    
    destroyCurrentScene()    
    createMainMenu()
end

function appFocusGain()
    Console:pushText('appFocusGain')
    local top_screen = PopUpScreensMgr:GetTop()
    if current_scene and top_screen:getName() == 'PauseScreen' then
        top_screen:onScreenDestroy()
    end
end

function appFocusLost()
    Console:pushText('appFocusLost')
    local top_screen = PopUpScreensMgr:GetTop()
    if current_scene and top_screen:getName() ~= 'PauseScreen' then
        createPauseScreen()
    end
end

function createMainSoundEntity()
    local main_sound = CreateAmbientTrack('Data/Sounds/GUI/MainMenu/MainMenu_Internoise.ogg') -- CreateSoundTrack
    main_sound:setName(NAME_MAIN_SOUND)
    main_sound:setRepeated(true)
    main_sound:setFadeDuration(0)
    main_sound:setFading(0)
    main_sound:play()

    return main_sound
end

function createMainMusicEntity()
    local index = math.random(3, 4)
    local main_music = CreateMusicTrack('Data/Music/music_' .. index .. '.ogg') -- CreateSoundTrack
    main_music:setName(NAME_MAIN_MUSIC)
    main_music:setRepeated(true)
    main_music:setFadeDuration(0)
    main_music:setFading(0)
    main_music:play()

    return main_music
end