local AchievementTaskCell = class("AchievementTaskCell", BaseCell)

function AchievementTaskCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.nameTx = uiMap["achievement_cell.name_tx"]
	self.progress = uiMap["achievement_cell.progress_bg.progress"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
	self.progressTx = uiMap["achievement_cell.progress_bg.progress_tx"]
	self.getBtn = uiMap["achievement_cell.get_btn"]
	self.getBtnTx = uiMap["achievement_cell.get_btn.text"]
	self.progressBg = uiMap["achievement_cell.progress_bg"]

	self.rewardCells = {}
	for i = 1, 3 do
		self.rewardCells[i] = CellManager:addCellComp(uiMap["achievement_cell.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	end

	self.getBtn:addClickListener(function()
		if DataManager.DailyTaskData:canClaimAchievement(self.taskId, self.taskStage) then
            ModuleManager.TaskManager:onTaskAchievementReq(self.taskId, self.taskStage)
        else
            ModuleManager.TaskManager:taskGoto(self.taskType)
        end
	end)
end

function AchievementTaskCell:refresh(taskId)
	self.taskId = taskId
	self.taskType = DataManager.DailyTaskData:getTaskTypeByAchievementId(self.taskId)
    self.taskStage = DataManager.DailyTaskData:getAchievementStage(self.taskId)
    local taskTarget = DataManager.DailyTaskData:getAchievementCondition(self.taskId, self.taskStage)
    local taskProg = DataManager.DailyTaskData:getAchievementProg(self.taskId)
    taskProg = taskProg > taskTarget and taskTarget or taskProg

	self.progress.value = taskProg / taskTarget
	self.progressTx:setText(taskProg.."/"..taskTarget)

	local rewards = {DataManager.DailyTaskData:getAchievementReward(self.taskId, self.taskStage)}
	for i = 1, 3 do
		if rewards[i] then
			self.rewardCells[i]:setActive(true)
			self.rewardCells[i]:refreshByConfig(rewards[i])
		else
			self.rewardCells[i]:setActive(false)
		end
	end

	if DataManager.DailyTaskData:isAchievementReceived(self.taskId, self.taskStage) then
		-- 已完成
		-- self.mask:setActive(true)
		self.getBtn:setTouchEnable(false)
		self.getBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_DONE))
		self.getBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_1")
		-- self.nameTx:setText("<color=#574A40>" .. GFunc.getTaskDesc(self.taskType, taskTarget) .. "</color>")
		self.nameTx:setText(GFunc.getTaskDesc(self.taskType, taskTarget))
	else
		-- 未完成 or 可领取
		-- self.mask:setActive(false)
		self.getBtn:setTouchEnable(true)
		-- self.nameTx:setText("<color=#5C3A2D>" .. GFunc.getTaskDesc(self.taskType, taskTarget) .. "</color>")
		self.nameTx:setText(GFunc.getTaskDesc(self.taskType, taskTarget))

		if DataManager.DailyTaskData:isAchievementFinished(self.taskId, self.taskStage) then
			self.getBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
			self.getBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_blue_1")
		else
			self.getBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_21))
			self.getBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_yellow_1")
		end
	end
end

return AchievementTaskCell