local TaskToastManager = class("TaskToastManager", BaseModule)

function TaskToastManager:ctor()
	self.toastList = {}
end

function TaskToastManager:clear()
end

function TaskToastManager:checkOnOpenUI()
	if #self.toastList <= 0 then
		return
	end
	local topUIObj = UIManager:getTopUIObj()
	if topUIObj then
		if self:checkUnshowUI(topUIObj) then
			self:closeToast()
		end
	end
end

function TaskToastManager:checkUnshowUI(uiObj)
	local uiIndex = uiObj:getUIIndex()
	if uiIndex == UIManager.UI_PATH.BATTLE_UI then
		return true
	end
	return false
end

function TaskToastManager:showToast(id, taskId, from, to)
	local topUIObj = UIManager:getTopUIObj()
	if topUIObj then
		if self:checkUnshowUI(topUIObj) then
			return
		end
	end
	local params = {
        id = id,
        taskId = taskId,
        from = from,
        to = to
    }
	table.insert(self.toastList, params)
	if #self.toastList == 1 then
		self:_showToast()
	end
end

function TaskToastManager:_showToast()
	local params = self.toastList[1]
	if params == nil then
		return
	end
	UIManager:getTaskToast(function(prefabObject)
		local uiMap = prefabObject:genAllChildren()
		local lockBg = uiMap["task_toast.lock_bg"]
		local icon = uiMap["task_toast.icon"]
		local nameTx = uiMap["task_toast.name_tx"]
		local progressComp = uiMap["task_toast.progress_bg.progress"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
		local progressTx = uiMap["task_toast.progress_bg.progress_tx"]
		local rewardCellComp = uiMap["task_toast.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)

		local dailyTaskInfo = ConfigManager:getConfig("task_daily")[params.id]
		if dailyTaskInfo == nil then
			return self:onFinish()
		end
		if dailyTaskInfo.bounty then
			lockBg:setVisible(true)
		else
			lockBg:setVisible(false)
		end
		local taskInfo = ConfigManager:getConfig("task")[params.taskId]
		if taskInfo == nil then
			return self:onFinish()
		end
		icon:setSprite(GConst.ATLAS_PATH.ICON_TASK, taskInfo.icon)

		local taskI18NInfo = I18N:getConfig("task")[params.taskId]
		if taskI18NInfo == nil then
			return self:onFinish()
		end
		nameTx:setText(taskI18NInfo.desc)
		local reward = taskInfo.reward
		if reward and reward[1] then
			rewardCellComp:refreshByConfig(reward[1])
		end

		if self.sliderSequence then
			self.sliderSequence:Kill()
		end
		self.sliderSequence = DOTweenManager:createSeqWithIntId()
		local curProgress = 0
		local from = params.from
		local to = params.to
		local last = to - from
		local startPercent = params.from / params.to
		local lastPercent = 1 - startPercent
		progressComp.value = from / to
		progressTx:setText(from .. "/" .. to)
        local tween = DOTweenManager:createDOTweenTo(
            function()
                return curProgress
            end,
            function(value)
                curProgress = value
				progressComp.value = startPercent + lastPercent*curProgress
				progressTx:setText(from + math.floor(last*curProgress) .. "/" .. to)
            end,
        	1, 1)
		self.sliderSequence:Append(tween)
		self.sliderSequence:AppendCallback(function()
			self.sliderSequence = nil
		end)

		local notchHeight = SafeAreaManager:getNotchScreenHeight()
		prefabObject:setAnchoredPositionY(-notchHeight)
		prefabObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = true

		if self.toastSequence == nil then
			self.toastSequence = DOTweenManager:createSeqWithIntId()
			self.toastSequence:AppendInterval(2)
			self.toastSequence:AppendCallback(function()
				self:onFinish()
			end)
			self.toastSequence:SetAutoKill(false)
		else
			self.toastSequence:Restart()
		end
	end)
end

function TaskToastManager:refreshText()
	if #self.toastList <= 0 then
		return
	end
	if self.toastSequence then
		self.toastSequence:Pause()
	end
	if self.sliderSequence then
		self.sliderSequence:Kill()
		self.sliderSequence = nil
	end
	self:onFinish()
end

function TaskToastManager:closeToast()
	if #self.toastList <= 0 then
		return
	end
	if self.toastSequence then
		self.toastSequence:Pause()
	end
	if self.sliderSequence then
		self.sliderSequence:Kill()
		self.sliderSequence = nil
	end
	UIManager:getTaskToast(function(prefabObject)
		prefabObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = false
	end)
	for i = 1, #self.toastList do
		table.remove(self.toastList)
	end
end

function TaskToastManager:onFinish()
	if #self.toastList <= 0 then
		UIManager:getTaskToast(function(prefabObject)
			prefabObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = false
		end)
		return
	end
	table.remove(self.toastList, 1)
	if #self.toastList > 0 then
		self:_showToast()
	else
		UIManager:getTaskToast(function(prefabObject)
			prefabObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = false
		end)
	end
end

return TaskToastManager