local SignWeekData = class("SignWeekData", BaseData)

function SignWeekData:ctor()
	self.startTime = 0
	self.endTime = 0
	self.claimedFree = {}
	self.claimedPay = {}
	
	self.data.isDirty = false
end

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

function SignWeekData:clear()
	self.startTime = 0
	self.endTime = 0
	self.claimedFree = {}
	self.claimedPay = {}

	DataManager:unregisterCrossDayFunc("SignWeekData")
	DataManager:unregisterTryOpenFunc("SignWeekData")
end

function SignWeekData:initData(data)
	data = data or {}
	if EDITOR_MODE then
		Logger.logHighlight("七天签到战令数据")
		Logger.printTable(data)
	end

	self.season = data.season or self:getMaxSeason()-- 赛季
	self.startTime = data.open_at or 0-- 本赛季开启时间
	self.endTime = self.startTime + self:getCycleDay() * 86400
	-- 战令数据
	self.claimedFree = data.claimed_free or {}
	self.claimedPay = data.claimed_pay or {}
	-- 初始化配置
	self:initConfig()

	self:setDirty()
	DataManager:registerCrossDayFunc("SignWeekData", function()
		if self:isOpen() then
			-- 跨周期
			if Time:getServerTime() >= self:getEndTime() then
				self.season = self:getMaxSeason()-- 用最新的赛季
				self.startTime = Time:getDayBeginTimeStamp()
				self.endTime = self.startTime + self:getCycleDay() * 86400
				self.claimedFree = {[1] = false}
				self.claimedPay = {}
				-- 初始化配置
				self:initConfig()
			else -- 正常跨天
				local day = self:calculateDay()
				if self:isUnlockPro() then
					self.claimedFree[day] = false
					self.claimedPay[day] = false
				else
					self.claimedFree[day] = false
				end
			end
			self:setDirty()
		end
    end)
	-- 如果功能未开启 注册功能开启监听
    if not self:isOpen() then
        DataManager:registerTryOpenFunc("SignWeekData", function()
            if not self:isOpen(false, true) then
                return
            end
            DataManager:unregisterTryOpenFunc("SignWeekData")

            if EDITOR_MODE then
                Logger.logHighlight("七天签到战令功能开启")
            end

			self.season = self:getMaxSeason()-- 用最新的赛季
			self.startTime = Time:getDayBeginTimeStamp()
			self.endTime = self.startTime + self:getCycleDay() * 86400
			self.claimedFree = {[1] = false}
			self.claimedPay = {}
			-- 初始化配置
			self:initConfig()

			self:setDirty()
        end)
    end
end

-- 是否开启
function SignWeekData:isOpen(showToast)
	if GFunc.isShenhe() then
		return false
	end
	if self.startTime and self.startTime > 0 then -- 如果已经开启过了则无视正常开启条件，直接开启
		return true
	end
	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.SIGN_BOUNTY, not showToast) then
        return false
    end
	return true
end

-- 获取开始时间
function SignWeekData:getStartTime()
	return self.startTime or 0
end

-- 获取结束时间
function SignWeekData:getEndTime()
	return self.endTime or 0
end

-- 获取赛季剩余时间
function SignWeekData:getRemainTime()
	return self:getEndTime() - Time:getServerTime()
end

-- 计算当前处于第几天
function SignWeekData:calculateDay()
	local diffTime = Time:getServerTime() - self:getStartTime()
	local day = math.floor(diffTime / 86400) + 1
	return day
end

-- 获得已签到天数
function SignWeekData:getSignDay()
	local totalDay = 0
	for k, v in pairs(self.claimedFree) do
		if v then
			totalDay = totalDay + 1
		end
	end
	return totalDay
end

function SignWeekData:showRedPoint()
	if not self:isOpen() then
		return false
	end
	local day = self:calculateDay()
	for i = 1, day do
		if self:getCanGetReward(GConst.SignConst.REWARD_TYPE.FREE, i) then
			return true
		end
	end

	return false
end

function SignWeekData:getMaxSeason()
	local maxSeason = 0
	for id, data in pairs(ConfigManager:getConfig("act_week_signin")) do
		maxSeason = math.max(maxSeason, data.season)
	end
	return maxSeason
end

-- 初始化排序后的配置列表
function SignWeekData:initConfig()
	self.configList = {}
	for id, data in pairs(ConfigManager:getConfig("act_week_signin")) do
		if data.season == self:getSeason() then
			data.id = id
			table.insert(self.configList, data)
		end
	end
	table.sort(self.configList, function (a, b)
		return a.id < b.id
	end)
end

function SignWeekData:getConfigList()
	return self.configList or {}
end

-- 最大等级
function SignWeekData:getMaxLv()
	return #self.configList
end

-- 获取奖励
function SignWeekData:getReward(type, day)
	local cfg = self.configList[day]
	if type == GConst.SignConst.REWARD_TYPE.FREE then
		return cfg.reward
	elseif type == GConst.SignConst.REWARD_TYPE.PRO then
		return cfg.reward_pro
	end
end

-- 获取循环时间
function SignWeekData:getCycleDay()
	return 7
end

-- 获取高级奖励礼包id
function SignWeekData:getProRewardGiftId()
	return GConst.SignConst.GIFT_ID[self:getSeason()]
end

-- 获取奖励解锁状态
function SignWeekData:isUnlockPro()
	if not self:isOpen() then
		return false
	end
	local boughtTime = DataManager.PaymentData:getGiftBoughtTime(PayManager.PURCHARSE_TYPE.ACT_GIFT, self:getProRewardGiftId())
	if self:getStartTime() <= boughtTime and boughtTime <= self:getEndTime() then
		return true
	end
	return false
end
function SignWeekData:onBountyBuySuccess()
	self:setDirty()
end
-- 是否已经领取
function SignWeekData:getHasGotReward(type, day)
	if type == GConst.SignConst.REWARD_TYPE.FREE then
		return self.claimedFree[day] or false
	elseif type == GConst.SignConst.REWARD_TYPE.PRO then
		return self.claimedPay[day] or false
	end
end

-- 是否可领取
function SignWeekData:getCanGetReward(type, day)
	local curDay = self:calculateDay()
	if day > curDay then
		return false
	else
		local got = self:getHasGotReward(type, day)
		if got then
			return false
		else
			if type == GConst.SignConst.REWARD_TYPE.FREE then
				if self.claimedFree[day] == false then
					return true
				end
			elseif type == GConst.SignConst.REWARD_TYPE.PRO then
				if self.claimedPay[day] == false then
					return true
				end
			end
		end
	end
	return false
end
function SignWeekData:getMinCanGetRewardDay()
	local day = self:calculateDay()
	for i = 1, day do
		if self:getCanGetReward(GConst.SignConst.REWARD_TYPE.FREE, i) then
			return i
		end
		if self:isUnlockPro() and self:getCanGetReward(GConst.SignConst.REWARD_TYPE.PRO, i) then
			return i
		end
	end
	return day
end
function SignWeekData:getSeason()
	return self.season
end

function SignWeekData:getReSignCost()
	return GFunc.getConstCost("act_bounty_buy_cost")
end

-- 解锁高级奖励
function SignWeekData:onUnlockPro()
	-- 请求领取所有奖励
	ModuleManager.SignManager:reqSignClaimed(nil, true)

	self:setDirty()
end

return SignWeekData