class 'LoadingScreenBase' (BaseScreen)

function LoadingScreenBase:__init(name)
	BaseScreen.__init(self, name)

	self:setupFill()
	self.loading_time = 2

	if not isCollectorsEdition() then
		self:entityHide('CE')
	end
	
	self:onUpdateLoadingPanel(self.window, 0)
end

function LoadingScreenBase:setupFill()
	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()
end

function LoadingScreenBase:cacheTextures()
	self.timer:add(	function ()
						self:cacheFolderFiles('Data/Textures/Common/')
						self:cacheFolderFiles('Data/Textures/Screens/Notes/')
						self:cacheFolderFiles('Data/Textures/Screens/YesNo/')

						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/Coin/')
						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/')
					end, 0.1)
end

function LoadingScreenBase:onShown()
	self:cacheTextures()
	self.window.onUpdate:add( bind(self.onUpdateLoadingPanel, self) )
end
-------------------------------------------------------------------------------

function LoadingScreenBase:__finalize()
end
-------------------------------------------------------------------------------

function LoadingScreenBase:cacheTexture(path)
	if not self.cacheAnchorSprite then
		self.cacheAnchorSprite = CreateSpriteEntity('')
		self.cacheAnchorSprite:setName('cacheAnchorSprite')
		self.cacheAnchorSprite:setRespondable(false)
		self.cacheAnchorSprite:setActive(false)
		self.cacheAnchorSprite:setVisible(false)
		self.cacheAnchorSprite:setOpacity(0)
		self.cacheAnchorSprite:setOrder(50000)			-- Для того, чтобы кидало в низ лога :)
	end

	local cacheSprite = CreateSpriteEntity(path)
	cacheSprite:setName(path)
	cacheSprite:attachTo(self.cacheAnchorSprite)

	return cacheSprite
end

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

function LoadingScreenBase:cacheFont( path )
	if ( not self.cacheAnchorSprite ) then
		self.cacheAnchorSprite = CreateSpriteEntity( '' )
		self.cacheAnchorSprite:setName( 'cacheAnchorSprite' )
		self.cacheAnchorSprite:setVisible( false )
		self.cacheAnchorSprite:setActive( false )
		self.cacheAnchorSprite:setRespondable( false )
		self.cacheAnchorSprite:setOpacity( 0 )
		self.cacheAnchorSprite:setOrder( 50000 )
	end

	local cacheText = CreateTextEntity()
	cacheText:setFont( path )
	cacheText:attachTo( self.cacheAnchorSprite )

	return cacheText
end

function LoadingScreenBase:cacheFolderFonts( path )
	local data = getResourceFolderFiles( path )
		
	for ind = 0, data:size() - 1 do
		local fontPath = data:get( ind )

		if ( string.sub( fontPath, -4, -1 ) == '.fnt' ) then
			self:cacheFont( fontPath )
		end
	end
end
-------------------------------------------------------------------------------
function LoadingScreenBase:onUpdateLoadingPanel(entity, dt)
	if self.time then
		if self.time > self.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')
			
			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','load_filling')
			
		else -- UPDATE FILLING PANEL
			self.time = self.time + dt
			
			local ratio = self.time/self.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 LoadingScreenBase:onLoadingScreenDestroy()
	PopUpScreensMgr:pop()
	createMainMenu()
end