local ChapterBoxCell = class("ChapterBoxCell", BaseCell)

function ChapterBoxCell:init()
	local uiMap = self:getUIMap()

	self.titleTx = uiMap["cell.title_tx"]
	self.claimBtn = uiMap["cell.claim_btn"]
	self.claimBtnTx = uiMap["cell.claim_btn.text"]
	self.claimImg = uiMap["cell.claim_img"]
	self.claimImgTx = uiMap["cell.claim_img.text"]
	self.rewardCells = {}
	for i = 1, 3 do
		self.rewardCells[i] = CellManager:addCellComp(uiMap["cell.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	end
	self.claimImgTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_DONE))

	self.claimBtn:addClickListener(function()
		ModuleManager.ChapterManager:openBox(self.chapterId, self.idx)
	end)
end

function ChapterBoxCell:refresh(chapterId, idx)
	local list = DataManager.ChapterData:getChapterBoxRewardList(chapterId)
	local rewardCfg = list[idx]
	if rewardCfg == nil then
		return
	end
	self.chapterId = chapterId
	self.idx = idx
	local needWave = DataManager.ChapterData:getChapterBoxNum(chapterId, idx)
	self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.CHAPTER_BOX_DESC_1, needWave))

	local hadGot = DataManager.ChapterData:getChapterBoxRewardGot(chapterId, idx)
	if hadGot then
		self.claimBtn:setActive(false)
		self.claimImg:setActive(true)
	else
		self.claimBtn:setActive(true)
		self.claimImg:setActive(false)

		if DataManager.ChapterData:boxCanGet(chapterId, idx) then
			self.claimBtn:setTouchEnable(true)
			self.claimBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_blue_1")
			self.claimBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
		else
			self.claimBtn:setTouchEnable(false)
			self.claimBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_1")
			self.claimBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
		end
	end

	for i = 1, 3 do
		if rewardCfg[i] then
			self.rewardCells[i]:setActive(true)
			self.rewardCells[i]:refreshByConfig(rewardCfg[i], hadGot, hadGot)
		else
			self.rewardCells[i]:setActive(false)
		end
	end
end

return ChapterBoxCell