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

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

function ActSprintSummonData:clear()
	DataManager:unregisterCrossDayFunc("ActSprintSummonDataAll")
	self.isInit = false
end

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

function ActSprintSummonData:clearActInfo(actId)
	if self.actId ~= actId then
		-- 重置礼包
		self:tryResetGift(actId, true)
	end
end

function ActSprintSummonData:getActConfig(id)
	if id then
		return ConfigManager:getConfig("activity")[id]
	else
		return ConfigManager:getConfig("activity")
	end
end

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

function ActSprintSummonData:setActivityInfo(data)
	if self.actId ~= data.actId then
		self:clear()
	end
	self.actId = data.actId
	self.startTime = data.startTime
	self.endTime = data.endTime
	self.extraTime = data.extraTime

	if data.data ~= GConst.EMPTY_STRING then
		local info = NetManager:decodeActivityData("ActivityRushSign", data.data)
		if info then
			self:initActivityData(info)
		end
	end
	self:initBountyCfg()

	if not self.isInit then
		self.isInit = true
		-- 跨天
		DataManager:registerCrossDayFunc("ActSprintSummonDataAll", function()
			if not self:getIsOpen() then
				return
			end
			-- 当天开启 重置任务
			if self:getStartTime() == Time:getBeginningOfServerToday() then
				DataManager.ActTaskData:getTaskDataScore(self.actId)
			end
			-- local isCrossWeek = Time:getDayofWeek() == 1
			-- -- 重置礼包
			-- for _, actGiftId in ipairs(self:getActGiftList(self.actId)) do
			-- 	local cfgInfo = self:getActGiftConfig(actGiftId)
			-- 	if cfgInfo and cfgInfo.limit_type == 1 then
			-- 		DataManager.PaymentData:resetGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, actGiftId)
			-- 	end
			-- 	if cfgInfo and cfgInfo.limit_type == 2 and isCrossWeek then
			-- 		DataManager.PaymentData:resetGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, actGiftId)
			-- 	end
			-- end
			if self.bountyCurrDay then
				self.bountyCurrDay = self.bountyCurrDay + 1
			end
			self:tryResetGift()

			self:setDirty()
		end)
	end
end

function ActSprintSummonData:initActivityData(data)
	if EDITOR_MODE then
		Logger.logHighlight("初始化冲刺战令数据")
		Logger.printTable(data)
	end
	self.bountyCurrDay = data.day
	self.bountyInfo = data.sign or GConst.EMPTY_TABLE
end

--region 战令
function ActSprintSummonData:onBountySign(sign)
	if sign then
		self.bountyInfo = sign
		self:setDirty()
	end
end

function ActSprintSummonData:initBountyCfg()
	if self.bountyCfg == nil then
		self.bountyCfg = {}
		for id, info in pairs(ConfigManager:getConfig("act_bounty")) do
			if info.activity == self:getActId() then
				info.id = id
				self.bountyCfg[id] = info
			end
		end
	end
	if not self.bountyCfgList then
		self.bountyCfgList = {}
		for k,v in pairs(self.bountyCfg) do
			table.insert(self.bountyCfgList, v)
		end
		table.sort(self.bountyCfgList, function(a, b) return a.id < b.id end)
	end
end


function ActSprintSummonData:getBountyCfg(id)
	if id then
		return self.bountyCfg[id]
	else
		return self.bountyCfg
	end
end

function ActSprintSummonData:getBountyCfgList(id)
	if id then
		return self.bountyCfgList[id]
	else
		return self.bountyCfgList
	end
end

function ActSprintSummonData:getBountyCurrDay()
	return self.bountyCurrDay or 0
end

function ActSprintSummonData:getHasGotBountyReward(grade, day)
	if self.bountyInfo == nil then
		return false
	end
	local claimedMap = self.bountyInfo.claimed
	if claimedMap == nil then
		return false
	end
	local bountyCfg = self.bountyCfgList[day]
	if bountyCfg == nil then
		return false
	end
	local claimed = claimedMap[bountyCfg.id]
	if claimed == nil then
		return false
	end
	local list = claimed.grade
	if list == nil then
		return false
	end
	for k, v in ipairs(list) do
		if v == grade then
			return true
		end
	end
	return false
end

function ActSprintSummonData:isUnlockBountyPro()
	if self.bountyCfgList == nil then
		return false
	end
	local giftId = self.bountyCfgList[1].reward_pro_pay
	if giftId == nil then
		return false
	end
	local boughtTime = DataManager.PaymentData:getGiftBoughtTime(PayManager.PURCHARSE_TYPE.ACT_GIFT, giftId)
	if self:getStartTime() <= boughtTime and boughtTime <= self:getEndTime() then
		return true
	end
	return false
end

function ActSprintSummonData:getBountyProGiftId()
	if self.bountyCfgList == nil then
		return 0
	end
	local giftId = self.bountyCfgList[1].reward_pro_pay
	return giftId
end

function ActSprintSummonData:getBountyProRechargeId()
	if self.bountyCfgList == nil then
		return 0
	end
	local giftId = self.bountyCfgList[1].reward_pro_pay
	local cfg = ConfigManager:getConfig("act_gift")[giftId]
	return cfg and cfg.recharge_id or 0
end

function ActSprintSummonData:isHaveUnclaimedBountyReward()
	if self.bountyCfgList == nil then
		return false
	end
	local maxDay = #self.bountyCfgList
	local currDay = self:getBountyCurrDay()
	if currDay > maxDay then
		currDay = maxDay
	end
	for i = 1, currDay do
		if self:canClaimBountyReward(0, i) then
			return true
		end
		if self:canClaimBountyReward(1, i) then
			return true
		end
	end
	return false
end

function ActSprintSummonData:canClaimBountyReward(grade, day)
	local currDay = self:getBountyCurrDay()
	if currDay < day then -- 没到时间
		return false
	end
	if self:getHasGotBountyReward(grade, day) then -- 已经领过了
		return false
	end
	if grade == 0 then -- 免费
		return true
	else
		if self:isUnlockBountyPro() then
			return true
		end
	end
	return false
end

function ActSprintSummonData:hasBountyRp()
	if self:isHaveUnclaimedBountyReward() then
		return true
	end
	return false
end
--endregion

--region 活动时间
function ActSprintSummonData:syncedData()
	return self.syncData
end

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

	return self:getTimeOpen()
end

function ActSprintSummonData:getIsShow()
	local funcOpen = DataManager.ActivityData:getActFuncOpen(self:getActId())
	if funcOpen then
		local isFuncOpen = ModuleManager:getIsOpen(funcOpen, true)
		return isFuncOpen
	end

	return true
end

function ActSprintSummonData:clearActId()
	self.actId = nil
end

-- 任意活动开启均有效
function ActSprintSummonData:getTimeOpen()
	if not self.startTime then
		return false
	end

	local nowTime = Time:getServerTime()
	if self.startTime <= nowTime and nowTime <= self.extraTime then
		return true
	end

	return false
end

function ActSprintSummonData:getIsLastDay()
	if not self:getStartTime() then
		return false
	end

	local nowTime = Time:getServerTime()
	if self:getExtraTime() > nowTime and nowTime >= self:getEndTime() then
		return true
	end
end

function ActSprintSummonData:getTotalRemainTime()
	local remainTime = self:getNormalRemainTime()
	if remainTime <= 0 then
		remainTime = self:getRemainTime()
	end

	return remainTime
end

-- 获取活动时间
function ActSprintSummonData:getStartTime()
	return self.startTime or 0
end

function ActSprintSummonData:getEndTime()
	return self.endTime or 0
end

function ActSprintSummonData:getExtraTime()
	return self.extraTime or 0
end

function ActSprintSummonData:getRemainTime()
	if not self.startTime then
		return 0
	end

	local nowTime = Time:getServerTime()
	if self.extraTime < nowTime then
		return 0
	end

	return self.extraTime - nowTime
end

function ActSprintSummonData:getNormalRemainTime()
	local nowTime = Time:getServerTime()
	if self:getEndTime() < nowTime then
		return 0
	end

	return self:getEndTime() - nowTime
end
--endregion

--region 红点
function ActSprintSummonData:hasRedPoint()
	if not self:syncedData() then
		return false
	end

	if not self:getIsOpen() then
		return false
	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 ActSprintSummonData:getRpByPage(page)
	if page == 1 then
		return self:hasStandardRp()
	elseif page == 2 then
		return self:hasBountyRp()
	elseif page == 3 then
		return self:hasGiftRp()
	elseif page == 4 then
		return self:hasTaskRp()
	end
	return false
end

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

function ActSprintSummonData:hasStandardRp(actId)
	actId = actId or self:getActId()
	return DataManager.ActTaskData:getCanGetTurnRewardScore(actId) or DataManager.ActTaskData:getCanAnyTaskCanGetRewardScore(actId)
end

function ActSprintSummonData:hasGiftRp()
	if self.giftIds == nil then
		self.giftIds = DataManager.ActGiftData:getGiftIdsSort(self:getActId())
	end
	for i, id in ipairs(self.giftIds) do
		if DataManager.ActGiftData:isFreeGift(self:getActId(), id) and DataManager.ActGiftData:getGiftCanBuyCount(self:getActId(), id) > 0 then
			if DataManager.PaymentData:getIsSkipAd() then
				return true
			end
			return not self.fitstGift
		end
	end
end

function ActSprintSummonData:hasTaskRp()
	local taskList = DataManager.ActTaskData:getTaskIdList(self.actId)
	for _, actTaskId in ipairs(taskList) do
		if DataManager.ActTaskData:canClaimTask(self.actId, actTaskId) then
			return true
		end
	end
	return false
end
--endregion

--region 
function ActSprintSummonData:getBIType()
	return BIReport.ACT_SPRINT_TYPE.SUMMON
end

-- 普通任务
function ActSprintSummonData:getBINormalTaskNumInfo()
	local num, totalNum = 0, 0
	local taskList = DataManager.ActTaskData:getTaskIdList(self.actId)
	for _, taskId in ipairs(taskList) do
		if DataManager.ActTaskData:isTaskReceived(self.actId, taskId) then
			num = num + 1
		end
		totalNum = totalNum + 1
	end
	return num, totalNum
end

-- 达标任务
function ActSprintSummonData:getBISprintTaskNumInfo()
	local num, totalNum = 0, 0
	local taskList = DataManager.ActTaskData:getTaskIdListScore(self.actId)
	for _, taskId in ipairs(taskList) do
		if DataManager.ActTaskData:getHasGotTaskRewardScore(self.actId, taskId) then
			num = num + 1
		end
		totalNum = totalNum + 1
	end
	return num, totalNum
end

function ActSprintSummonData:getCanGetRewardStandardTaskList()
	local taskIdList = {}
	local taskList = DataManager.ActTaskData:getTaskIdListScore(self.actId)
	for _, taskId in ipairs(taskList) do
		if DataManager.ActTaskData:getCanGetTaskRewardScore(self.actId, taskId) then
			table.insert(taskIdList, taskId)
		end
	end
	return taskIdList
end
--endregion

return ActSprintSummonData