local FundLevelCell = class("FundLevelCell", BaseCell)

function FundLevelCell:init()
	local uiMap = self:getUIMap()
	self.progNode = uiMap["fund_level_cell.prog"]
	self.imgProg = uiMap["fund_level_cell.prog.img_prog"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
	self.lvNode = uiMap["fund_level_cell.level"]
	self.txLv = uiMap["fund_level_cell.level.tx_lv"]
	self.freeRewardCells = {}
	for i = 1, 2 do
		table.insert(self.freeRewardCells, uiMap["fund_level_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_level_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 FundLevelCell: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 needLevel = DataManager.FundLevelData:getNeedLevel(id)
	local stage = DataManager.FundLevelData:getStageById(id)
	local value = DataManager.PlayerData:getLv() >= needLevel and 1 or 0

	self.imgProg.value = value
	self.txLv:setText(needLevel)
	-- if value == 1 then
	-- 	self.txLv:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).color = self.colorLight
	-- else
	-- 	self.txLv:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).color = self.colorGray
	-- end

	local freeRewards = DataManager.FundLevelData:getFreeRewards(id)
	local freeReceived = DataManager.FundLevelData:getFreeGot(id)
	local canGet = DataManager.FundLevelData: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.FundLevelManager:getReward(stage)
				end)
				cell:showFrameAnimation()
			else
				cell:hideFrameAnimation()
			end
		else
			cell:setActive(false)
		end
	end

	local proRewards = DataManager.FundLevelData:getProRewards(id)
	local proReceived = DataManager.FundLevelData:getProGot(id)
	local locked = not DataManager.FundLevelData:getProBought(stage)
	local canGet = DataManager.FundLevelData: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.FundLevelManager:getReward(stage)
				end)
				cell:showFrameAnimation()
			else
				cell:hideFrameAnimation()
			end
		else
			cell:setActive(false)
		end
	end
end

return FundLevelCell