local SummonData = class("SummonData", BaseData)
local DEFAULT_FACTOR = GConst.BattleConst.DEFAULT_FACTOR

local SummonCfg = ConfigManager:getConfig("summon")

function SummonData:ctor()
	self:clear()
end

function SummonData:clear()
	self.summonDataMap = nil
	self.data.isDirty = false
	DataManager:unregisterCrossDayFunc("SummonData")
end

function SummonData:setDirty()
	self.data.isDirty = not self.data.isDirty
end

function SummonData:init(data)
	data = data or {}
	if EDITOR_MODE then
		Logger.logHighlight("抽奖数据")
		Logger.printTable(data)
	end
	self:initData(data)
	self:setDirty()
	if not self.isInit then
		self.isInit = true
		-- 跨天
		-- DataManager:registerCrossDayFunc("SummonData", function()
		-- 	self:setDirty()
		-- end)	
	end
end

function SummonData:initData(data)
	self.summonDataMap = {}
	for i = 1, 2 do
		self.summonDataMap[i] = {}
		if data[i] then
			self.summonDataMap[i].freeCount = data[i].free_count
			self.summonDataMap[i].adCount = data[i].ad_count
			self.summonDataMap[i].statCount = data[i].stat_count
			self.summonDataMap[i].triggerCount = data[i].trigger_count
			self.summonDataMap[i].wishId = data[i].wish_id
			self.summonDataMap[i].wishCount = data[i].wish_count
		else
			self.summonDataMap[i].freeCount = 0
			self.summonDataMap[i].adCount = 0
			self.summonDataMap[i].statCount = 0
			self.summonDataMap[i].triggerCount = 0
			self.summonDataMap[i].wishId = 0
			self.summonDataMap[i].wishCount = 0
		end
	end
    -- int64 free_count    = 1; // 今日免费次数
    -- int64 ad_count      = 2; // 今日广告免费次数
    -- int64 stat_count    = 3; // 总抽取次数
    -- int64 trigger_count = 4; // 保底累计次数
    -- int32 wish_id       = 5; // 心愿英雄 ID
    -- int64 wish_count    = 6; // 心愿累计次数
end

function SummonData:getIsOpen(showToast)
	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.SUMMON_FORCE, not showToast) then
		return false
	end
	return true
end

--@region 抽奖数据
--是否有免费抽奖
function SummonData:hasSummonFree(summonType)
	local freeCount = self:getSummonFreeCount(summonType)
	if freeCount > 0 then
		return true
	end
	local adCount = self:getSummonAdCount(summonType)
	if adCount > 0 then
		return true
	end
	return false
end

-- 心愿次数
function SummonData:getSummonWishCount(summonType)
	if self.summonDataMap[summonType] then
		return self.summonDataMap[summonType].wishCount or 0
	end
	return 0
end

-- 获取心愿英雄id
function SummonData:getSummonWishHeroId(summonType)
	if self.summonDataMap[summonType] then
		return self.summonDataMap[summonType].wishId
	end
	return 0
end

-- 获取免费抽取次数
function SummonData:getSummonFreeCount(summonType)
	local freeCount = self:getSummonFreeNum(summonType)
	local useCount = 0
	if self.summonDataMap[summonType] then
		useCount = self.summonDataMap[summonType].freeCount or 0
	end
	return freeCount - useCount
end

-- 获取免费广告抽取次数
function SummonData:getSummonAdCount(summonType)
	local freeCount = self:getSummonAdNum(summonType)
	local useCount = 0
	if self.summonDataMap[summonType] then
		useCount = self.summonDataMap[summonType].adCount or 0
	end
	return freeCount - useCount
end

-- 保底累计次数
function SummonData:getSummonTriggerCount(summonType)
	if self.summonDataMap[summonType] then
		return self.summonDataMap[summonType].triggerCount or 0
	end
	return 0
end

-- 获取抽奖总次数
function SummonData:getSummonCount(summonType)
	if self.summonDataMap[summonType] then
		return self.summonDataMap[summonType].statCount or 0
	end
	return 0
end

-- 重新设置英雄成功过
function SummonData:updateSummonWishHeroId(summonType, id)
	if self.summonDataMap[summonType] then
		if self.summonDataMap[summonType].wishId == id then
			self.summonDataMap[summonType].wishId = nil
		else
			self.summonDataMap[summonType].wishId = id
		end
		self:setDirty()
	end
end

function SummonData:summonWishClaim(summonType)
	if self.summonDataMap[summonType] then
		self.summonDataMap[summonType].wishCount = self.summonDataMap[summonType].wishCount - 100
		self:setDirty()
	end
end

-- 刷新抽奖次数
function SummonData:updateSummonCount(summonType, times, free)
	if self.summonDataMap[summonType] then
		local unlockTimes = self:getSummonWishUnlock(summonType)
		if unlockTimes and self.summonDataMap[summonType].statCount + times >= unlockTimes then
			if self.summonDataMap[summonType].statCount < unlockTimes then
				self.summonDataMap[summonType].wishCount = self.summonDataMap[summonType].wishCount + times -
					(unlockTimes - self.summonDataMap[summonType].statCount)
			else
				self.summonDataMap[summonType].wishCount = self.summonDataMap[summonType].wishCount + times
			end
		end
		self.summonDataMap[summonType].statCount = self.summonDataMap[summonType].statCount + times
		if free and free == 1 then
			self.summonDataMap[summonType].adCount = self.summonDataMap[summonType].adCount + 1
		elseif free and free == 2 then
			self.summonDataMap[summonType].freeCount = self.summonDataMap[summonType].freeCount + 1
		end
	end
	self:setDirty()
end

function SummonData:setSummonTrigger(summonType, isReset)
	if isReset then
		self.summonDataMap[summonType].triggerCount = 0
	else
		self.summonDataMap[summonType].triggerCount = self.summonDataMap[summonType].triggerCount + 1
		local max = SummonCfg[summonType].guarantee1
		if self.summonDataMap[summonType].triggerCount >= max then
			self.summonDataMap[summonType].triggerCount = self.summonDataMap[summonType].triggerCount - max
		end
	end
end
--@endregion

--@region 红点
function SummonData:hasSummonCostRedPoint()
	return self:hasSummonFree(1) or self:hasSummonFree(2)
end
--@endregion

--@region 获取消耗
-- 获取当前所需的抽卡消耗
function SummonData:getSummonCosts(summonType, count)
	local costs = {}
	local costItem = self:getSummonItemCost(summonType)
	local costItemId = costItem.id
	local costItemNum = costItem.num    -- 单抽消耗道具数量
	local costItemHas = DataManager.BagData.ItemData:getItemNumById(costItemId) --拥有的道具数量
	local costGem = self:getSummonGemCost(summonType)
	-- if self:getIsActivity() then
	-- 	table.insert(costs, {type = GConst.REWARD_TYPE.ITEM, id = costItemId, num = costItemNum * count })
	-- else
		if costItemHas < costItemNum then -- 道具不足以单抽   全用宝石
			if not costGem then
				table.insert(costs, {type = GConst.REWARD_TYPE.ITEM, id = costItemId, num = costItemNum * count})
			else
				local costGemId = costGem.id
				local costGemNum = costGem.num
				table.insert(costs, {type = GConst.REWARD_TYPE.ITEM, id = costGemId, num = costGemNum * count})
			end
		elseif costItemHas < costItemNum * count then -- 有道具但是不足  道具和宝石混合
			--道具
			local meetCount = math.floor(costItemHas / costItemNum)
			table.insert(costs, {type = GConst.REWARD_TYPE.ITEM, id = costItemId, num = meetCount * costItemNum})
			if costGem then
				--钻石
				local costGemId = costGem.id
				local costGemNum = costGem.num
				local diffNum = count - meetCount
				table.insert(costs, {type = GConst.REWARD_TYPE.ITEM, id = costGemId, num = costGemNum * diffNum})
			end
		else -- 道具满足
			table.insert(costs, {type = GConst.REWARD_TYPE.ITEM, id = costItemId, num = costItemNum * count})
		end
	-- end
	return costs
end
--@endregion

--@region 配置
function SummonData:getSummonConfig(id)
	if id then
		return SummonCfg[id]
	else
		return SummonCfg
	end
end

----- 获取概率
function SummonData:getSummonShow(summonType)
	return SummonCfg[summonType].show
end

-- 获取单抽消耗
function SummonData:getSummonItemCost(summonType)
	return SummonCfg[summonType].item_cost
end

-- 获取单抽钻石消耗
function SummonData:getSummonGemCost(summonType)
	return SummonCfg[summonType].cost
end

function SummonData:getSummonWishUnlock(summonType)
	return SummonCfg[summonType].guarantee3
end

-- 心愿英雄列表
function SummonData:getSummonWishConfig(summonType)
	return SummonCfg[summonType].love
end

-- 心愿保底次数
function SummonData:getSummonWishGuarantee2(summonType)
	return SummonCfg[summonType].guarantee2
end

-- 小保底次数
function SummonData:getSummonWishGuarantee1(summonType)
	return SummonCfg[summonType].guarantee1
end

function SummonData:getSummonFreeCd(summonType)
	return SummonCfg[summonType].ad_time
end

function SummonData:getSummonWeight(summonType)
	return SummonCfg[summonType].summon
end

function SummonData:getSummonAdNum(summonType)
	return SummonCfg[summonType].ad_num
end

function SummonData:getSummonFreeNum(summonType)
	return SummonCfg[summonType].free_num
end
--@endregion

return SummonData