local ActivityData = class("ActivityData", BaseData)


function ActivityData:ctor()
end

function ActivityData:clear()
	self.checkedUIMap = nil

	self.taskData = nil
	self.taskIds = nil
end

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

function ActivityData:getActOpenKey()
	return ModuleManager.MODULE_KEY.ACT_ALL_OPEN
end

function ActivityData:init(data)
	data = data or {}
end

function ActivityData:initActivityData(actId, data)
	if EDITOR_MODE then
		Logger.logHighlight("初始化活动任务数据：" .. actId)
		Logger.printTable(data)
	end

	if self.taskData == nil then
		self.taskData = {}
	end
	for i, info in pairs(data) do
		if self.taskData[actId] == nil then
			self.taskData[actId] = {}
		end
		self.taskData[actId][info.id] = info
	end
end

--region 兑换相关
function ActivityData:initExchangeData(data)
	if EDITOR_MODE then
		Logger.logHighlight("活动的兑换数据")
		Logger.printTable(data)
	end
	self.exchangeData = data and data.exchange_count or {}
	self.exchangeTime = data and data.exchange_at or {}
end

-- 获取已兑换次数
function ActivityData:getExchangeCount(id)
	if self.exchangeData == nil then
		return 0
	end
	return self.exchangeData[id] or 0
end

function ActivityData:onActivityExchange(id, count)
	if self.exchangeData == nil then
		return
	end
	if not self.exchangeData[id] then
		self.exchangeData[id] = 0
	end
	self.exchangeData[id] = self.exchangeData[id] + (count or 0)
	self.exchangeTime[id] = Time:getServerTime()
	self:setDirty()
end

function ActivityData:getExchangeTime(id)
	if self.exchangeTime == nil then
		return 1
	end
	return self.exchangeTime[id] or 1
end
--endregion

function ActivityData:initSkipPopInfo()
	self.loginPopSkipFlag = false
	local flag = LocalData:getSkipPopFlag()
	self.loginPopSkipFlag = flag == 1

	local lastTime = LocalData:getSkipPopUITime()
	if lastTime ~= Time:getBeginningOfServerToday() then
		self.loginPopSkipFlag = false
		self.theDayFirstLogin = true
		flag = 0
	else
		self.theDayFirstLogin = false
	end

	LocalData:setSkipPopFlag(flag)
	LocalData:setSkipPopUITime()
end

function ActivityData:getLoginPopSkipFlag()
	-- if not GFunc.IsGotServerTime() then
	-- 	return false
	-- end

	if self.loginPopSkipFlag == nil then
		self:initSkipPopInfo()
	end
	return self.loginPopSkipFlag
end

function ActivityData:setLoginPopSkipFlag(value)
	if self.loginPopSkipFlag == value then
		return
	end

	self.loginPopSkipFlag = value
	local flag = 0
	if self.loginPopSkipFlag then
		flag = 1
	end
	LocalData:setSkipPopFlag(flag)
	LocalData:setSkipPopUITime()
end

function ActivityData:getTheDayFirstLogin()
	-- if not GFunc.IsGotServerTime() then
	-- 	return false
	-- end

	if self.theDayFirstLogin == nil then
		self:initSkipPopInfo()
	end

	return self.theDayFirstLogin
end

---- 进入过某个界面
function ActivityData:getCheckUI(moduleName)
	if not self.checkedUIMap then
		return false
	end
	return self.checkedUIMap[moduleName]
end

function ActivityData:setCheckUI(moduleName)
	if not moduleName then
		return
	end
	if not self.checkedUIMap then
		self.checkedUIMap = {}
	end
	self.checkedUIMap[moduleName] = true
end

---- 活动通用接口
function ActivityData:getActConfig(actId)
	if actId then
		return ConfigManager:getConfig("activity")[actId]
	else
		return ConfigManager:getConfig("activity")
	end
end

-- 判定活动是否存在
function ActivityData:getActExist(actId)
	return self:getActConfig(actId) ~= nil
end

-- 判定活动类型
function ActivityData:getActType(actId)
	if not self:getActExist(actId) then
		return
	end
	return self:getActConfig(actId).act_type
end

-- 获取活动ui类型
function ActivityData:getActUIType(actId)
	if not self:getActExist(actId) then
		return
	end
	return self:getActConfig(actId).ui_type
end

-- 获得活动时间类型
function ActivityData:getActTimeType(actId)
	if not self:getActExist(actId) then
		return
	end

	return self:getActConfig(actId).time_type
end

function ActivityData:getActCfgStartTime(actId)
	if not self:getActExist(actId) then
		return 0
	end
	return self:getActConfig(actId).start_time_1 or 0
end

function ActivityData:getActCfgEndTime(actId)
	if not self:getActExist(actId) then
		return 0
	end
	return self:getActConfig(actId).end_time_1 or 0
end

function ActivityData:getActCfgExtraTime(actId)
	if not self:getActExist(actId) then
		return 0
	end
	return self:getActConfig(actId).extra_time_1 or 0
end

function ActivityData:getActCfgStartTime2(actId)
	if not self:getActExist(actId) then
		return 0
	end
	return self:getActConfig(actId).start_time_2
end

function ActivityData:getActCfgEndTime2(actId)
	if not self:getActExist(actId) then
		return 0
	end
	return self:getActConfig(actId).end_time_2
end

function ActivityData:getActCfgExtraTime2(actId)
	if not self:getActExist(actId) then
		return 0
	end
	return self:getActConfig(actId).extra_time_2
end

function ActivityData:getTurntableAdCount(actId)
	if not self:getActExist(actId) then
		return 0
	end
	return self:getActConfig(actId).ad_times or 0
end

function ActivityData:getActFuncOpen(actId)
	if not self:getActExist(actId) then
		return
	end
	return self:getActConfig(actId).func_open
end

-- 获取活动开始是否忽略创号天数
function ActivityData:getActIgnoreCreateDay(actId)
	if not self:getActExist(actId) then
		return
	end
	return self:getActConfig(actId).circle_limit
end

function ActivityData:getActRoundReward(actId)
	if not self:getActExist(actId) then
		return {}
	end
	return self:getActConfig(actId).round_reward
end

function ActivityData:getActRoundLimit(actId)
	if not self:getActExist(actId) then
		return 0
	end
	return self:getActConfig(actId).round_limit
end

-- 获取活动相关内容是否可展示
function ActivityData:canShowActContent(actId)
	if actId == nil then
		return true
	end
	if not self:getActExist(actId) then
		return true
	end

	local startTime, endTime, extraTime = self:getActTimeInfo(actId)

	local hour = GFunc.getConstIntValue("act_show_cultivation_time")

	return Time:getServerTime() >= (startTime - hour * 3600)
end

-- 获取活动时间配置信息
function ActivityData:getActTimeInfo(actId)
	if actId == nil then
		return nil, nil, nil
	end
	if not self:getActExist(actId) then
		return nil, nil, nil
	end

	local startDay = self:getActCfgStartTime(actId)
	local endDay = self:getActCfgEndTime(actId)
	local extraDay = self:getActCfgExtraTime(actId)

	local startDay2 = self:getActCfgStartTime2(actId)
	local endDay2 = self:getActCfgEndTime2(actId)
	local extraDay2 = self:getActCfgExtraTime2(actId)

	local timeType = self:getActTimeType(actId)
	local startTime = 0
	local endTime = 0
	local extraTime = 0
	if timeType == 1 then -- 开服时间
		startTime = DataManager.PlayerData:getServerOpenBeginTime()
		startTime = startTime + (startDay - 1) * 86400
		endTime = startTime + endDay * 86400
		extraTime = endTime + extraDay * 86400
	elseif timeType == 2 then -- 建号时间
		startTime = DataManager.PlayerData:getCreatePlayerBeginTime()
		startTime = startTime + (startDay - 1) * 86400
		endTime = startTime + endDay * 86400
		extraTime = endTime + extraDay * 86400
	elseif timeType == 3 then -- 固定时间
		startTime = Time:getCertainTimeByStr(startDay2)
		endTime = Time:getCertainTimeByStr(endDay2)
		extraTime = Time:getCertainTimeByStr(extraDay2)
	end

	-- Logger.logHighlight(actId)
	-- Logger.logHighlight(startTime)
	-- Logger.logHighlight(endTime)

	return startTime, endTime, extraTime
end

-- 活动是否在开启时间
function ActivityData:getActIsOpen(actId)
	local startTime, endTime = self:getActTimeInfo(actId)
	if startTime == nil or endTime == nil then
		return false
	end

	return Time:getServerTime() >= startTime and Time:getServerTime() <= endTime
end

-- 活动是否已经结束
function ActivityData:getActIsOver(actId)
	local startTime, endTime = self:getActTimeInfo(actId)
	if startTime == nil or endTime == nil then
		return false
	end

	return Time:getServerTime() >= endTime
end

-- 获得活动时间戳, 返回即将开的下一次的时间戳
function ActivityData:getActStampTime(actId, customStartTime)
	if not self:getActExist(actId) then
		return
	end

	local config = self:getActConfig(actId)
	local duration
	local pause
	if config.circle then
		duration = config.circle[1]
		pause = config.circle[2]
	end

	local startDay = self:getActCfgStartTime(actId)
	local endDay = self:getActCfgEndTime(actId)
	local extraDay = self:getActCfgExtraTime(actId)

	local timeType = self:getActTimeType(actId)
	local openDay = 1
	if timeType == 1 then -- 开服时间
		openDay = DataManager.PlayerData:getServerOpenDay()
	elseif timeType == 2 then -- 建号时间
		openDay = DataManager.PlayerData:getCreateDay()
	else
		pause = nil
	end

	if pause then
		if customStartTime then
			local startTime = customStartTime + duration * 86400 + pause * 86400
			local endTime = startTime + duration * 86400
			local extraTime = endTime + extraDay * 86400
			return startTime, endTime, extraTime
		end
		while (startDay <= endDay) do
			if startDay >= openDay then -- 即将开
				break
			-- elseif startDay <= openDay and (endDay + extraDay) > openDay then -- 正在开
			-- 	break
			else
				startDay = startDay + duration + pause
			end
		end
	end

	local startTime = 0
	local endTime = 0
	local extraTime = 0
	if timeType == 1 then -- 开服时间
		startTime = DataManager.PlayerData:getServerOpenBeginTime()
		startTime = startTime + (startDay - 1) * 86400
		endTime = startTime + endDay * 86400
		extraTime = endTime + extraDay * 86400
	elseif timeType == 2 then -- 建号时间
		startTime = DataManager.PlayerData:getCreatePlayerBeginTime()
		startTime = startTime + (startDay - 1) * 86400
		endTime = startTime + endDay * 86400
		extraTime = endTime + extraDay * 86400
	elseif timeType == 3 then -- 固定时间
		startTime = DataManager.ActTimeData:getActStartTime(actId) or 0
		endTime = DataManager.ActTimeData:getActEndTime(actId) or 0
		extraTime = DataManager.ActTimeData:getActExtraTime(actId) or 0

		if startTime < Time:getServerTime() then -- 只需要即将开的
			startTime = 0
			endTime = 0
			extraTime = 0
		end
	elseif timeType == 4 then -- 基于固定时间顺延(服务器已处理,同3一样即可)
		startTime = DataManager.ActTimeData:getActStartTime(actId) or 0
		endTime = DataManager.ActTimeData:getActEndTime(actId) or 0
		extraTime = DataManager.ActTimeData:getActExtraTime(actId) or 0

		if startTime < Time:getServerTime() then -- 只需要即将开的
			startTime = 0
			endTime = 0
			extraTime = 0
		end
	end

	return startTime, endTime, extraTime
end

function ActivityData:getActPushInfo()
	if not ModuleManager:getIsOpen(DataManager.ActivityData:getActOpenKey(), true) then
		return
	end
	local nextOpenInfo = {}
	local nextExtraInfo = {} -- 只会在当前开着的活动中查找
	local ids = self:getCurVersionIDs()
	local curTime = Time:getServerTime()
	for actType, info in pairs(DataManager.ActTimeData:getActTypeTimeList()) do
		local config = self:getActConfig(info.actId)
		if config.begin_mail and info.startTime > curTime then
			table.insert(nextOpenInfo, {actId = info.actId, actType = config.act_type, startTime = info.startTime, endTime = info.endTime, extraTime = info.extraTime})
		elseif config.end_mail and info.startTime <= curTime and info.endTime > curTime then
			table.insert(nextExtraInfo, {actId = info.actId, actType = config.act_type, startTime = info.startTime, endTime = info.endTime, extraTime = info.extraTime})
		else
			for _, id in ipairs(ids) do
				local newConfig = self:getActConfig(id)
				if newConfig.begin_mail and id >= info.actId and newConfig.type == actType then
					local startTime, endTime, extraTime = self:getActStampTime(id, info.startTime)
					if startTime and startTime > curTime then
						table.insert(nextOpenInfo, {actId = id, actType = config.act_type, startTime = startTime, endTime = endTime, extraTime = extraTime})
					end
				end
			end
		end
	end

	return nextOpenInfo, nextExtraInfo
end

function ActivityData:getCurVersionIDs()
	if not self.curVersionIds then
		self.curVersionIds = {}
		local versionIds = {}
		for id, info in pairs(self:getActConfig()) do
			if info.version then
				if not versionIds[info.version] then
					versionIds[info.version] = {}
				end
				table.insert(versionIds[info.version], id)
			end
		end
		local version = 0
		for v, list in pairs(versionIds) do
			if v > version then
				self.curVersionIds = list
			end
		end
	end

	return self.curVersionIds
end

function ActivityData:tryResetActItem(actId)
	local cfg = self:getActConfig(actId)
	if cfg and cfg.parameter then
		local clearItemIds = cfg.parameter
		for i, id in ipairs(clearItemIds) do
			DataManager.BagData.ItemData:clearItemById(id)
		end
	end
end

return ActivityData