local ActGiftData = class("ActGiftData", BaseData)

local ActGift = ConfigManager:getConfig("act_gift")

function ActGiftData:ctor()
end

function ActGiftData:clear()
	self.giftConfig = nil
end

--@region 礼包
function ActGiftData:getActGiftsByActId(actId)
    local cfg = ConfigManager:getConfig("activity")[actId]
    if not cfg then
        return {}
    end
    return cfg.act_gift
end

function ActGiftData:getGiftConfig(actId, id)
	self.giftConfig = self.giftConfig or {}
	if self.giftConfig[actId] == nil then
		self.giftConfig[actId] = {}
		local ids = self:getActGiftsByActId(actId)
		for i, id in ipairs(ids) do
			local info = ConfigManager:getConfig("act_gift")[id]
			info.id = id
			self.giftConfig[actId][id] = info
		end
	end

	if id then
		return self.giftConfig[actId][id]
	else
		return self.giftConfig[actId]
	end
end

function ActGiftData:getGiftIdsSort(actId)
	local tempList = {}
	for id, info in pairs(self:getGiftConfig(actId)) do
		local temp = {id = info.id, _sort = info.id}

		-- if self:isFreeGift(actId, info.id) then
		-- 	temp._sort = temp._sort - 100000
		-- end
		if self:getGiftCanBuyCount(actId, info.id) <= 0 then
			temp._sort = temp._sort + 100000000000
		end

		table.insert(tempList, temp)
	end
	table.sort(tempList, function(a, b) return a._sort < b._sort end)

	local ids = {}
	for i, data in ipairs(tempList) do
		table.insert(ids, data.id)
	end

	return ids
end

-- 是否是广告礼包
function ActGiftData:isAdGift(actId, id)
	local cfg = self:getGiftConfig(actId, id)
	if cfg.item_cost == nil and cfg.recharge_id == nil and cfg.parameter_pro == nil then
		return true
	end
	return false
end

-- 是否是免费礼包
function ActGiftData:isFreeGift(actId, id)
	local cfg = self:getGiftConfig(actId, id)
	if cfg.parameter_pro and cfg.parameter_pro[1] and cfg.parameter_pro[1] == 1 then
		return true
	end
	return false
end

-- 是否是每日刷新礼包
function ActGiftData:isDailyGift(actId, id)
	local cfg = self:getGiftConfig(actId, id)
	if cfg.limit_type ~= nil and cfg.limit_type == 1 then
		return true
	end
	return false
end

-- 是否是每周刷新礼包
function ActGiftData:isWeekGift(actId, id)
	local cfg = self:getGiftConfig(actId, id)
	if cfg.limit_type ~= nil and cfg.limit_type == 2 then
		return true
	end
	return false
end

-- 获取礼包购买次数
function ActGiftData:getGiftBuyCount(id)
	return DataManager.PaymentData:getGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, id)
end

-- 获取礼包剩余可购买次数
function ActGiftData:getGiftCanBuyCount(actId, id)
	local cfg = self:getGiftConfig(actId, id)
	if cfg then
		return cfg.limit - self:getGiftBuyCount(id)
	end
	return 0
end

function ActGiftData:tryResetGift(actId, force)
	local ids = self:getActGiftsByActId(actId)
	if ids == nil then
		return
	end
	for index, id in ipairs(ids) do
		local config = ActGift[id]
		if config then
			if force or self:isDailyGift(actId, id) then
				DataManager.PaymentData:resetGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, id)
			elseif self:isWeekGift(actId, id) then
				local isCrossWeek = Time:getDayofWeek() == 1
				if isCrossWeek then
					DataManager.PaymentData:resetGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, id)
				end
			end
		end
	end
end

-- 礼包名
function ActGiftData:getActGiftI18NName(actGiftId)
	return I18N:getConfig("act_gift")[actGiftId] and I18N:getConfig("act_gift")[actGiftId].value
end
--@endregion

return ActGiftData