local DailyTaskCell = class("DailyTaskCell", BaseCell)

function DailyTaskCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.icon = uiMap["task_cell.icon"]
	self.nameTx = uiMap["task_cell.name_tx"]
	self.progressBg = uiMap["task_cell.progress_bg"]
	self.progress = uiMap["task_cell.progress_bg.progress"]
	self.progressTx = uiMap["task_cell.progress_bg.progress_tx"]
	self.getBtnRoot = uiMap["task_cell.get_btn"]
	local getBtn = uiMap["task_cell.get_btn.btn"]
	getBtn:addClickListener(function()
		if self.taskId then
			ModuleManager.TaskManager:claimDailyTask(self.taskId)
		end
	end)
	self.getBtnTx = uiMap["task_cell.get_btn.btn.text"]
	self.getBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
	self.refreshBtnRoot = uiMap["task_cell.refresh_btn"]
	local refreshBtn = uiMap["task_cell.refresh_btn.btn"]
	refreshBtn:addClickListener(function()
		if self.taskId then
			ModuleManager.TaskManager:refreshDailyTask(self.taskId)
		end
	end)
	self.adBtnRoot = uiMap["task_cell.ad_btn"]
	local adBtn = uiMap["task_cell.ad_btn.btn"]
	adBtn:addClickListener(function()
		if self.taskId then
			ModuleManager.TaskManager:watchTaskSpecialAd()
		end
	end)
	self.refreshBtnTx = uiMap["task_cell.refresh_btn.btn.text"]
	self.refreshBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.STR_REFRESH))
	self.rewardCellComp = uiMap["task_cell.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	self.lockNode = uiMap["task_cell.lock_node"]
	self.lockBg = uiMap["task_cell.lock_bg"]
	local lockBtn = uiMap["task_cell.lock_node.lock_btn.btn"]
	lockBtn:addClickListener(function()
		if DataManager.BountyData:getIsOpen() then
			DataManager.BountyData:clearPopBought()
			ModuleManager.BountyManager:showBountyMainUI()
		end
	end)
	local lockBtnTx = uiMap["task_cell.lock_node.lock_btn.btn.text"]
	lockBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.STR_UNLOCK))
	local lockTx = uiMap["task_cell.lock_node.lock_tx"]
	lockTx:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_DESC_1))
	self.completeNode = uiMap["task_cell.complete"]
	local completeTx = uiMap["task_cell.complete.text"]
	completeTx:setText(I18N:getGlobalText(I18N.GlobalConst.STR_COMPLETED))
end

function DailyTaskCell:refresh(task)
	self.taskId = task.id
	local icon = DataManager.DailyTaskData:getDailyTaskIcon(task.id)
	self.icon:setSprite(GConst.ATLAS_PATH.ICON_TASK, icon)
	local taskDesc = DataManager.DailyTaskData:getDailyTaskDesc(task.id)
	self.nameTx:setText(taskDesc)

	if task.lock then
		self.lockNode:setVisible(true)
		self.lockBg:setVisible(true)
		self.progressBg:setVisible(false)
		self.refreshBtnRoot:setVisible(false)
		self.getBtnRoot:setVisible(false)
		self.adBtnRoot:setVisible(false)
		self.completeNode:setVisible(false)
	else
		self.lockBg:setVisible(false)
		self.lockNode:setVisible(false)
		self.progressBg:setVisible(true)
		local count = task.progress
		local needCount = task.needProgress
		if task.claimed then -- 已经领过了
			self.completeNode:setVisible(true)
			self.getBtnRoot:setVisible(false)
			self.refreshBtnRoot:setVisible(false)
			self.adBtnRoot:setVisible(false)
			if count >= needCount then
				count = needCount
			end
		else
			self.completeNode:setVisible(false)
			if count >= needCount then
				count = needCount
				self.getBtnRoot:setVisible(true)
				self.refreshBtnRoot:setVisible(false)
				self.adBtnRoot:setVisible(false)
			else
				self.getBtnRoot:setVisible(false)
				local isAdType = DataManager.DailyTaskData:getDailyTaskIsAdType(task.id)
				if isAdType then
					self.adBtnRoot:setVisible(true)
					self.refreshBtnRoot:setVisible(false)
				else
					self.adBtnRoot:setVisible(false)
					local canRefresh = DataManager.DailyTaskData:getDailyTaskCanRefresh(task.id)
					if canRefresh then
						if task.refresh then -- 刷过了
							self.refreshBtnRoot:setVisible(false)
						else
							self.refreshBtnRoot:setVisible(true)
						end
					else
						self.refreshBtnRoot:setVisible(false)
					end
				end
			end
		end
		self.progressTx:setText(count .. "/" .. needCount)
		self.progress:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = count/needCount
	end
	local reward = DataManager.DailyTaskData:getDailyTaskReward(task.id)
	if reward and reward[1] then
		self.rewardCellComp:refreshByConfig(reward[1])
	end
end

return DailyTaskCell