local ActBaseData = require "app/userdata/activity/act_base_data"
local ActSprintData = class("ActSprintData", ActBaseData)

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

function ActSprintData:clear()
	DataManager:unregisterCrossDayFunc("ActSprintData")
	ModuleManager.TaskManager:unRegisterAllModuleTask("ActSprintData")
	self.isInit = false

	self.exchanged = {} -- 兑换道具
end

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

function ActSprintData:init(data)
	data = data or {}
	self.exchanged = data.exchanged or {}

	if not self.isInit then
		self.isInit = true
		-- 跨天
		DataManager:registerCrossDayFunc("ActSprintData", function()
			local isCrossWeek = Time:getDayofWeek() == 1
			-- 重置兑换
			for id, _ in pairs(self.exchanged) do
				if self:getExchangeRefreshType(id) == 1 then
					self.exchanged[id] = 0
				elseif self:getExchangeRefreshType(id) == 2 then
					if isCrossWeek then
						self.exchanged[id] = 0
					end
				end
			end

			self:setDirty()
		end)
	end
end

function ActSprintData:getSprintDetailData(actId)
	if actId == GConst.ActSprintConst.ACT_ID.SUMMON_ALL then
		return DataManager.ActSprintSummonDataAll
	end
	-- if actId == GConst.ActSprintConst.ACT_ID.SUMMON then
	-- 	return DataManager.ActSprintSummonData
	-- elseif actId == GConst.ActSprintConst.ACT_ID.CORE_SOUL_BOX then
	-- 	return DataManager.ActSprintBoxData
	-- elseif actId == GConst.ActSprintConst.ACT_ID.SUMMON_V2 then
	-- 	return DataManager.ActSprintSummonDataV2
	-- elseif actId == GConst.ActSprintConst.ACT_ID.CORE_SOUL_BOX_V2 then
	-- 	return DataManager.ActSprintBoxDataV2
	-- elseif actId == GConst.ActSprintConst.ACT_ID.SUMMON_ALL then
	-- 	return DataManager.ActSprintSummonDataAll
	-- elseif actId == GConst.ActSprintConst.ACT_ID.CORE_SOUL_BOX_ALL then
	-- 	return DataManager.ActSprintBoxDataAll
	-- elseif actId == GConst.ActSprintConst.ACT_ID.SUMMON_LOOP then
	-- 	return DataManager.ActSprintSummonDataLoop
	-- elseif actId == GConst.ActSprintConst.ACT_ID.CORE_SOUL_BOX_LOOP then
	-- 	return DataManager.ActSprintBoxDataLoop
	-- elseif actId == GConst.ActSprintConst.ACT_ID.TREE_LOOP then
	-- 	return DataManager.ActSprintTreeDataLoop
	-- end
end

function ActSprintData:getIsOpen()
	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.ACT_GOGOGO, true) then
		return false
	end

	return self:getTimeOpen()
end

function ActSprintData:getActSprintIdList()
	self.openIdList = table.clearOrCreate(self.openIdList)
	for key, actId in pairs(GConst.ActSprintConst.ACT_ID) do
		local data = self:getSprintDetailData(actId)
		if data and data:getIsOpen() then
			table.insert(self.openIdList, actId)
		end
	end

	if EDITOR_MODE then
		Logger.logHighlight("当前开启的冲刺活动:")
		Logger.printTable(self.openIdList)
	end

	return self.openIdList
end

-- 任意活动开启均有效
function ActSprintData:getTimeOpen()
	local isAnyOpen = false
	for key, actId in pairs(GConst.ActSprintConst.ACT_ID) do
		local data = self:getSprintDetailData(actId)
		if data and data:getTimeOpen() and data:getIsShow() then
			isAnyOpen = true
			break
		end
	end
	return isAnyOpen
end

function ActSprintData:getIsLastDay()
	local isLastDay = false
	for key, actId in pairs(GConst.ActSprintConst.ACT_ID) do
		local data = self:getSprintDetailData(actId)
		if data and data:getIsLastDay() then
			isLastDay = true
			break
		end
	end
	return isLastDay
end

function ActSprintData:getTotalRemainTime()
	local totalRemainTime
	for key, actId in pairs(GConst.ActSprintConst.ACT_ID) do
		local data = self:getSprintDetailData(actId)
		if data and data:getTotalRemainTime() then
			totalRemainTime = data:getTotalRemainTime()
			break
		end
	end
	return totalRemainTime
end

function ActSprintData:getRemainTime()
	local maxRemainTime = 0
	for key, actId in pairs(GConst.ActSprintConst.ACT_ID) do
		local data = self:getSprintDetailData(actId)
		if data and data:getRemainTime() > maxRemainTime then
			maxRemainTime = data:getRemainTime()
		end
	end
	return maxRemainTime
end

function ActSprintData:getNormalRemainTime(actId)
	-- 找目前最长时间的
	local maxRemainTime = 0
	for key, actId in pairs(GConst.ActSprintConst.ACT_ID) do
		local data = self:getSprintDetailData(actId)
		if data and data:getNormalRemainTime() > maxRemainTime then
			maxRemainTime = data:getNormalRemainTime()
		end
	end
	return maxRemainTime
end

function ActSprintData:hasRedPoint(ignoreExchange)
	if not self:getIsOpen() then
		return false
	end

	-- 兑换商店
	-- if not ignoreExchange then
	-- 	if self:hasExchangeRp() then
	-- 		return true
	-- 	end
	-- end

	if self:hasStandardRp() then
		return true
	end

	if self:hasGiftRp() then
		return true
	end

	if self:hasTaskRp() then
		return true
	end

	if self:hasBountyRp() then
		return true
	end

	return false
end

function ActSprintData:getActNameStr()
	return I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_1)
end

function ActSprintData:hasExchangeRp()
	if self:isShopOpen() then
		return self:getHasFreeExchangeItem()
	else
		return false
	end
end

function ActSprintData:hasStandardRp()
	for key, actId in pairs(GConst.ActSprintConst.ACT_ID) do
		local data = self:getSprintDetailData(actId)
		if data and data:hasStandardRp() then
			return true
		end
	end
	return false
end

function ActSprintData:hasGiftRp()
	for key, actId in pairs(GConst.ActSprintConst.ACT_ID) do
		local data = self:getSprintDetailData(actId)
		if data and data:hasGiftRp() then
			return true
		end
	end
	return false
end

function ActSprintData:hasTaskRp()
	for key, actId in pairs(GConst.ActSprintConst.ACT_ID) do
		local data = self:getSprintDetailData(actId)
		if data and data:hasTaskRp() then
			return true
		end
	end
	return false
end

function ActSprintData:hasBountyRp()
	for key, actId in pairs(GConst.ActSprintConst.ACT_ID) do
		local data = self:getSprintDetailData(actId)
		if data and data:hasBountyRp() then
			return true
		end
	end
	return false
end
--@endregion

-- 活动数据相关 **********************************************************************
function ActSprintData:getActConfig(id)
	if id then
		return ConfigManager:getConfig("activity")[id]
	else
		return ConfigManager:getConfig("activity")
	end
end

function ActSprintData:getActType(id)
	return self:getActConfig(id).act_type
end

function ActSprintData:getActGiftList(id)
	return self:getActConfig(id).act_gift
end

function ActSprintData:getActShowReward(id)
	return self:getActConfig(id).show_reward
end

-- 兑换道具相关 **********************************************************************

function ActSprintData:isShopOpen()
	-- 特定活动才会开放
	for key, actId in pairs(GConst.ActSprintConst.ACT_ID) do
		local data = self:getSprintDetailData(actId)
		if data and data:getIsOpen() then
			if not GConst.ActSprintConst.ACT_ID_NOT_SHOP[actId] then
				return true
			end
		end
	end
	return false
end

function ActSprintData:getExchangeCount(id)
	return self.exchanged[id] or 0
end

function ActSprintData:setExchangeCount(id, count)
	self.exchanged[id] = count
end

function ActSprintData:onExchangeSuccess(id, count)
	self.exchanged[id] = (self.exchanged[id] or 0) + count
	self:setDirty()
end

function ActSprintData:getExchangeConfig(id)
	if id then
		return ConfigManager:getConfig("act_exchange")[id]
	else
		return ConfigManager:getConfig("act_exchange")
	end
end

function ActSprintData:getExchangeIdList()
	local cfg = self:getExchangeConfig()
	self.exchangeIdList = table.clearOrCreate(self.exchangeIdList)
	for id, info in pairs(cfg) do
		-- 策划说不配的是兑换活动 相信策划 不会改的
		if info.activity == nil and self:getExchangeShow(id) then
			table.insert(self.exchangeIdList, id)
		end
	end
	-- 排序
    table.sort(self.exchangeIdList, function(a, b)
		local isFreeA = self:getExchangeCost(a) == nil
		local isFreeB = self:getExchangeCost(b) == nil
		if isFreeA == isFreeB then
			local canBuyA = DataManager.ActSprintData:getExchangeRemainCount(a) > 0
			local canBuyB = DataManager.ActSprintData:getExchangeRemainCount(b) > 0
			if canBuyA and canBuyB then
				return a < b
			elseif not canBuyA and not canBuyB then
				return a < b
			else
				return canBuyA
			end
		else
			return isFreeA
		end
    end)
	return self.exchangeIdList
end

function ActSprintData:getExchangeReward(id)
	return self:getExchangeConfig(id).reward
end

function ActSprintData:getExchangeCost(id)
	return self:getExchangeConfig(id).cost
end

function ActSprintData:getExchangeRefreshType(id)
	return self:getExchangeConfig(id).refresh_type
end

function ActSprintData:getExchangeLimit(id)
	return self:getExchangeConfig(id).limit
end

function ActSprintData:getExchangeFuncOpen(id)
	return self:getExchangeConfig(id).func_open
end

function ActSprintData:getExchangeForceId(id)
	return self:getExchangeConfig(id).force_id
end

function ActSprintData:getExchangeShow(id)
	-- local funcOpen = self:getExchangeFuncOpen(id)
	-- local forceId = self:getExchangeForceId(id)
	-- if funcOpen and not ModuleManager:getIsOpen(funcOpen, true) then
	-- 	return false
	-- end
	-- if forceId and not DataManager.ForceData:getIsOwnForce(forceId) then
	-- 	return false
	-- end
	return true
end

function ActSprintData:getExchangeRemainCount(id)
	return self:getExchangeLimit(id) - self:getExchangeCount(id)
end

function ActSprintData:getHasFreeExchangeItem()
	local cfgList = self:getExchangeIdList()
	for _, id in ipairs(cfgList) do
		if self:getExchangeCost(id) == nil and self:getExchangeRemainCount(id) > 0 then
			return true
		end
	end
	return false 
end

return ActSprintData