local ActScoreTaskCompV1 = class("ActScoreTaskCompV1", LuaComponent)
local REWARD_CELL_COUNT = 3
local CELL = "app/ui/activity/common/cell/act_score_task_cell_v1"

function ActScoreTaskCompV1:init()
	local uiMap = self:getUIMap()
	-- self.banner = uiMap["act_score_task_comp_v1.bg.banner"]
	self.txTitle = uiMap["act_score_task_comp_v1.top.title_tx"]
	self.txTitle2 = uiMap["act_score_task_comp_v1.top.desc_tx"]
	self.txDesc = uiMap["act_score_task_comp_v1.top.turn_bg.desc_tx"]
	self.goBtn = uiMap["act_score_task_comp_v1.top.go_btn"]
	self.goTx = uiMap["act_score_task_comp_v1.top.go_btn.text"]
	self.getBtn = uiMap["act_score_task_comp_v1.top.turn_reward_bg.get_btn"]
	self.getTx = uiMap["act_score_task_comp_v1.top.turn_reward_bg.get_btn.text"]
	self.helpBtn = uiMap["act_score_task_comp_v1.top.title_tx.help_btn"]
	if not self.rewardList then
		self.rewardList = {}
		for i = 1, REWARD_CELL_COUNT do
			table.insert(self.rewardList, CellManager:addCellComp(uiMap["act_score_task_comp_v1.top.turn_reward_bg.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
		end
	end
	self.rewardDescTx = uiMap["act_score_task_comp_v1.top.turn_reward_bg.desc_tx"]
	self.scrollrect = uiMap["act_score_task_comp_v1.scrollrect"]
end

function ActScoreTaskCompV1:setParentUI(parentUI, dataKey)
	self.parentUI = parentUI
	self.dataKey = dataKey
	self.data = DataManager[dataKey]
end

function ActScoreTaskCompV1:refresh()
	local actId = self.data:getActId()
	local turn = DataManager.ActTaskData:getTurnScore(actId)
	local maxTurn = DataManager.ActTaskData:getMaxTurnScore(actId)
	local canGetTurnReward = DataManager.ActTaskData:getCanGetTurnRewardScore(actId)
	local gotAllTurnReward = DataManager.ActTaskData:getGotAllTurnRewardScore(actId)

	-- 轮次奖励
	local rewards = DataManager.ActTaskData:getRewardsgetMaxTurnScore(actId)
	for i = 1, REWARD_CELL_COUNT do
		local reward = rewards and rewards[i]
		if reward then
			self.rewardList[i].baseObject:setActive(true)
			if gotAllTurnReward then
				self.rewardList[i]:refreshByConfig(reward, true, true)
				self.rewardList[i]:hideFrameAnimation()
			else
				self.rewardList[i]:refreshByConfig(reward)
				if canGetTurnReward then
					self.rewardList[i]:showFrameAnimation()
					self.rewardList[i]:addClickListener(function()
						ModuleManager.ActSprintManager:reqStandardReward(actId)
					end)
				else
					self.rewardList[i]:hideFrameAnimation()
				end
			end
		else
			self.rewardList[i].baseObject:setActive(false)
		end
	end
	
	self.rewardDescTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_12))
	-- self.banner:setTexture(GConst.ActSprintConst.ACT_MAIN_BANNER[actId])
	-- self.txTitle:setText(I18N:getGlobalText(GConst.ActSprintConst.ACT_LIST_NAME[actId]))
	self.txTitle2:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_19))
	self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_9) .. "(" .. (turn >= maxTurn and maxTurn or (turn + 1)) .. "/" .. maxTurn .. ")")
	self.goTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_14))
	self.goBtn:addClickListener(function()
		EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.CHANGE_MAIN_CITY_PAGE, {page = GConst.MainCityConst.BOTTOM_PAGE.MAIN})
		ModuleManager.SummonManager:showSummonMainUI()
	end)
	self.getBtn:setActive(false)
	self.helpBtn:addClickListener(function()
		local params = {}
		params.desc = I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_17)
		ModuleManager.TipsManager:showHelpTips(params)
	end)

	-- 滑动窗
	self.standardList = DataManager.ActTaskData:getActTaskListScore(actId)
	if self.scrollRect == nil then
		self.scrollRect = self.scrollrect:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
		self.scrollRect:addInitCallback(function()
			return CELL
		end)
		self.scrollRect:addRefreshCallback(function(index, cell)
			cell:refresh(actId, self.standardList[index])
		end)
	end
	if self.scrollRect:getTotalCount() == nil or self.scrollRect:getTotalCount() <= 0 then
		self.scrollRect:refillCells(#self.standardList)
	elseif self.scrollRect:getTotalCount() ~= #self.standardList then
		self.scrollRect:clearCells()
		self.scrollRect:refillCells(#self.standardList)
	else
		self.scrollRect:updateAllCell()
	end
end

return ActScoreTaskCompV1