local BaseModule = class("BaseModule")

function BaseModule:scheduleGlobal(func, inter)
	local sid = SchedulerManager:scheduleGlobal(func, inter)
	if self._schedulerIds == nil then
		self._schedulerIds = {}
	end
	table.insert(self._schedulerIds, sid)
	return sid
end

function BaseModule:resumeScheduleGlobal(sid)
	SchedulerManager:resume(sid)
end

function BaseModule:pauseScheduleGlobal(sid)
	SchedulerManager:pause(sid)
end

function BaseModule:performWithDelayGlobal(func, delay)
	local sid = SchedulerManager:performWithDelayGlobal(func, delay)
	if self._schedulerIds == nil then
		self._schedulerIds = {}
	end
	table.insert(self._schedulerIds, sid)
	return sid
end

function BaseModule:unscheduleGlobal(sid)
	if self._schedulerIds == nil then
		return
	end
	for k, v in ipairs(self._schedulerIds) do
		if v == sid then
			table.remove(self._schedulerIds, k)
			break
		end
	end
	SchedulerManager:unscheduleGlobal(sid)
end

function BaseModule:addEventListener(key, func, priority)
	local tag = EventManager:addEventListener(key, func, priority)
	self._baseEventListeners = self._baseEventListeners or {}
	self._baseEventListeners[key] = tag
end

function BaseModule:removeEventListener(key)
	if self._baseEventListeners and self._baseEventListeners[key] then
		EventManager:removeEventListener(key, self._baseEventListeners[key])
		self._baseEventListeners[key] = nil
	end
end

function BaseModule:unscheduleAll()
	if self._schedulerIds == nil then
		return
	end

	for i = 1, #self._schedulerIds do
		SchedulerManager:unscheduleGlobal(table.remove(self._schedulerIds))
	end
end

function BaseModule:removeAllEventListeners()
	if self._baseEventListeners == nil then
		return
	end
	for key, tag in pairs(self._baseEventListeners) do
		EventManager:removeEventListener(key, tag)
		self._baseEventListeners[key] = nil
	end
end

-- 阻塞式，等待服务器回复以后再回调callback
function BaseModule:sendMessage(msgName, params, responseData, callback, getType, lockGame)
	if lockGame == nil then
		lockGame = true
	end
	NetManager:send(self, msgName, params, responseData, callback, lockGame, nil, getType)
end

-- 阻塞式，等待所有指令完成以后再发送此消息，并且锁界面，等待服务器回复以后再回调callback
function BaseModule:sendMessageTillBeforeOver(msgName, params, responseData, callback, getType)
	NetManager:sendTillBeforeOver(self, msgName, params, responseData, callback, true, nil, getType)
end

-- 各个模块的manager按各自需求来重写clear即可
function BaseModule:clear()
end

function BaseModule:_clear()
	self:unscheduleAll()
	self._schedulerIds = nil
	self:removeAllEventListeners()
	self._baseEventListeners = nil
	self:clear()
end

return BaseModule