local TutorialConst = require "app/module/tutorial/tutorial_const"

local TutorialFinishListener = {}

function TutorialFinishListener:init()
end

function TutorialFinishListener:clear()
	ModuleManager.TutorialManager:removeAllEventListeners()
end

-- 检查是否已经完成条件，没有的话如果有监听则添加监听
function TutorialFinishListener:checkAndAddListener(tutorialId)
	local finishType = DataManager.TutorialData:getFinishTypeById(tutorialId)
	local func = TutorialFinishListener._addListener[finishType]
	if func then
		return func(tutorialId)
	end
	return false
end

function TutorialFinishListener:getIsHaveListener(tutorialId)
	local finishType = DataManager.TutorialData:getFinishTypeById(tutorialId)
	local func = TutorialFinishListener._addListener[finishType]
	if func then
		return true
	end
	return false
end

-- 当UI完全打开
local function _listenerOnUIShow(tutorialId)
	local params = DataManager.TutorialData:getFinishTypeParams()
	if params == nil then -- 打开界面的完成条件必须要有参数
		return false
	end
	local uiPath = TutorialConst.UI_PATH[params]
	local uiObj = UIManager:getUIByIndex(uiPath)
	if uiObj and uiObj:getIsShowComplete() then
		return true
	end
	ModuleManager.TutorialManager:addEventListener(EventManager.CUSTOM_EVENT.UI_SHOW_COMPLETE, function(index)
		if index == uiPath then
			ModuleManager.TutorialManager:finishAndNextTutorial(tutorialId)
		end
	end)
	return false
end

-- 当UI关闭
local function _listenerOnUIClose(tutorialId)
	local params = DataManager.TutorialData:getFinishTypeParams()
	if params == nil then -- 打开界面的完成条件必须要有参数
		return false
	end
	local uiPath = TutorialConst.UI_PATH[params]
	local uiObj = UIManager:getUIByIndex(uiPath)
	if uiObj == nil or uiObj:isClosed() then
		return true
	end
	ModuleManager.TutorialManager:addEventListener(EventManager.CUSTOM_EVENT.UI_CLOSE, function(index)
		if index == uiPath then
			ModuleManager.TutorialManager:finishAndNextTutorial(tutorialId)
		end
	end)
	return false
end

local function _listenerOnCanElimination(tutorialId)
	ModuleManager.TutorialManager:addEventListener(EventManager.CUSTOM_EVENT.BOARD_FILL_OVER, function(index)
		ModuleManager.TutorialManager:finishAndNextTutorial(tutorialId)
	end)
	return false
end

local function _listenerOnEliminationOver(tutorialId)
	local elementPosList = DataManager.TutorialData:getTargetElement()
	if not elementPosList then
		return false
	end
	ModuleManager.TutorialManager:addEventListener(EventManager.CUSTOM_EVENT.ELIMINATION_OVER, function(posIdmap)
		local over = true
		for _, posId in ipairs(elementPosList) do
			if not posIdmap[posId] then
				over = false
			end
		end

		if over then
			if ModuleManager.BattleManager.battleController then
				ModuleManager.BattleManager.battleController:onLinkOver()
			end
			ModuleManager.TutorialManager:finishAndNextTutorial(tutorialId)
		else
			if ModuleManager.BattleManager.battleController then
				ModuleManager.BattleManager.battleController:clearGridSequence()
				EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.SHOW_ELIMINATION_TUTORAIL, elementPosList)
			end
		end
	end)
	return false
end

TutorialFinishListener._addListener = {
	[TutorialConst.FINISH_TYPE.ON_UI_SHOW] = _listenerOnUIShow,
	[TutorialConst.FINISH_TYPE.ON_UI_CLOSE] = _listenerOnUIClose,
	[TutorialConst.FINISH_TYPE.ON_BOARD_CAN_ELIMINATION] = _listenerOnCanElimination,
	[TutorialConst.FINISH_TYPE.ON_ELIMINATION_OVER] = _listenerOnEliminationOver,
}

return TutorialFinishListener