local BattlePool = {}

function BattlePool:init()
	self.effectPool = self:createEffectPool()
end

function BattlePool:getEffectPool()
	return self.effectPool
end

function BattlePool:createObjPool()
	local pool = {
		dataPool = {},
		num = 0
	}

	function pool:pop()
		local obj = nil
		if pool.num > 0 then
			obj = pool.dataPool[pool.num]
			pool.dataPool[pool.num] = nil
			pool.num = pool.num - 1
		end
		return obj
	end

	function pool:push(obj)
		pool.num = pool.num + 1
		pool.dataPool[pool.num] = obj
	end
	return pool
end

function BattlePool:createEffectPool()
	local poolObj = BaseObject:create()
	poolObj:initWithGameObject(CS.UnityEngine.GameObject())
	poolObj:dontDestroyOnLoad()
	poolObj:setActive(false)
	local pool = {
		dataPool = {},
		poolObj = poolObj,
		poolCount = 0
	}

	function pool:pop(path)
		local obj = nil
		local p = pool.dataPool[path]
		if p and #p > 0 then
			obj = table.remove(p)
			if #p == 0 then
				pool.poolCount = pool.poolCount - 1
			end
		end
		return obj
	end

	function pool:push(obj, notSetParent)
		local path = obj:getAssetPath()
		local p = pool.dataPool[path]
		if p == nil then
			p = {}
			pool.dataPool[path] = p
		end
		if #p == 0 then
			pool.poolCount = pool.poolCount + 1
		end
		table.insert(p, obj)
		if not notSetParent then
			obj:setParent(pool.poolObj, false)
		end
	end

	function pool:getPoolCount()
		return pool.poolCount
	end

	function pool:getRoot()
		return pool.poolObj
	end

	function pool:clearPool(path)
		local p = pool.dataPool[path]
		if p then
			local count = #p
			for i = 1, count do
				local obj = table.remove(p)
				obj:destroy()
			end
			pool.poolCount = pool.poolCount - 1
		end
	end

	return pool
end

function BattlePool:clear()
	self.effectPool.poolObj:destroy()
	self.effectPool = nil
end

return BattlePool