local ActTimeData = class("ActTimeData", BaseData)

function ActTimeData:ctor()
	self.isInited = false
	self.actTimeList = {}
end

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

---- 活动改版后，服务器只会发一个活动类型的当前或下一次的数据，不是想之前一样无脑把activity表转过来
function ActTimeData:init(data)
	if EDITOR_MODE then
		Logger.logHighlight("活动时间初始化-------------------------------------------------------------")
		for index, info in ipairs(data) do
			Logger.logHighlight("活动时间初始化:%s id:%s type:%s time:%s %s %s cur:%s", index, info.id,  info.type, info.start_at, info.end_at, info.extra_at, Time:getServerTime())
		end
	end
	data = data or {}
	self.actTimeList = data
	self:refreshActTime()
	self.isInited = true
end

function ActTimeData:refreshActTime()
	local curTime = Time:getServerTime()
	self.actTypeTimeList = {}
	for _, info in ipairs(self.actTimeList) do
		local actId = info.id
		if ModuleManager.ActivityManager:getActivityInCfg(actId) then
			local actType = info.type
			local startTime = info.start_at or 0
			if startTime < 0 then
				startTime = 0
			end

			local endTime = info.end_at
			if endTime < 0 then
				endTime = 0
			end

			local extraTime = info.extra_at
			if extraTime < endTime then
				extraTime = endTime
			end

			local version = info.version or 0

			local actGifts = info.gifts or {}
			local data = info.data
			Logger.logHighlight("活动暂未开启：" .. (curTime - extraTime))
			-- if startTime <= curTime and curTime < extraTime then -- 在有效期内
				if self.actTypeTimeList[actType] == nil or self.actTypeTimeList[actType].endTime > endTime then
					self.actTypeTimeList[actType] = {actId = actId, actType = actType, startTime = startTime, endTime = endTime, extraTime = extraTime, actGifts = actGifts, data = data, version = version}
					self:initActivityData(actType, self.actTypeTimeList[actType])
				else
					-- 如果start_time更小 替换
					if self.actTypeTimeList[actType].startTime > startTime then
						self.actTypeTimeList[actType] = {actId = actId, actType = actType, startTime = startTime, endTime = endTime, extraTime = extraTime, actGifts = actGifts, data = data, version = version}
						self:initActivityData(actType, self.actTypeTimeList[actType])
					-- 如果version更小 替换
					elseif self.actTypeTimeList[actType].version > version then
						self.actTypeTimeList[actType] = {actId = actId, actType = actType, startTime = startTime, endTime = endTime, extraTime = extraTime, actGifts = actGifts, data = data, version = version}
						self:initActivityData(actType, self.actTypeTimeList[actType])
					end
				end
			-- else
			-- 	if EDITOR_MODE then
			-- 		Logger.logHighlight("活动暂未开启：" .. info.id)
			-- 	end
			-- end
		end
	end
	self:setDirty()
end

function ActTimeData:getActTimeList()
   return self.actTimeList
end

function ActTimeData:getActTimeInfoByActId(id)
	for _, info in ipairs(self.actTimeList) do
		if info.id == id then
			return info
		end
	end
end

function ActTimeData:getActStartTime(actId)
	for _, info in ipairs(self.actTimeList) do
		if info.id == actId then
			return info.start_at
		end
	end
end

function ActTimeData:getActEndTime(actId)
	for _, info in ipairs(self.actTimeList) do
		if info.id == actId then
			local extraTime = info.extra_at
			if extraTime < info.end_at then
				extraTime = info.end_at
			end
			return extraTime
		end
	end
end

function ActTimeData:getActExtraTime(actId)
	for _, info in ipairs(self.actTimeList) do
		if info.id == actId then
			local extraTime = info.extra_at
			if extraTime < info.end_at then
				extraTime = info.end_at
			end
			return extraTime
		end
	end
end

function ActTimeData:getActIsOpen(actId)
	local curTime = Time:getServerTime()
	local actStartTime = self:getActStartTime(actId) or 0
	local actEndTime = self:getActEndTime(actId) or 0
	if actStartTime <= curTime and curTime < actEndTime then
		return true
	end
	return false
end

function ActTimeData:getActTypeTimeList()
	return self.actTypeTimeList or {}
end

function ActTimeData:initActivityData(actType, info)
	-- if isCrossDayClientRefresh and GConst.ActivityConst.DISABLE_CLIENT_CROSS_DAY_REFRESH[actType] then
	-- 	return
	-- end
	local data = DataManager[GConst.ActivityConst.ACTIVITY_TYPE_DATA[actType]]
	if data then
		if self.isInited and not data:getIsOpen() then
			-- 跨天开启的新活动，提前清理礼包数据
			if EDITOR_MODE then
				Logger.logHighlight("开启活动 act_id = " .. tostring(info.actId))
			end
			if data.clearActInfo then
				data:clearActInfo(info.actId)
			end
		end

		if data.initActInfo then
			data:initActInfo(info)-- 初始化基类方法
		end
		data:setActivityInfo(info)-- 初始化子类方法
	else
		-- 冲刺活动 无法用type区分 用id区分
		local data = DataManager.ActSprintData:getSprintDetailData(info.actId)
		if data then
			if self.isInited and not data:getIsOpen() then
			-- 跨天开启的新活动，提前清理礼包数据
				if EDITOR_MODE then
					Logger.logHighlight("开启活动 act_id = " .. tostring(info.actId))
				end
				if data.clearActInfo then
					data:clearActInfo(info.actId)
				end
			end

			if data.initActInfo then
				data:initActInfo(info)-- 初始化基类方法
			end
			data:setActivityInfo(info)-- 初始化子类方法
		end
	end
end

return ActTimeData