local FundChapterCell = class("FundChapterCell", BaseCell)

function FundChapterCell:init()
	local uiMap = self:getUIMap()
	self.progNode = uiMap["fund_chapter_cell.prog"]
	self.imgProg = uiMap["fund_chapter_cell.prog.img_prog"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
	self.lvNode = uiMap["fund_chapter_cell.level"]
	self.txLv = uiMap["fund_chapter_cell.level.tx_lv"]
	self.freeRewardCells = {}
	for i = 1, 2 do
		table.insert(self.freeRewardCells, uiMap["fund_chapter_cell.rewards_free.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
	end
	self.proRewardCells = {}
	for i = 1, 2 do
		table.insert(self.proRewardCells, uiMap["fund_chapter_cell.rewards_pro.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
	end

	self.colorLight = BF.Color(0.6235294, 0.3137255, 0.1764706, 1)
	self.colorGray = BF.Color(0.3568628, 0.3568628, 0.3568628, 1)
end

function FundChapterCell:refresh(id, isLast)
	-- 刷新进度
	local helf = self:getBaseObject():getSizeDeltaY() / 2
	if self:getIndex() == 1 then
		self.progNode:setAnchoredPositionY(-helf / 2)
		self.progNode:setSizeDeltaY(helf)
	elseif isLast then
		self.progNode:setAnchoredPositionY(helf / 2)
		self.progNode:setSizeDeltaY(helf)
	else
		self.progNode:setAnchoredPositionY(0)
		self.progNode:setSizeDeltaY(self:getBaseObject():getSizeDeltaY())
	end

	local needChapter = DataManager.ChapterFundData:getNeedChapter(id)
	local chapterId = DataManager.ChapterData:getChapterPage(needChapter)
	local stageNum = DataManager.ChapterData:getChapterStage(needChapter)
	
	local stage = DataManager.ChapterFundData:getStageById(id)
	local value = DataManager.ChapterData:getChapterPassed(needChapter) and 1 or 0

	self.imgProg.value = value
	self.txLv:setText(chapterId .. "-" .. stageNum)
	if value == 1 then
		self.lvNode:setSprite(GConst.ATLAS_PATH.UI_FUND, "fund_dec_1")
		-- self.txLv:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).color = self.colorLight
	else
		self.lvNode:setSprite(GConst.ATLAS_PATH.UI_FUND, "fund_dec_3")
		-- self.txLv:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).color = self.colorGray
	end
	-- GFunc.centerTxAndImgAndTx(txObj1, spacing1, imgObj, spacing2, txObj2)

	local freeRewards = DataManager.ChapterFundData:getFreeRewards(id)
	local freeReceived = DataManager.ChapterFundData:getFreeGot(id)
	local canGet = DataManager.ChapterFundData:getFreeCanGet(id)
	for i, cell in ipairs(self.freeRewardCells) do
		if freeRewards and freeRewards[i] then
			cell:setActive(true)
			cell:refreshByConfig(freeRewards[i], freeReceived, freeReceived)
			if canGet then
				cell:addClickListener(function()
					ModuleManager.FundChapterManager:getReward(stage)
				end)
				cell:showFrameAnimation()
			else
				cell:hideFrameAnimation()
				cell:setClickShowTips()
			end
		else
			cell:setActive(false)
		end
	end

	local proRewards = DataManager.ChapterFundData:getProRewards(id)
	local proReceived = DataManager.ChapterFundData:getProGot(id)
	local locked = not DataManager.ChapterFundData:getProBought(stage)
	local canGet = DataManager.ChapterFundData:getProCanGet(id)
	for i, cell in ipairs(self.proRewardCells) do
		if proRewards and proRewards[i] then
			cell:setActive(true)
			cell:refreshByConfig(proRewards[i], proReceived, proReceived)
			cell:showLock(locked)
			if canGet then
				cell:addClickListener(function()
					ModuleManager.FundChapterManager:getReward(stage)
				end)
				cell:showFrameAnimation()
			else
				cell:hideFrameAnimation()
				cell:setClickShowTips()
			end
		else
			cell:setActive(false)
		end
	end
end

return FundChapterCell
