require "startup.lua"

local loadingTime = TimeCounter("Loading time = ")
local version = PermanentStorage.GetGlobalStringOrDef("temp:Ver", "Full")

do
	local loadingTime = TimeCounter("LoadScreen loading time = ")

	LoadResource("fonts.xml")
	
	if PermanentStorage.GetGlobalBool("temp:screensaverLogo") then
		LoadResource("../screenbranding/advertisement/logo.xml")
	end
	
	if version == "1" then
		LoadText("Language_Helloween.xml")
	elseif version == "2" then
		LoadText("Language_Thanks.xml")
	elseif version == "3" then
		LoadText("Language_Christmas.xml")
	else
		LoadText("Language.xml")
	end

	LoadResource("StartupResource.xml")
	LoadTemplate("StartupTemplate.xml")
	LoadControlTree("Startup.xml")
	SendMessage("GameScreen", "template LoadScreenTemplate")   --кладем заставку [

	if PermanentStorage.GetGlobalBoolOrDef("Ask player name", true) and version == "Full"  then
		SendMessage("GameScreen", "defineText [loading_circle] loading_circle_first")
	else
		local tankId = "1"
		local defaultTank = version
		if version == "Full" then
			defaultTank = "1"
		end
        if PermanentStorage.GetGlobalBool("temp:ScreensaverMode") then
            tankId = PermanentStorage.GetLocalStringOrDef("Screensaver Tank", defaultTank)
		else
		    tankId = PermanentStorage.GetLocalStringOrDef("Current Tank", defaultTank)
		end
		local tankType = PermanentStorage.GetLocalStringOrDef(tankId.." Tank Type", version)
		if     tankType == "1" then
			SendMessage("GameScreen", "defineText [loading_circle] loading_circle_helloween")
		elseif tankType == "2" then
			SendMessage("GameScreen", "defineText [loading_circle] loading_circle_thanksgiving")
		elseif tankType == "3" then
			SendMessage("GameScreen", "defineText [loading_circle] loading_circle_christmas")
		end
	end

	SendMessage("GameScreen", "create top instance") -- ] кладем заставку
	
	if PermanentStorage.GetGlobalBool("temp:ScreensaverMode") then
		SendMessage("Startup logo alpha changer", "change alpha to 1.0")
	end

	InstantinateGameLogo("GameLogoLoadScreen", PermanentStorage.GetGlobalBoolOrDef("Ask player name", true)) --создаем GameLogo

	PermanentStorage.SetLocalBool("temp:Startup screen loaded", true)

    loadingTime:WriteToLog()
end
Lock("GameScreen") --блокируем ввод пользователей

require "newplayer.lua"
require "Common.lua"
require "Game.lua"
require "MainMenu.lua"
require "MainScreen.lua"
require "MainScreenAndShop.lua"
require "Shop.lua"
require "ChooseTank.lua"
require "GameField.lua"
require "Tutorial.lua"
require "Help.lua"
require "Holidays.lua"
require "SendMail.lua"

do
    local loadingTime = TimeCounter("Music and sound loading time = ")
    
	if version == "1"     then LoadResource("Music_Halloween.xml")
	elseif version == "2" then LoadResource("Music_Thanks.xml")
	elseif version == "3" then LoadResource("Music_Christmas.xml")
	else                       LoadResource("Music!.xml") end

	if PermanentStorage.GetGlobalBoolOrDef("Ask player name", true) then
        CreateNewPlayer()
	end	
	SwitchTrack()
	
	if (PermanentStorage.IsLocalNameDefined("Music Volume")) then
		Sound:SetMusicVolume(PermanentStorage.GetLocalFloat("Music Volume") * 100)
	else
		Sound:SetMusicVolume(PermanentStorage.GetGlobalFloat("Music Volume") * 100)
	end
	
	if PermanentStorage.GetGlobalBool("temp:ScreensaverMode") then
		if PermanentStorage.GetGlobalBoolOrDef("isScreensaverMute", false) then
			PermanentStorage.SetLocalFloat("Music Volume", 0)
			PermanentStorage.SetLocalFloat("Sound Volume", 0)
			Sound:SetMusicVolume(0)
		end
	end
	
	loadingTime:WriteToLog()
end

do
	local loadingTime = TimeCounter("Templates loading time = ")
	
	LoadTemplate("GUI.xml")
	LoadTemplate("PathTemplates.xml")
	LoadTemplate("TooltipTemplates.xml")
	LoadTemplate("CommonDialogs.xml")
	
	if     version == "1"    then
		LoadTemplate("FishInstances_Halloween.xml")
		LoadTemplate("Credits_Halloween.xml")
		LoadTemplate("BeautyInstances_Halloween.xml")
	elseif version == "2"    then
		LoadTemplate("FishInstances_Thanks.xml")
		LoadTemplate("Credits_Thanks.xml")
		LoadTemplate("BeautyInstances_Thanks.xml")
	elseif version == "3"    then
		LoadTemplate("FishInstances_Christmas.xml")
		LoadTemplate("Credits_Christmas.xml")
		LoadTemplate("BeautyInstances_Cristmas.xml")
	elseif version == "Full" then
		LoadTemplate("FishInstances.xml")
		LoadTemplate("Credits.xml")		
		LoadTemplate("BeautyInstances_Halloween.xml")
		LoadTemplate("BeautyInstances_Thanks.xml")
		LoadTemplate("BeautyInstances_Cristmas.xml")
	else assert(false) end
	
	LoadTemplate("ComfortsTemplate.xml")
	LoadTemplate("TankBackground.xml")
	LoadTemplate("AnyShop.xml")
	LoadTemplate("GameBackgroundsGui.xml")
	LoadTemplate("MainMenu.xml")
	LoadTemplate("MainScreen.xml")
	LoadTemplate("TankOnly.xml")
	LoadTemplate("OptionsDialog.xml")
	LoadTemplate("PlayersDialog.xml")
	LoadTemplate("Intro.xml")
	LoadTemplate("ScreensaverOptionsDialog.xml")
	LoadTemplate("ChooseTank.xml")
	LoadTemplate("HintTextTemplates.xml")
	LoadTemplate("GameField.xml")
	LoadTemplate("Help.xml")
	LoadTemplate("GreetingCards.xml")
	loadingTime:WriteToLog()
end
do
	local loadingTime = TimeCounter("Resource loading time = ")
 	if version == "1" then
		LoadResource("GUI_Resources_Helloween.xml")
		LoadResource("BackgroundResources_halloween.xml")
		LoadResource("TankBackgroundResources_halloween.xml")
		LoadResource("FishResources_Halloween.xml")
		
	elseif version == "2" then
		LoadResource("GUI_Resources_Thanksgiving.xml")
		LoadResource("BackgroundResources_thanksgiving.xml")
		LoadResource("TankBackgroundResources_thanksgiving.xml")
		LoadResource("FishResources_Thanks.xml")
		
	elseif version == "3" then
		LoadResource("GUI_Resources_Christmas.xml")
		LoadResource("BackgroundResources_christmas.xml")
		LoadResource("TankBackgroundResources_christmas.xml")
		LoadResource("FishResources_Christmas.xml")
		
	elseif version == "Full" then
		LoadResource("GUI_Resources.xml")
		LoadResource("BackgroundResources.xml")
		LoadResource("TankBackgroundResources.xml")
		LoadResource("FishResources.xml")
		
	else
		assert(false)
	end
	
	LoadResource("LocalResources.xml")
	
	if version == "Full" or version == "1" then LoadResource("BeautyResources_Helloween.xml") end
	if version == "Full" or version == "2" then LoadResource("BeautyResources_Thanksgiving.xml") end
	if version == "Full" or version == "3" then LoadResource("BeautyResources_Christmas.xml") end

	if PermanentStorage.GetGlobalBool("temp:adVisible") then
		LoadTemplate("../screenbranding/advertisement/templates.xml")
		LoadResource("../screenbranding/advertisement/ad.xml")
	end
	
	LoadResource("Sound.xml")
	LoadResource("video/Resources.xml")	
	loadingTime:WriteToLog()
end
do
	local loadingTime = TimeCounter("ParticlEffects loading time = ")
	LoadEffects("effects/GameBackgrounds.xml")
	LoadEffects("effects/ComfortEffect3.xml")
	LoadEffects("effects/Aquarium.xml")
	LoadEffects("effects/StartEffect.xml")
	LoadEffects("effects/Merry.xml")
	
	require "plstart.lua"
	require "fmstart.lua"
	
	loadingTime:WriteToLog()
end

loadingTime:WriteToLog()

GameFunc("XML loaded")
