class 'l_LoadingScreen' (BaseScreen)

local LOADING_TIME = 2

function l_LoadingScreen:__init(name)
    BaseScreen.__init(self, name)
    
    do
		self.time = 0
		
		self.clipping_object = self:getEntityNamed('load_filling')
		local box = self.clipping_object:getBoundingBox()
		self.clipping_object_width = box:getWidth()
		self.clipping_object_height = box:getHeight()
		
		self:entityShow('text_please_wait')
			
        local eff = CreateAnimationEffector()
        eff:setDestroysOnEnd(true)
        eff:attachTo( self:getEntityNamed('text_please_wait') )
        eff:setMode(_ANIM_LOOP)
        eff:setKey(_TRACK_S,  0,        0.95)
        eff:setKey(_TRACK_S,  0.25,	    0.95)
        eff:setKey(_TRACK_S,  0.5,		1)
        eff:setKey(_TRACK_S,  0.75,	    0.95)
        eff:setKey(_TRACK_S,  1,		0.95)
        eff:play()
    end

    self:cacheFolderFiles('Data/Textures/Common/')
    self:cacheFolderFiles('Data/Textures/Screens/Notes/')

    self:cacheFolderFiles('Data/Textures/GUI/Buttons/')
    self:cacheFolderFiles('Data/Textures/GUI/ToolFound/')
    self:cacheFolderFiles('Data/Textures/GUI/Zoom/')
    self:cacheFolderFiles('Data/Textures/GUI/Inventory/')
    self:cacheFolderFiles('Data/Textures/GUI/MG/')
    self:cacheFolderFiles('Data/Textures/GUI/Hint/')
    
    self:cacheFolderFiles('Data/Textures/GUI/ControlsPanel/Anim/Hint/')
    self:cacheFolderFiles('Data/Textures/GUI/ControlsPanel/Anim/Notes/')
    self:cacheFolderFiles('Data/Textures/GUI/ControlsPanel/Buttons/')
    self:cacheFolderFiles('Data/Textures/GUI/ControlsPanel/Layers/')

    self:cacheFolderFiles('Data/Textures/GUI/Comics/')
    
    local music_entity = CreateMusicTrack('Data/Music/music_6.ogg')
	music_entity:setName('main_menu_music')
	music_entity:attachTo(self:getMainEntity())

    music_entity:setRepeated(true)
	music_entity:setFading(1)
	music_entity:play()
end

function l_LoadingScreen:__finalize()
end

function l_LoadingScreen:cacheTexture(path)
	local cacheSprite = CreateSpriteEntity(path)
	cacheSprite:setName(path)
	cacheSprite:setRespondable(false)
	cacheSprite:setActive(false)
	cacheSprite:setVisible(false)
	cacheSprite:setOpacity(0)
	cacheSprite:setOrder(50000)			-- ??? ????, ????? ?????? ? ??? ???? :)

	return cacheSprite
end

function l_LoadingScreen:cacheFolderFiles(path)
	local data = getResourceFolderFiles(path)
		
	for j = 0, data:size() - 1 do
		local texturePath = data:get(j)
		self:cacheTexture(texturePath)
	end
end

function l_LoadingScreen:onUpdate(dt)
	if self.time then
		if self.time > LOADING_TIME then
			 self.time = nil
			 
			local function open(entity, state)
				if state then
					self:onLoadingScreenDestroy()
				end
			end
			self:getMainEntity():setHandlingChildEvents(true)
			self:getMainEntity().onClickBegin:add(open)
			
			self:entityFadeShow('text_anykey')
			self:entityFadeHide('text_please_wait')
			
			local eff = CreateAnimationEffector()
			eff:setDestroysOnEnd(true)
			eff:attachTo( self:getEntityNamed('text_anykey') )
			eff:setMode(_ANIM_LOOP)
			eff:setKey(_TRACK_S,  0,        0.95)
			eff:setKey(_TRACK_S,  0.25,	    0.95)
			eff:setKey(_TRACK_S,  0.5,		1)
			eff:setKey(_TRACK_S,  0.75,	    0.95)
			eff:setKey(_TRACK_S,  1,		0.95)
			eff:play()

			self:entityFadeHide('load_border')
			self:entityFadeHide('load_filling')
			
		else -- UPDATE FILLING PANEL
			self.time = self.time + dt
			
			local ratio = self.time/LOADING_TIME
			local width = -self.clipping_object_width/2 + self.clipping_object_width*ratio
			self.clipping_object:setScissorBox(-self.clipping_object_width/2, -self.clipping_object_height/2, width, self.clipping_object_height/2)
		end
	end
end

function l_LoadingScreen:onLoadingScreenDestroy()
     PopUpScreensMgr:pop()
     local vecProfiles = ProfilesManager:getProfiles()
     if vecProfiles:size() == 0 then
        createIntroScreen()
     else
        createMainMenu()
     end
end


--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function l_LoadingScreen:CreateLoaderTable()
	local objects =
	{
		{ name = 'Loading_SC',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 10,  editingLocked = false,  treeItemIsExpanded = true,  hotSpotX = 683, hotSpotY = 384,  texturePath = 'Data/Textures/Screens/Loading/Loading_SC.jpg',  width = 1366,  height = 768,  },
		{ name = 'text_anykey',  entityType = 'TextEntity',  parentName = 'Loading_SC',  x = 0,  y = -370,  order = 0,  opacity = 0,  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'PRESS_ANY_KEY',  font = 'Data/Fonts/cyrillic.fnt', height = 23,  },
		--{ name = 'CE',  entityType = 'SpriteEntity',  parentName = 'Loading_SC',  x = -21,  y = -356,  order = 0, scale = 0.5,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Logo/CE.png',  width = 1024,  height = 104,  },
		{ name = 'text_please_wait',  entityType = 'TextEntity',  parentName = 'Loading_SC',  x = 14,  y = 324,  order = 0,  opacity = 0,  editingLocked = false,  treeItemIsExpanded = false,  localizationKey = 'LOADING',  font = 'Data/Fonts/cyrillic.fnt', height = 23,  },
		{ name = 'load_filling',  entityType = 'SpriteEntity',  parentName = 'Loading_SC',  x = 1,  y = 355,  order = 20,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Screens/Loading/load_filling.png',  width = 870,  height = 10,  },
		{ name = 'load_border',  entityType = 'SpriteEntity',  parentName = 'Loading_SC',  x = 0,  y = 355,  order = 50,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Loading/load_border.png',  width = 878,  height = 20,  },
	}
	return objects
end
