local GrowthFundData = class("GrowthFundData", BaseData)

local ACT_GIFT_FUND_BASE_ID_1 = 60202
local ACT_GIFT_FUND_BASE_ID_2 = 60402
local ACT_GIFT_FUND_ADVANCE_ID_1 = 60102
local ACT_GIFT_FUND_ADVANCE_ID_2 = 60302

function GrowthFundData:ctor()
	self.data.dirty = false
	self.isClaimAll = false
end

function GrowthFundData:init(data)
	data = data or GConst.EMPTY_TABLE

	self.freeClaimed = {}
	self.freeClaimedCount = 0
	if data.free_awarded then
		for _, v in ipairs(data.free_awarded) do
			self.freeClaimed[v] = true
			self.freeClaimedCount = self.freeClaimedCount + 1
		end
	end
	self.baseClaimed = {}
	self.baseClaimedCount = 0
	if data.pay_low_awarded then
		for _, v in ipairs(data.pay_low_awarded) do
			self.baseClaimed[v] = true
			self.baseClaimedCount = self.baseClaimedCount + 1
		end
	end
	self.advanceClaimed = {}
	self.advanceClaimedCount = 0
	if data.pay_high_awarded then
		for _, v in ipairs(data.pay_high_awarded) do
			self.advanceClaimed[v] = true
			self.advanceClaimedCount = self.advanceClaimedCount + 1
		end
	end
	self:initGrowthInfo()
	self:initFundGrade()
end

function GrowthFundData:initGrowthInfo()
	if not self.growthInfoMap then
		self.growthInfoMap = {}
		local cfg = self:getLevelFundCfg()
		for id, info in pairs(cfg) do
			if not self.growthInfoMap[info.stage] then
				self.growthInfoMap[info.stage] = {}
			end
			table.insert(self.growthInfoMap[info.stage], id)
		end
		for grade, _ in pairs(self.growthInfoMap) do
			table.sort(self.growthInfoMap[grade], function(a, b)
				return a < b
			end)
		end
	end
end

function GrowthFundData:initFundGrade()
	local fundGrade = 1
	local totalCount = 0
	while true do
		local growInfoIds = self.growthInfoMap[fundGrade]
		if growInfoIds == nil then
			fundGrade = fundGrade - 1
			break
		end
		totalCount = totalCount + #growInfoIds
		if self.freeClaimedCount >= totalCount and
			self.baseClaimedCount >= totalCount and
			self.advanceClaimedCount >= totalCount then
			fundGrade = fundGrade + 1
		else
			break
		end
	end
	if fundGrade < 1 then
		fundGrade = 1
	end
	self.data.fundGrade = fundGrade
	if self.data.fundGrade == 2 then
		self.fundBaseId = ACT_GIFT_FUND_BASE_ID_2
		self.fundAdvanceId = ACT_GIFT_FUND_ADVANCE_ID_2
		if self.freeClaimedCount >= totalCount and self.baseClaimedCount >= totalCount and self.advanceClaimedCount >= totalCount then
			self.isClaimAll = true
		end
	else
		self.fundBaseId = ACT_GIFT_FUND_BASE_ID_1
		self.fundAdvanceId = ACT_GIFT_FUND_ADVANCE_ID_1
	end
end

function GrowthFundData:markDirty()
	self.data.dirty = not self.data.dirty
end

function GrowthFundData:getIsOpen()
	if self.isClaimAll then
		return false
	end
	return ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.FUND, true)
end

function GrowthFundData:getFundGrade()
	return self.data.fundGrade
end

function GrowthFundData:getFundBaseId()
	return self.fundBaseId
end

function GrowthFundData:getFundAdvanceId()
	return self.fundAdvanceId
end

function GrowthFundData:getCurrGradeBaseRechargeId()
	local info = self:getActGiftCfg()[self.fundBaseId]
	return info.recharge_id
end

function GrowthFundData:getCurrGradeAdvanceRechargeId()
	local info = self:getActGiftCfg()[self.fundAdvanceId]
	return info.recharge_id
end

function GrowthFundData:getActGiftCfg()
	if self.cfg == nil then
		self.cfg = ConfigManager:getConfig("act_gift")
	end
	return self.cfg
end

function GrowthFundData:getLevelFundCfg()
	if self.levelFundCfg == nil then
		self.levelFundCfg = ConfigManager:getConfig("act_level_fund")
	end
	return self.levelFundCfg
end

function GrowthFundData:getCurrGradeInfoList()
	return self.growthInfoMap[self.data.fundGrade] or GConst.EMPTY_TABLE
end

function GrowthFundData:getNeedLevel(id)
	local levelFundInfo = self:getLevelFundCfg()[id]
	if levelFundInfo == nil then
		return 9999
	end
	return levelFundInfo.level
end

function GrowthFundData:getAllStepFundRewards(id)
	local levelFundInfo = self:getLevelFundCfg()[id]
	if levelFundInfo == nil then
		return nil, nil, nil
	end
	return levelFundInfo.reward_free, levelFundInfo.reward_small, levelFundInfo.reward
end

function GrowthFundData:getFreeFundRewards(id)
	local levelFundInfo = self:getLevelFundCfg()[id]
	if levelFundInfo == nil then
		return nil
	end
	return levelFundInfo.reward_free
end

function GrowthFundData:getBaseFundRewards(id)
	local levelFundInfo = self:getLevelFundCfg()[id]
	if levelFundInfo == nil then
		return nil
	end
	return levelFundInfo.reward_small
end

function GrowthFundData:getAdvanceFundRewards(id)
	local levelFundInfo = self:getLevelFundCfg()[id]
	if levelFundInfo == nil then
		return nil
	end
	return levelFundInfo.reward
end

function GrowthFundData:getIsBoughtBase()
	return DataManager.ShopData:getGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, self.fundBaseId) > 0
end

function GrowthFundData:getIsBoughtAdvance()
	return DataManager.ShopData:getGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, self.fundAdvanceId) > 0
end

function GrowthFundData:getFreeClaimedMap()
	return self.freeClaimed
end

function GrowthFundData:getIsClaimedFree(id)
	return self.freeClaimed[id]
end

function GrowthFundData:getBaseClaimedMap()
	return self.baseClaimed
end

function GrowthFundData:getIsClaimedBase(id)
	return self.baseClaimed[id]
end

function GrowthFundData:getAdvanceClaimedMap()
	return self.advanceClaimed
end

function GrowthFundData:getIsClaimedAdvance(id)
	return self.advanceClaimed[id]
end

function GrowthFundData:getMinUnclaimedRewardIndex()
	local playerLevel = DataManager.PlayerData:getLv()
	local growInfoIds = self:getCurrGradeInfoList()
	local minIndex = 0
	local isBoughtBase = self:getIsBoughtBase()
	local isBoughtAdvance = self:getIsBoughtAdvance()
	for k, id in ipairs(growInfoIds) do
		local levelFundInfo = self:getLevelFundCfg()[id]
		if levelFundInfo then
			if playerLevel >= levelFundInfo.level then -- 等级到了
				if not self.freeClaimed[id] then -- 能领但是没有领
					minIndex = k
					break
				end
				if isBoughtBase then
					if not self.baseClaimed[id] then -- 能领但是没有领
						minIndex = k
						break
					end
				end
				if isBoughtAdvance then
					if not self.advanceClaimed[id] then -- 能领但是没有领
						minIndex = k
						break
					end
				end
			else
				break
			end
		end
	end
	return minIndex
end

-- 是否有未领取的奖励
function GrowthFundData:getIfCanClaimReward()
    if not self:getIsOpen() then
        return false
    end
    return self:getMinUnclaimedRewardIndex() > 0
end

function GrowthFundData:onClaimFundRewards(list)
	if list == nil then
		return
	end
	if #list <= 0 then
		return
	end
	for _, v in ipairs(list) do
		if v.grade == 0 then
			if not self.freeClaimed[v.id] then
				self.freeClaimed[v.id] = true
				self.freeClaimedCount = self.freeClaimedCount + 1
			end
		elseif v.grade == 1 then
			if not self.baseClaimed[v.id] then
				self.baseClaimed[v.id] = true
				self.baseClaimedCount = self.baseClaimedCount + 1
			end
		elseif v.grade == 2 then
			if not self.advanceClaimed[v.id] then
				self.advanceClaimed[v.id] = true
				self.advanceClaimedCount = self.advanceClaimedCount + 1
			end
		end
	end
	self:initFundGrade()
	self:markDirty()
end

function GrowthFundData:onBoughtFund()
	self:markDirty()
end

return GrowthFundData