local MainCompBaseCell = require "app/ui/main_city/component/main_comp_base_cell"
local DailyChallengeComp = class("DailyChallengeComp", MainCompBaseCell)

function DailyChallengeComp:getIsOpen()
    return DataManager.DailyChallengeData:isOpen()
end

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

function DailyChallengeComp:getShowEntranceRedPoint()
    return DataManager.DailyChallengeData:isMeetChallenge()
end

function DailyChallengeComp:isShowTopNode()
    return true
end

function DailyChallengeComp:isShowSideBar()
    return true
end

function DailyChallengeComp:isShowHeroFormation()
    return true
end

function DailyChallengeComp:getFightHpCost()
    return DataManager.DailyChallengeData:getChallengeHpCost()
end

function DailyChallengeComp:getFightTodayRemainCount()
    return DataManager.DailyChallengeData:getTodayRemainLimitCount()
end

function DailyChallengeComp:onClickFight()
	ModuleManager.DailyChallengeManager:startChallenge()
end

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

function DailyChallengeComp:init()
	self.uiMap = self:getBaseObject():genAllChildren()
	self:refreshShow()
end

function DailyChallengeComp:refreshShow()
	if not DataManager.DailyChallengeData:isOpen() then
		return
	end
	self:refreshTxt()
	self:refreshTask()
	self:refreshBuff()
	self:refreshBoss()
	self:refreshCountdown()
end

function DailyChallengeComp:refreshTxt()
	self.uiMap["daily_challenge_comp.title.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE))
	self.uiMap["daily_challenge_comp.record_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.CHAPTER_DESC_1, DataManager.DailyChallengeData:getMaxWave()))
end

function DailyChallengeComp:refreshTask()
	if self.tasks == nil or #self.tasks < 3 then
		self.tasks = GFunc.getTable()
		table.insert(self.tasks, self.uiMap["daily_challenge_comp.challenge.task.icon_task1"])
		table.insert(self.tasks, self.uiMap["daily_challenge_comp.challenge.task.icon_task2"])
		table.insert(self.tasks, self.uiMap["daily_challenge_comp.challenge.task.icon_task3"])
	end

	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, 0.6)
			taskObj:setSprite(GConst.ATLAS_PATH.DAILY_CHALLENGE, "daily_task_1")
		else
			-- 任务奖励不可领取
			taskObj:removeRedPoint()
			if not DataManager.DailyChallengeData:isTaskFinish(i) then
				taskObj:setSprite(GConst.ATLAS_PATH.DAILY_CHALLENGE, "daily_task_1")
			else
				taskObj:setSprite(GConst.ATLAS_PATH.DAILY_CHALLENGE, "daily_task_2")
			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 DailyChallengeComp:refreshBuff()
	local buffObj = self.uiMap["daily_challenge_comp.challenge.buffs.buff.btn_buff"]
	local buffIds = DataManager.DailyChallengeData:getTodayBuffIds()
	buffObj:addClickListener(function()
		ModuleManager.TipsManager:showDescTips(DataManager.DailyChallengeData:getBuffDesc(buffIds[1]), buffObj)
    end)

	local debuffObj = self.uiMap["daily_challenge_comp.challenge.buffs.debuff.btn_debuff"]
	debuffObj:addClickListener(function()
		ModuleManager.TipsManager:showDescTips(DataManager.DailyChallengeData:getBuffDesc(buffIds[2]), debuffObj)
    end)
end

function DailyChallengeComp: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.uiMap["daily_challenge_comp.boss.spine_node"], 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 DailyChallengeComp:refreshCountdown()
	self.countdownTx = self.uiMap["daily_challenge_comp.countdown.time_tx"]
	if not self.countdownSid then
		self.countdownSid = self:getBaseObject():scheduleGlobal(function()
			self:updateTime()
		end, 1)
	end
    self:updateTime()
end

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

return DailyChallengeComp