local DailyChallengeUI = class("DailyChallengeUI", BaseUI)

-- function DailyChallengeUI:getIsOpen()
--     return DataManager.DailyChallengeData:isOpen()
-- end

-- function DailyChallengeUI:getEntranceName()
--     return I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE)
-- end

-- function DailyChallengeUI:getShowEntranceRedPoint()
--     return DataManager.DailyChallengeData:isMeetChallenge()
-- end

function DailyChallengeUI:isFullScreen()
	return false
end

function DailyChallengeUI:getPrefabPath()
	return "assets/prefabs/ui/daily_challenge/daily_challenge_ui.prefab"
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()))

	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.buffObj = uiMap["daily_challenge_ui.buffs.buff.btn_buff"]
	self.debuffObj = uiMap["daily_challenge_ui.buffs.debuff.btn_debuff"]
	self.bossSpine = uiMap["daily_challenge_ui.spine_node"]
	self.countdownTx = uiMap["daily_challenge_ui.countdown.time_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.fightBtn:addClickListener(function ()
		ModuleManager.DailyChallengeManager:startChallenge()
	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()

	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 rewards = DataManager.DailyChallengeData:getTaskRewards(tasksData[i].task_id)
				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.buffObj:addClickListener(function()
		ModuleManager.TipsManager:showDescTips(DataManager.DailyChallengeData:getBuffDesc(buffIds[1]), self.buffObj)
	end)

	self.debuffObj:addClickListener(function()
		ModuleManager.TipsManager:showDescTips(DataManager.DailyChallengeData:getBuffDesc(buffIds[2]), self.debuffObj)
	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

return DailyChallengeUI