local FundManager = class("FundManager", BaseModule)

function FundManager:showLevelFundUI()
	UIManager:showUI("app/ui/fund/growth_fund_ui")
end

function FundManager:buyLevelFund(id)
	if not DataManager.GrowthFundData:getIsOpen() then
        return
    end
	PayManager:purchasePackage(id, PayManager.PURCHARSE_TYPE.ACT_GIFT)
end

function FundManager:claimFundRewards()
	local claimRewards = {}
	local playerLevel = DataManager.PlayerData:getLv()
	local GrowthFundData = DataManager.GrowthFundData
	local gradeInfoIds = GrowthFundData:getCurrGradeInfoList()
	local isBoughtBase = GrowthFundData:getIsBoughtBase()
	local isBoughtAdvance = GrowthFundData:getIsBoughtAdvance()
	local freeClaimed = GrowthFundData:getFreeClaimedMap()
	local baseClaimed = GrowthFundData:getBaseClaimedMap()
	local advanceClaimed = GrowthFundData:getAdvanceClaimedMap()
	for _, id in ipairs(gradeInfoIds) do
		local levelFundInfo = GrowthFundData:getLevelFundCfg()[id]
		if levelFundInfo then
			if playerLevel >= levelFundInfo.level then -- 等级到了
				if not freeClaimed[id] then -- 能领但是没有领
					table.insert(claimRewards, {
						id = id,
						grade = 0
					})
				end
				if isBoughtBase then
					if not baseClaimed[id] then -- 能领但是没有领
						table.insert(claimRewards, {
							id = id,
							grade = 1
						})
					end
				end
				if isBoughtAdvance then
					if not advanceClaimed[id] then -- 能领但是没有领
						table.insert(claimRewards, {
							id = id,
							grade = 2
						})
					end
				end
			else
				break
			end
		end
	end
	if #claimRewards <= 0 then
		return
	end
	local args = {
		id_with_lv = claimRewards
	}
	self:sendMessage(ProtoMsgType.FromMsgEnum.FundAwardReq, args, {}, self.onClaimFundRewards, BIReport.ITEM_GET_TYPE.GROWTH_FUND)
end

function FundManager:onClaimFundRewards(result)
	if result.err_code == GConst.ERROR_STR.SUCCESS then
		if result.rewards then
			GFunc.showRewardBox(result.rewards)
		end
		DataManager.GrowthFundData:onClaimFundRewards(result.id_with_lv)
    end
end

return FundManager