local DailyChallengeUI = class("DailyChallengeUI", BaseUI)

function DailyChallengeUI:isFullScreen()
	return false
end

function DailyChallengeUI:getCurrencyParams()
	if self.currencyParams == nil then
		self.currencyParams = {
			itemIds = {
				GConst.ItemConst.ITEM_ID_GOLD,
				GConst.ItemConst.ITEM_ID_GEM,
				GConst.ItemConst.ITEM_ID_VIT
			},
			showType = GConst.CURRENCY_TYPE.HORIZONTAL
		}
	end
 	return self.currencyParams
end


function DailyChallengeUI:getPrefabPath()
	return "assets/prefabs/ui/daily_challenge/daily_challenge_ui.prefab"
end

function DailyChallengeUI:onClose()
	self.bossSpine:removeAllChildren()
end

function DailyChallengeUI:ctor()
	ModuleManager.DailyChallengeManager:checkDayChange()
end

function DailyChallengeUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()

	uiMap["daily_challenge_ui.close_btn"]:addClickListener(function ()
		self:closeUI()
	end)
	uiMap["daily_challenge_ui.title.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE))
	uiMap["daily_challenge_ui.record_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.CHAPTER_DESC_1, DataManager.DailyChallengeData:getMaxWave()))
	uiMap["daily_challenge_ui.up_diff_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_2))
	uiMap["daily_challenge_ui.down_diff_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_7))

	self.tasks = GFunc.getTable()
	table.insert(self.tasks, uiMap["daily_challenge_ui.task.icon_task1"])
	table.insert(self.tasks, uiMap["daily_challenge_ui.task.icon_task2"])
	table.insert(self.tasks, uiMap["daily_challenge_ui.task.icon_task3"])

	self.taskMasks = GFunc.getTable()
	table.insert(self.taskMasks, uiMap["daily_challenge_ui.task.icon_task1.mask"])
	table.insert(self.taskMasks, uiMap["daily_challenge_ui.task.icon_task2.mask"])
	table.insert(self.taskMasks, uiMap["daily_challenge_ui.task.icon_task3.mask"])

	self.buffBtn = uiMap["daily_challenge_ui.buffs.buff"]
	self.debuffBtn = uiMap["daily_challenge_ui.buffs.debuff"]
	self.bossSpine = uiMap["daily_challenge_ui.spine_node"]
	self.countdownTx = uiMap["daily_challenge_ui.countdown.time_tx"]
	self.diffTx = uiMap["daily_challenge_ui.diff_tx"]

	self.fightBtn = uiMap["daily_challenge_ui.fight_btn"]
	-- 体力消耗
	self.fightCost = uiMap["daily_challenge_ui.fight_btn.cost"]
	self.costTxDesc = uiMap["daily_challenge_ui.fight_btn.cost.tx_desc"]
	self.costTxCost = uiMap["daily_challenge_ui.fight_btn.cost.tx_cost"]
	-- 剩余次数
	self.countTxNum = uiMap["daily_challenge_ui.fight_btn.tx_count"]
	-- 按钮文本
	self.txFight = uiMap["daily_challenge_ui.fight_btn.tx_desc"]

	self.upDiffBtn = uiMap["daily_challenge_ui.up_diff_btn"]
	self.upDiffBtnDescTx = uiMap["daily_challenge_ui.up_diff_btn.desc_tx"]
	self.downDiffBtn = uiMap["daily_challenge_ui.down_diff_btn"]
	self.fightBtn:addClickListener(function ()
		ModuleManager.DailyChallengeManager:startChallenge()
	end)
	self.upDiffBtn:addClickListener(function ()
		if DataManager.DailyChallengeData:canGetReward() then
			GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_6))
			return
		end
		local diffChapter = DataManager.DailyChallengeData:getDiffLv()
		local maxChapter = DataManager.DailyChallengeData:getMaxDiffLv()
		if diffChapter == maxChapter then
			return
		else
			local cfg = DataManager.DailyChallengeData:getDifficultyCfg(diffChapter + 1)
			local chapterId = DataManager.ChapterData:getMaxChapterId()
			if cfg.max_chapter <= chapterId then
				local params = {
					content = I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_4),
					boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
					okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
					cancelText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_CANCEL),
					okFunc = function()
						ModuleManager.DailyChallengeManager:onChapterDailyChallengeLevelReq(diffChapter + 1)
					end,
				}
				GFunc.showMessageBox(params)
			else
				local chapterPage = DataManager.ChapterData:getChapterPage(cfg.max_chapter)
				local chapterStage = DataManager.ChapterData:getChapterStage(cfg.max_chapter)
				local str = I18N:getGlobalText(I18N.GlobalConst.FUNC_OPEN_STAGE, DataManager.ChapterData:getChapterNameXYMode(cfg.max_chapter))
				GFunc.showToast(str)
			end
		end
	end)
	self.downDiffBtn:addClickListener(function ()
		if DataManager.DailyChallengeData:canGetReward() then
			GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_6))
			return
		end
		local diffChapter = DataManager.DailyChallengeData:getDiffLv()
		if diffChapter == 1 then
			return
		else
			local params = {
				content = I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_5),
				boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
				okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
				cancelText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_CANCEL),
				okFunc = function()
					ModuleManager.DailyChallengeManager:onChapterDailyChallengeLevelReq(diffChapter - 1)
				end,
			}
			GFunc.showMessageBox(params)
		end
	end)

	self:bind(DataManager.DailyChallengeData, "isDirty", function()
		self:onRefresh()
	end)
end

function DailyChallengeUI:updateTime()
	ModuleManager.DailyChallengeManager:checkDayChange()
	local remainTime = Time:getTodaySurplusTime()
	self.countdownTx:setText(GFunc.getTimeStrWithHMS(remainTime))
end

function DailyChallengeUI:onRefresh()
	self:refreshTask()
	self:refreshBuff()
	self:refreshBoss()
	self:updateTime()
	self:refreshFightBtn()
	self:refreshDiffBtn()

	if DataManager.DailyChallengeData:getIsPopTask() then
		ModuleManager.DailyChallengeManager:showBattleTaskUI()
	end
end

function DailyChallengeUI:refreshTask()
	local tasksData = DataManager.DailyChallengeData:getTasks()
	for i = 1, #self.tasks do
		local taskObj = self.tasks[i]
		if DataManager.DailyChallengeData:canClaimTask(i) then
			-- 任务奖励可领取
			taskObj:addRedPoint(30, 30, 1)
			-- taskObj:setSprite(GConst.ATLAS_PATH.DAILY_CHALLENGE, "daily_task_1")
			self.taskMasks[i]:setActive(false)
		else
			-- 任务奖励不可领取
			taskObj:removeRedPoint()
			if not DataManager.DailyChallengeData:isTaskFinish(i) then
				-- taskObj:setSprite(GConst.ATLAS_PATH.DAILY_CHALLENGE, "daily_task_1")
				self.taskMasks[i]:setActive(false)
			else
				-- taskObj:setSprite(GConst.ATLAS_PATH.DAILY_CHALLENGE, "daily_task_2")
				self.taskMasks[i]:setActive(true)
			end
		end
		taskObj:addClickListener(function()
			if DataManager.DailyChallengeData:canClaimTask(i) then
				ModuleManager.DailyChallengeManager:getTaskReward(i)
			else
				local diffChapter = DataManager.DailyChallengeData:getDiffLv()
				local cfg = DataManager.DailyChallengeData:getDifficultyCfg(diffChapter)
				local rewards = cfg.task_daily_reward[i] or {}
				local desc = DataManager.DailyChallengeData:getTaskDesc(tasksData[i].task_id)
				ModuleManager.TipsManager:showRewardsTips(rewards, desc, taskObj, nil, nil, false, ModuleManager.TipsManager.REWARDS_TIPS_TYPE.TASK)
			end
		end)
	end
end

function DailyChallengeUI:refreshBuff()
	local buffIds = DataManager.DailyChallengeData:getTodayBuffIds()
	self.buffBtn:addClickListener(function()
		ModuleManager.TipsManager:showDescTips(DataManager.DailyChallengeData:getBuffDesc(buffIds[1]), self.buffBtn)
	end)

	self.debuffBtn:addClickListener(function()
		ModuleManager.TipsManager:showDescTips(DataManager.DailyChallengeData:getBuffDesc(buffIds[2]), self.debuffBtn)
	end)
end

function DailyChallengeUI:refreshBoss()
	local curBossInfo = DataManager.DailyChallengeData:getFinalBossInfo()
	if not curBossInfo then
		return
	end
	if self.curModelId == curBossInfo.model_id then
		return
	end
	if self.spineBoss then
		self.spineBoss:destroy()
		self.spineBoss = nil
		self.curModelId = nil
	end
	self.curModelId = curBossInfo.model_id
	SpineManager:loadHeroAsync(self.curModelId, self.bossSpine, function(spineObject)
		self.spineBoss = spineObject
		self.spineBoss:setDefaultMix(0)
		self.spineBoss:setLocalScale(curBossInfo.model_ui, curBossInfo.model_ui, curBossInfo.model_ui)
		self.spineBoss:playAnimation(GConst.BattleConst.SPINE_ANIMATION_NAME.IDLE, true, false)
	end)
end

function DailyChallengeUI:refreshFightBtn()
	local isShowFight = false

	-- 体力消耗
	local cost = DataManager.DailyChallengeData:getChallengeHpCost()
	if cost then
		isShowFight = true
		self.fightCost:setActive(true)
		self.costTxDesc:setText(I18N:getGlobalText(I18N.GlobalConst.START_DESC))
		self.costTxCost:setText(GFunc.getRewardNum(cost))
	else
		self.fightCost:setActive(false)
	end

	-- 剩余次数
	local remainCount = DataManager.DailyChallengeData:getTodayRemainLimitCount()
	if remainCount >= 0 then
		isShowFight = true
		self.countTxNum:setActive(true)
		self.countTxNum:setText(I18N:getGlobalText(I18N.GlobalConst.TODAY_REMAIN_TIMES, remainCount))
	else
		self.countTxNum:setActive(false)
	end

	if isShowFight then
		self.fightBtn:setActive(true)
	else
		self.fightBtn:setActive(false)
	end
end

function DailyChallengeUI:refreshDiffBtn()
	local diffChapter = DataManager.DailyChallengeData:getDiffLv()
	local maxChapter = DataManager.DailyChallengeData:getMaxDiffLv()
	if diffChapter == maxChapter then
		self.upDiffBtn:setActive(false)
	else
		self.upDiffBtn:setActive(true)
		local cfg = DataManager.DailyChallengeData:getDifficultyCfg(diffChapter + 1)
		local chapterId = DataManager.ChapterData:getMaxChapterId()
		local chapterPage = DataManager.ChapterData:getChapterPage(cfg.max_chapter)
		local chapterStage = DataManager.ChapterData:getChapterStage(cfg.max_chapter)
		local str
		if cfg.max_chapter <= chapterId then
			str = I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_3, chapterPage, chapterStage, "<color=#87FF9E>(" .. I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_8) .. ")</color>")
			self.upDiffBtn:addRedPoint(70, 22, 0.8)
		else
			str = I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_3, chapterPage, chapterStage, "<color=#FF8787>(" .. I18N:getGlobalText(I18N.GlobalConst.CHAPTER_WAVE_REAWRD_1) .. ")</color>")
			self.upDiffBtn:removeRedPoint()
		end
		self.upDiffBtnDescTx:setText(str)
	end
	self.downDiffBtn:setActive(diffChapter > 1)
	self.diffTx:setText(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_9, diffChapter))
end

return DailyChallengeUI