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

local TutorialManager = class("TutorialManager", BaseModule)

function TutorialManager:checkAndPlayForceTutorial()
    if DataManager.TutorialData:getIsInTutorial() then
        return
    end
    local haveTutorial = DataManager.TutorialData:getIsHaveTutorial()
	if haveTutorial then
        self:showTutorial()
		return true
    else
        self:stopTutorial()
    end
end

-- 每一步引导都和服务器进行一次通信
function TutorialManager:sendTutorialId(id, callback)
	local args = {
		id = id
	}

	self.sendTutorialIdCallback = callback

	self:sendMessage(ProtoMsgType.FromMsgEnum.GuideFundMarkReq, args, {}, self.sendTutorialIdFinish)
end

function TutorialManager:sendTutorialIdFinish(result)
	if result.err_code == GConst.ERROR_STR.SUCCESS then
		if result.reqData and result.reqData.id then
			BIReport:postTutorialStep(result.reqData.id)
		end

		if self.sendTutorialIdCallback then
			self.sendTutorialIdCallback(true)
		end
	else
		if self.sendTutorialIdCallback then
			self.sendTutorialIdCallback(false)
		end
	end
end

-- 显示引导
function TutorialManager:showTutorial()
	if self.tutorialFinishListener == nil then
		self.tutorialFinishListener = require "app/module/tutorial/tutorial_finish_listener"
	end
	self.tutorialFinishListener:init()

	if self.tutorialAction == nil then
		self.tutorialAction = require "app/module/tutorial/tutorial_action"
	end
	self.tutorialAction:init()

	self:removeAllEventListeners()
	DataManager.TutorialData:setInTutorial(true)
	UIManager:showTutorial()
	if self.tutorialUIComp then
		self.tutorialUIComp:setVisible(true)
		self:startTutorial()
	else
		UIManager:getTutorial(function(tutorialObj)
			self.tutorialUIComp = tutorialObj:addLuaComponent(GConst.TYPEOF_LUA_CLASS.TUTORIAL_UI)
			self:startTutorial()
		end)
	end
end

-- 开始引导
function TutorialManager:startTutorial()
	local tutorialId = DataManager.TutorialData:getTutorialId()
	-- 上报引导每一步
	-- BIReport:postTutorialStep(tutorialId)
	Logger.log("开始引导:%s", tutorialId)

	DataManager.TutorialData:markFuncTutorialFinish(tutorialId)

	-- 如果这一步引导已经完成了就进入下一步，没有完成则如果有监听就添加监听
	local over = self.tutorialFinishListener:checkAndAddListener(tutorialId)
	if over then
		self:finishAndNextTutorial(tutorialId)
	else
		-- 开始当前步骤的引导
		self.tutorialUIComp:onTutorialStart()
		self.tutorialAction:doTutorial(tutorialId)
	end
end

-- 结束引导
function TutorialManager:stopTutorial()
	if DataManager.TutorialData then
		Logger.log("停止引导:%s", DataManager.TutorialData:getTutorialId())
	end
	if self.tutorialFinishListener then
		self.tutorialFinishListener:clear()
	end

	if self.tutorialAction then
		self.tutorialAction:clear()
	end

	self:removeAllEventListeners()
	self:unscheduleAll()
	if DataManager.TutorialData then
		DataManager.TutorialData:stopTutorial()
	end
	if self.tutorialUIComp then
		self.tutorialUIComp:onTutorialStop()
	end
	UIManager:hideTutorial()

	EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.TUTORIAL_TASK_STOP)
end

-- 完成当前步并进入下一步
function TutorialManager:finishAndNextTutorial(tutorialId)
	-- 屏蔽点击
	self.tutorialUIComp:setBlockTouchEnabled(true)
	self.tutorialUIComp:unregisterClickBtn()
	-- 移除所有计时器
	self.tutorialUIComp:clearScheduler()
	-- 移除所有的监听
	self.tutorialFinishListener:clear()
	-- 此步引导完成时需要处理的东西
	self.tutorialAction:doStepOver(tutorialId)

	local haveNext = DataManager.TutorialData:nextTutorial()
	if haveNext then
		self.tutorialUIComp:hideAllTips()
		local delay = DataManager.TutorialData:getDelayStartTime()
		self:performWithDelayGlobal(function()
			self:startTutorial()
		end, delay)
	else
		self:performWithDelayGlobal(function()
			self:stopTutorial()
		end, 0)
	end
end

-- 检查完成条件并进入下一步
function TutorialManager:checkAndNextTutorial(tutorialId)
	local currTutorialId = DataManager.TutorialData:getTutorialId()
	if currTutorialId ~= tutorialId then
		return
	end

	-- 没有完成条件就直接完成，有完成条件就等待完成条件通过再完成
	if not self.tutorialFinishListener:getIsHaveListener(tutorialId) then
		self:finishAndNextTutorial(currTutorialId)
	end
end

-- 添加点击屏幕的监听
function TutorialManager:registerClickScreenListener(callback)
	self.tutorialUIComp:registerClickScreenListener(callback)
end

-- 添加点击指定组件的监听
function TutorialManager:registerClickBtnListener(callback, targetName, clickType)
	self.tutorialUIComp:registerClickBtnListener(callback, targetName, clickType)
end

-- 显示对话
function TutorialManager:showTalk(callback)
	self.tutorialUIComp:showTalk(callback)
end

-- 设置引导时的触摸是否屏蔽
function TutorialManager:setBlockTouchEnabled(enabled)
	self.tutorialUIComp:setBlockTouchEnabled(enabled)
end

function TutorialManager:getBlockTouchEnabled()
	if self.tutorialUIComp == nil then
		return false
	end
	return self.tutorialUIComp:getBlockTouchEnabled()
end

-- 检查功能引导
function TutorialManager:checkFuncTutorial(id, onlyCheck)
	if DataManager.TutorialData:getIsInTutorial() or GFunc.isShenhe() then
        return false
    end
	if not DataManager.TutorialData:getTutorialIdMap(id) then
		return false
	end

	local tutorialFuncStartInfo = ConfigManager:getConfig("tutorial_start")[id]
	if tutorialFuncStartInfo == nil then
		return false
	end
	local tutorialInfo = ConfigManager:getConfig("tutorial")[tutorialFuncStartInfo.start_id]
	if tutorialInfo == nil then
		return false
	end
	if tutorialFuncStartInfo.uires_path == nil or tutorialFuncStartInfo.uires_path == "" then -- 没有绑定界面的就需要额外检查一下开启条件
		return false
	end

	local haveTutorial = DataManager.TutorialData:getIsHaveFuncTutorial(tutorialFuncStartInfo.start_id, tutorialInfo, tutorialFuncStartInfo)
	if onlyCheck then
		return haveTutorial
	end

	if haveTutorial then
        self:showFuncTutorial(DataManager.TutorialData:getTutorialId())
		return true
    end
	return false
end

function TutorialManager:showFuncTutorial(id)
	self:sendTutorialId(id, function(success)
		if EDITOR_MODE or not Platform:getIsPublishChannel() then -- 跳过引导
			if ModuleManager.DevToolManager:getSkip() then
				success = false
			end
		end
		if success then
			if not DataManager.TutorialData:getIsFuncTutorialFinished(id) then
				DataManager.TutorialData:markFuncTutorialFinish(id)
				local uires = DataManager.TutorialData:getTutorialId2UIPath(id)
				Logger.logHighlight(uires)
				if uires == nil or uires == "" then
					self:showTutorial()
				else
					local uiList = UIManager:getUIList()
					for k, v in ipairs(uiList) do
						if v:getPrefabPath() == uires then
							return self:showTutorial()
						end
					end
					self:addEventListener(EventManager.CUSTOM_EVENT.UI_SHOW_COMPLETE, function(uiIndex)
						local uiObj = UIManager:getUIByIndex(uiIndex)
						if uiObj:getPrefabPath() == uires then
							self:removeEventListener(EventManager.CUSTOM_EVENT.UI_SHOW_COMPLETE)
							self:showTutorial()
						end
					end)
				end
			else
				self:stopTutorial()
			end
		else
			DataManager.TutorialData:markFuncTutorialFinish(id)
			self:stopTutorial()
		end
	end)
end

-- gm用,跳过当前正在进行的引导
function TutorialManager:gmSkipTutorial()
	if Platform:getIsPublishChannel() then
		return
	end

	if not DataManager.TutorialData:getIsInTutorial() then
        return
    end

	-- 功能引导则直接退出即可
	if DataManager.TutorialData:getIsFuncTutorial() then
		return self:stopTutorial()
	end

	-- 强制引导则先跟服务器发送消息再退出
	self:stopTutorial()
end

return TutorialManager