dofile('data/Scripts/Constants.lua')
dofile('data/Scripts/Helpers.lua')
dofile('data/Scripts/LevelBasicPolicies.lua')
dofile('data/Scripts/ReferenceCounter.lua')
dofile('data/Scripts/SoundInit.lua')
dofile('data/Scripts/ComicsInit.lua')
dofile('data/Scripts/_test.lua')
dofile('data/Scripts/Console.lua')

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

DoTest();	--// (!) DEBUG

LoadSounds();

------------------------------------------------------------------------------
--//
globalLastLevelIndex = -1;

function SetLastLevelIndex(index)
	globalLastLevelIndex = index;
end

function GetLastLevelIndex()
	return globalLastLevelIndex;
end

function CompleteLevel()
	GTimerDoString('playField:GetLevelScreen():ScreenDestroy()', 0.9);
	if (globalLastLevelIndex < 25) then
		GTimerDoString('load_level(GetLevelMap()[00], false)', 1.0);
	else
		GTimerDoString('load_level(GetLevelMap()[26], false)', 1.0);
	end
end

function StartLevel(levelName, restoringFromSave)
    local levelNameToStart = GetFirstLevelPath();
    if (IsCorrectLevelName(levelName) and GetLevelIndex(levelName) ~= 26) then 
        levelNameToStart = levelName;
    elseif (restoringFromSave) then
        restoringFromSave = false;
    end
    SetLastLevelIndex(GetLevelIndex(levelNameToStart));
--// (!) Restoring data for global map only
	local restoring = restoringFromSave;	--(GetLevelIndex(levelNameToStart) == 0 and restoringFromSave or false);
    load_level(levelNameToStart, restoring and GetGameManager():GetProfileManager():GetCurrentProfile():GetLastLevelStateDump(false) ~= '');
end

function ApplyLevelDump(applyDump)
    assert(applyDump ~= nil and type(applyDump) == 'boolean');
    
    if (applyDump == true) then
    	local dump = GetGameManager():GetProfileManager():GetCurrentProfile():GetLastLevelStateDump(false);
        DoString(dump);
    end
end
------------------------------------------------------------------------------
--//
function GetLevelMap()
	local levels = {};
	local addLevel = function (levelIndex, levelFileName)
		assert(levels[levelIndex] == nil);
		levels[levelIndex] = levelFileName;
	end

--local surveyBuildEnabled = true;

	addLevel(00, 'data/Scripts/Scenes/00_GlobalMap.lua');
	addLevel(01, 'data/Scripts/Scenes/01_TempleNearTheSea.lua');
	addLevel(02, 'data/Scripts/Scenes/02_InsideTempleNearTheSea.lua');
	addLevel(03, 'data/Scripts/Scenes/03_Camp.lua');
	addLevel(04, 'data/Scripts/Scenes/04_ElevatorInCave.lua');
	addLevel(05, 'data/Scripts/Scenes/05_OutOfCave.lua');
if (surveyBuildEnabled == true) then
	addLevel(06, 'data/Scripts/Scenes/06_AtCollapsedAirshipSB.lua');
else
	addLevel(06, 'data/Scripts/Scenes/06_AtCollapsedAirship.lua');
	addLevel(07, 'data/Scripts/Scenes/07_HutOnTree.lua');
	addLevel(08, 'data/Scripts/Scenes/08_HutOnTree2.lua');
	addLevel(09, 'data/Scripts/Scenes/09_AtAbandonedShip.lua');
	addLevel(10, 'data/Scripts/Scenes/10_UndergroundGarden.lua');
	addLevel(11, 'data/Scripts/Scenes/11_AboriginalCamp.lua');
	addLevel(12, 'data/Scripts/Scenes/12_ShamansHut.lua');
	addLevel(13, 'data/Scripts/Scenes/13_TribeLeaderHut.lua');
	addLevel(14, 'data/Scripts/Scenes/14_TempleEntrance.lua');
	addLevel(15, 'data/Scripts/Scenes/15_InsideTemple.lua');
	addLevel(16, 'data/Scripts/Scenes/16_TempleHall.lua');
	addLevel(17, 'data/Scripts/Scenes/17_CandleCave.lua');
	addLevel(18, 'data/Scripts/Scenes/18_AncientTeleport.lua');
	addLevel(19, 'data/Scripts/Scenes/19_DinoField.lua');
	addLevel(20, 'data/Scripts/Scenes/20_NearCollapsedShip.lua');
	addLevel(21, 'data/Scripts/Scenes/21_Volcano.lua');
	addLevel(22, 'data/Scripts/Scenes/22_NearBalloon.lua');
	addLevel(23, 'data/Scripts/Scenes/23_NearSacrificePlace.lua');
	addLevel(24, 'data/Scripts/Scenes/24_AtSacrificePlace.lua');
	addLevel(25, 'data/Scripts/Scenes/25_NearTheCopter.lua');
	addLevel(26, 'data/Scripts/Scenes/26_Outro.lua');
end
	return levels;
end

------------------------------------------------------------------------------
--//
function IsCorrectLevelName(levelName)
    local levelMap = GetLevelMap();
    for levelIndex, levName in pairs(levelMap) do
        if (levelName == levName) then
            return true;
        end
    end
    return false;
end

------------------------------------------------------------------------------
--//
function GetLevelIndex(levelName)
    local levelMap = GetLevelMap();
    for levelIndex, levName in pairs(levelMap) do
        if (levelName == levName) then
            return levelIndex;
        end
    end
    assert(false and 'Unregistered level name');
    return 0;
end

------------------------------------------------------------------------------
--//
function GetFirstLevelPath()
	local levelFileName = GetLevelMap()[00];
	assert(levelFileName ~= nil and levelFileName ~= '');
	return levelFileName;
end

------------------------------------------------------------------------------
--//
function GetMinigameFileNameByIndex(minigameIndex)
    local minigameMap = GetMinigameMap();
    if (minigameMap[minigameIndex] ~= nil) then
		return minigameMap[minigameIndex];
	end
	return '';
end

function GetMinigameMap()
	local minigames = {};
	local addMinigame = function (minigameIndex, minigameFileName)
		assert(minigames[minigameIndex] == nil);
		minigames[minigameIndex] = minigameFileName;
	end

	addMinigame(00, '');
	addMinigame(01, 'data/Scripts/Minigames/DrumsGate.lua');
	addMinigame(02, 'data/Scripts/Minigames/Platforms.lua');
	addMinigame(03, 'data/Scripts/Minigames/Clock.lua');
	addMinigame(04, 'data/Scripts/Minigames/Pinions.lua');
	addMinigame(05, 'data/Scripts/Minigames/Dino.lua');
	addMinigame(06, 'data/Scripts/Minigames/Pipes.lua');
	addMinigame(08, 'data/Scripts/Minigames/Zeppelin.lua');
	addMinigame(13, 'data/Scripts/Minigames/Ballance.lua');
	addMinigame(18, 'data/Scripts/Minigames/Teleport.lua');
	addMinigame(20, 'data/Scripts/Minigames/Ship.lua');
	addMinigame(21, 'data/Scripts/Minigames/MapPazzle.lua');
	addMinigame(24, 'data/Scripts/Minigames/Lock.lua');

	return minigames;
end

