dofile('data/Scripts/Utilities.lua')
dofile('data/Scripts/LevelDefaultEventHandlers.lua')
dofile('data/Scripts/ZoomZone.lua')
dofile('data/Scripts/ZoomDefaultEventHandlers.lua')
dofile('data/Scripts/GameState.lua')
dofile('data/Scripts/helpers/Helpers.lua')
dofile('data/Scripts/helpers/HelpersCursor.lua')
dofile('data/Scripts/helpers/HelpersDebug.lua')
dofile('data/Scripts/helpers/HelpersEffect.lua')
dofile('data/Scripts/helpers/HelpersGameObject.lua')
dofile('data/Scripts/helpers/HelpersHiddenObject.lua')
dofile('data/Scripts/helpers/HelpersVideo.lua')

dofile('data/Scripts/ho/HiddenBaseLogic.lua')
dofile('data/Scripts/ho/HiddenObjectLogic.lua')
dofile('data/Scripts/ho/HiddenPictureLogic.lua')

dofile('data/Scripts/helpers/LevelHelpers.lua')
dofile('data/Scripts/Timer.lua')
dofile('data/Scripts/Bind.lua')
dofile('data/Scripts/Hint.lua')
dofile('data/Scripts/DogEffect.lua')
dofile('data/Scripts/Subtitles.lua')
--dofile('data/Scripts/collect/CollectObjectsManager.lua')
dofile('data/Scripts/ReferenceCounter.lua')

dofile('data/Scripts/DragZone.lua')
dofile('data/Scripts/comics/BlackBar.lua')
dofile('data/Scripts/comics/Comics.lua')
dofile('data/Scripts/Sound.lua')
dofile('data/Scripts/Console.lua')
dofile('data/Scripts/InitSound.lua')
dofile('data/Scripts/RegisterTools.lua')
dofile('data/Scripts/RegisterScenes.lua')
dofile('data/Scripts/AnimationUtilities.lua')

dofile('data/Scripts/screens/BaseScreen.lua')
dofile('data/Scripts/screens/Inventory.lua')
dofile('data/Scripts/screens/MainMenu.lua')
dofile('data/Scripts/screens/Panel.lua')
dofile('data/Scripts/screens/ScreenManager.lua')
dofile('data/Scripts/screens/LoadingScreen_cycle.lua')
dofile('data/Scripts/screens/OKScreen.lua')
dofile('data/Scripts/screens/YesNoScreen.lua')
dofile('data/Scripts/screens/OptionsScreen.lua')
dofile('data/Scripts/screens/ProfilesScreen.lua')
dofile('data/Scripts/screens/InputNameScreen.lua')
dofile('data/Scripts/screens/HelpScreen.lua')
dofile('data/Scripts/screens/BigFishScreen.lua')
dofile('data/Scripts/screens/ERSGStudioScreen.lua')
dofile('data/Scripts/screens/IntroScreen.lua')
dofile('data/Scripts/screens/NotesScreen.lua')
dofile('data/Scripts/screens/InGameMenu.lua')
dofile('data/Scripts/screens/CreditsScreen.lua')
dofile('data/Scripts/screens/PlayingModeScreen.lua')
dofile('data/Scripts/screens/MoreInfoScreen.lua')
dofile('data/Scripts/screens/MoreGamesScreen.lua')
dofile('data/Scripts/screens/PromoScreen.lua')
dofile('data/Scripts/screens/PortalLogoScreen.lua')

dofile('data/Scripts/Fonts.lua')
dofile('data/Scripts/InteractiveHelp.lua')

if IsCollectorsEdition() and not GM:IsDemoMode() and false then
    dofile('data/ExtraContent/Scripts/screens/ExtraScreen.lua')
    dofile('data/ExtraContent/Scripts/screens/ExtraHelpers.lua')
    dofile('data/ExtraContent/Scripts/screens/ExtraNotesScreen.lua')
    dofile('data/ExtraContent/Scripts/screens/StrategyGuideScreen.lua')
end

--------------------------------------------------------------------------------
exitingApp = false;
--------------------------------------------------------------------------------

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

function GetLevelToLoad(tag)
    tag = tag or DEFAULT_TAG_NAME

    local state = GetGameManager():GetCurrPlayer().gameState
    local level_name
    local last_visited_scene = state:HasState(LAST_VISITED_SCENE, tag)
    
    if last_visited_scene then
        level_name = state:GetState(LAST_VISITED_SCENE, tag):Get(0)
    else
        local index = (tag == CE_TAG_NAME) and '1_ce' or '1'
        level_name = levels_index_map[index]
    end
    return level_name, last_visited_scene
end
--------------------------------------------------------------------------------

function GetCurrentLevel()
	return nil
end
--------------------------------------------------------------------------------

function DestroyCurrentLevel()
    if play_field then
        play_field:GetLevelScreen():DestroySelf()
        play_field = nil
    end
end
--------------------------------------------------------------------------------

function StartGame(tag)
    tag = tag or DEFAULT_TAG_NAME

    StopAmbientMusic(false)
    
    local level_to_load, last_visited_scene = GetLevelToLoad(tag)
    
    if tag == CE_TAG_NAME then
        local level_to_load, last_visited_scene = GetLevelToLoad(tag)
        StartLevel(level_to_load)
    else
        local level_to_load, last_visited_scene = GetLevelToLoad(tag)
        if last_visited_scene then
            StartLevel(level_to_load)
        else
            StartLevel(level_to_load)
            --CreateIntroScreen()
        end
    end -- if tag == CE_TAG_NAME then
end
--------------------------------------------------------------------------------

function CleanUpGameResources()
    PopUpScreensMgr:DestroyAll()
    exitConfirmationScreen = nil

    PlayAmbientMusic(nil)
    active_zones:PopAll()
    
    hidden_zoom_zones.zones:PopAll()
    hidden_zoom_zones.count:PopAll()
    
    DestroyCurrentLevel()
    
    GTimerClear()
    GTimerContinue()
end
--------------------------------------------------------------------------------
function InitResources()
    InitFonts()
    PreloadMusic()
    PreloadCommonSounds()
    PreloadTestVoice()
    PreloadAmbientSounds()
end
--------------------------------------------------------------------------------
function StartApplication()
    local showIntro = not IsDebugMode()
    
    PlayAmbientMusic('music_loading', true)

    if showIntro then
        if IsFileExists(PORTAL_LOGO_FILE_NAME) then
            CreatePortalLogoScreen()
        else
            CreateBigFishScreen()
        end
    else
        CreateMainMenu()
    end
end
--------------------------------------------------------------------------------

function AppExit()
    exitingApp = true
    CleanUpGameResources()
    APP:PerformExit()
end
--------------------------------------------------------------------------------

function AppFocusLost()
    if play_field and not IsDebugMode() then
        RestoreCursor()
		CreateInGameMenu()
		--[[
		if InteractiveHelp.tutorialScreen then
		    InteractiveHelp.tutorialScreen.screen:SetOpacity(0)
            play_field:Stop()
		end
		]]
    end
end
--------------------------------------------------------------------------------
function AppFocusGain()
end
--------------------------------------------------------------------------------
function FullScreenChange()
    if optionsScreen then
        optionsScreen.fullscreenCheckbox:SetState(APP:IsFullScreen() and 1 or 0)
        SetFullscreen()
    end
end
--------------------------------------------------------------------------------
function ToBeContinued()
    TBCScreen = BuildOKScreen("TRIAL_COMPLETED", "EndTBCScreen")
    PopUpScreensMgr:Push(TBCScreen)
end
--------------------------------------------------------------------------------

function EndTBCScreen()
    TBCScreen = nil
    CleanUpGameResources()

    StartMainMenu()
end
-------------------------------------------------------------------------------

function ShowEndOfSurvayDialog()
	local screen = CreateOKScreen( GetString('END_OF_SURVEY'), OnEndOfSurveyDo)
	screen:GetAssociatedLuaObject().textBox:SetBorderSize(530, 100)
	screen:GetAssociatedLuaObject().textBox:SetScale(0.9)
	PopUpScreensMgr:Push(screen)
end
-------------------------------------------------------------------------------

function OnEndOfSurveyDo()
	--AppExit()
    CleanUpGameResources()
	CreatePromoScreen()
end
-------------------------------------------------------------------------------

function ShowDemoDialog()
    GetCurrentLevel().timer:Add(
        function()
            CreateOKScreen( GetString('DEMO_COMPLETED'), OnEndOfDemo)
        end, 0.5)
end

function ShowCurrentVersionCompletedDialog()
    GetCurrentLevel().timer:Add(
        function()
            CreateOKScreen( GetString('END_OF_CURRENT_VERSION'), OnEndOfDemo)
        end, 0.5)
end
-------------------------------------------------------------------------------

function OnEndOfDemo()
    if GetCurrentLevel() then
        GetCurrentLevel().mouseBlocker:Push()
    end
    
    PopUpScreensMgr:DestroyAll() 
    
    --CreatePromoScreen()
    GetCurrentLevel().timer:Add(function()
                        CleanUpGameResources()
                        CreateMainMenu()
                    end, 1)    
end
-------------------------------------------------------------------------------