-------------------------------------------------------------------------------
class 'ResourceManager'
-------------------------------------------------------------------------------


local theResourceManager = nil

function GetResourceManager()
	if not theResourceManager then
		theResourceManager = ResourceManager()
	end
	return theResourceManager
end

-------------------------------------------------------------------------------

function ResourceManager:__init(size_cache)
	if OS_DESKTOP == nil then
		return
	end

	local platform  = Application:getPlatform()
	local memory_total = platform:getTotalPhysMemorySize()/1024/1024
	
	if memory_total < 1400 then
		return
	end

	self.size_cache = size_cache or 200

	self.resource	= {}

	self.cacheAnchorSprite = CreateSpriteEntity('')
	self.cacheAnchorSprite:setName('ResourceManager')
	self.cacheAnchorSprite:setRespondable(false)
	self.cacheAnchorSprite:setActive(false)
	self.cacheAnchorSprite:setVisible(false)
	self.cacheAnchorSprite:setOpacity(0)
	self.cacheAnchorSprite:setOrder(70000)
	
	self.origCreateSpriteEntity = CreateSpriteEntity
	CreateSpriteEntity = bind(self.creatorCreateSpriteEntity, self)

	self.origCreateSpriteEntityInstant = CreateSpriteEntityInstant
	CreateSpriteEntityInstant = bind(self.creatorCreateSpriteEntityInstant, self)

	self.origCreateCompositionElement = CreateCompositionElement
	CreateCompositionElement = bind(self.creatorCreateCompositionElement, self)

	WriteInfo('ResourceManager init (cashe = '..self.size_cache..' )')
end

function ResourceManager:setSizeCache(size_cache)
	if size_cache then
		self.size_cache = size_cache
	end
end

function ResourceManager:cacheTexture(path)
	local cacheSprite = self.origCreateSpriteEntity(path)
	cacheSprite:setName(path)
	cacheSprite:attachTo(self.cacheAnchorSprite)
	cacheSprite:hide()
	cacheSprite:setActive(false)

	table.insert(self.resource, {path = path, sprite = cacheSprite})
end

function ResourceManager:cache(path)
	self:checkMemory()

	if self:isNeededCache(path) then
		self:cacheTexture(path)
	end
end

function ResourceManager:remove_first_resource()
	local sprite = self.resource[1].sprite
	sprite:setTexture('')
	-- print('remove texture = '..sprite:getName())
	sprite:destroy()
	table.remove(self.resource, 1)
end

function ResourceManager:creatorCreateSpriteEntity(path)
	self:cache(path)

	return self.origCreateSpriteEntity(path)
end

function ResourceManager:creatorCreateSpriteEntityInstant(path)
	self:cache(path)

	return self.origCreateSpriteEntityInstant(path)
end

function ResourceManager:creatorCreateCompositionElement(path)
	self:cache(path)

	return self.origCreateCompositionElement(path)
end

function ResourceManager:checkMemory()
	while true do
		if table.size(self.resource) == 0 then
			break
		end

		local size_memory = getEntityTreeTexturesMemoryUsage(self.cacheAnchorSprite)/1024/1024
		-- print('size_memory = '..size_memory)

		if  size_memory > self.size_cache then
			self:remove_first_resource()
		else
			break;
		end
	end
end

function ResourceManager:isNeededCache(path)
	if string.len(path) == 0 then
		return false
	end

	for i, v in pairs(self.resource) do
		if v.path == path then
			local index = table.size(self.resource)
			self.resource[i], self.resource[index] = self.resource[index], self.resource[i]
			
			return false
		end
	end 

	local ext = ExtractFileExtention(path)
	if ext == 'webm' then
		return false
	end

	return true
end

function ResourceManager:clear()
	for i, v in pairs(self.resource) do
		local sprite = v.sprite
		sprite:setTexture('')
		sprite:destroy()
	end
end