local GrowthFundCell = class("GrowthFundCell", BaseCell)

local Fund_REWARD_CELL = "app/ui/fund/cell/growth_fund_reward_cell"

function GrowthFundCell:init()
	local uiMap = self:getUIMap()
	local width = GConst.UI_SCREEN_WIDTH

	self.fundRewardCell1 = uiMap["growth_fund_cell.fund_reward_cell_1"]:addLuaComponent(Fund_REWARD_CELL)
	self.fundRewardCell1:setAnchoredPositionX(-width/3)
	self.fundRewardCell1:addClickListener(function()
		if self.fundId == nil then
			return
		end
		local iCslaimed = DataManager.GrowthFundData:getIsClaimedBase(self.fundId)
		local needLevel = DataManager.GrowthFundData:getNeedLevel(self.fundId)
		local playerLevel = DataManager.PlayerData:getLv()
		if not iCslaimed and playerLevel >= needLevel then
			ModuleManager.FundManager:claimFundRewards()
		else
			local freeReward = DataManager.GrowthFundData:getFreeFundRewards(self.fundId)
			local reward = freeReward and freeReward[1]
			if reward then
				ModuleManager.TipsManager:showRewardTips(reward.id, reward.type, self.fundRewardCell1:getBaseObject())
			end
		end
	end)
	self.fundRewardCell2 = uiMap["growth_fund_cell.fund_reward_cell_2"]:addLuaComponent(Fund_REWARD_CELL)
	self.fundRewardCell2:addClickListener(function()
		if self.fundId == nil then
			return
		end
		local iCslaimed = DataManager.GrowthFundData:getIsClaimedBase(self.fundId)
		local needLevel = DataManager.GrowthFundData:getNeedLevel(self.fundId)
		local playerLevel = DataManager.PlayerData:getLv()
		if not iCslaimed and playerLevel >= needLevel and DataManager.GrowthFundData:getIsBoughtBase(self.fundId) then
			ModuleManager.FundManager:claimFundRewards()
		else
			local baseReward = DataManager.GrowthFundData:getBaseFundRewards(self.fundId)
			local reward = baseReward and baseReward[1]
			if reward then
				ModuleManager.TipsManager:showRewardTips(reward.id, reward.type, self.fundRewardCell2:getBaseObject())
			end
		end
	end)
	self.fundRewardCell3 = uiMap["growth_fund_cell.fund_reward_cell_3"]:addLuaComponent(Fund_REWARD_CELL)
	self.fundRewardCell3:addClickListener(function()
		if self.fundId == nil then
			return
		end
		local iCslaimed = DataManager.GrowthFundData:getIsClaimedAdvance(self.fundId)
		local needLevel = DataManager.GrowthFundData:getNeedLevel(self.fundId)
		local playerLevel = DataManager.PlayerData:getLv()
		if not iCslaimed and playerLevel >= needLevel and DataManager.GrowthFundData:getIsBoughtAdvance(self.fundId) then
			ModuleManager.FundManager:claimFundRewards()
		else
			local advanceReward = DataManager.GrowthFundData:getAdvanceFundRewards(self.fundId)
			local reward = advanceReward and advanceReward[1]
			if reward then
				ModuleManager.TipsManager:showRewardTips(reward.id, reward.type, self.fundRewardCell3:getBaseObject())
			end
		end
	end)
	self.fundRewardCell3:setAnchoredPositionX(width/3)
	self.levelBg = uiMap["growth_fund_cell.lv_bg"]
	self.levelBg:setAnchoredPositionX(-width/6)
	self.levelGrayImg = uiMap["growth_fund_cell.lv_bg.gray"]
	self.levelTx = uiMap["growth_fund_cell.lv_bg.tx"]
end

function GrowthFundCell:refresh(id)
	self.fundId = id
	local GrowthFundData = DataManager.GrowthFundData
	local needLevel = DataManager.GrowthFundData:getNeedLevel(id)
	local playerLevel = DataManager.PlayerData:getLv()
	local isActive = playerLevel >= needLevel
	self.levelGrayImg:setVisible(not isActive)
	self.levelTx:setText(GFunc.intToString(needLevel))
	local freeReward, baseReward, advanceReward = GrowthFundData:getAllStepFundRewards(id)
	self.fundRewardCell1:refresh(freeReward[1], isActive and 1 or 0, false, GrowthFundData:getIsClaimedFree(id))
	self.fundRewardCell2:refresh(baseReward[1], isActive and 2 or 0, not GrowthFundData:getIsBoughtBase(id), GrowthFundData:getIsClaimedBase(id))
	self.fundRewardCell3:refresh(advanceReward[1], isActive and 3 or 0, not GrowthFundData:getIsBoughtAdvance(id), GrowthFundData:getIsClaimedAdvance(id))
end

function GrowthFundCell:setVisible(visible)
	self.baseObject:setVisible(visible)
end

return GrowthFundCell