local BountyData = class("BountyData", BaseData)

function BountyData:ctor()
	self.startTime = 0
	self.endTime = 0
	-- 当前赛季数据
	self.season = 0
	self.level = 1
	self.exp = 0
	self.todayExp = 0
	self.receivedRewards = {}
	self.unlockPro = {}
	-- 上次赛季数据,如果已付费则可领奖
	self.previousSeason = 1
	self.previousLevel = 1
	self.previousExp = 0
	self.previousReceivedRewards = {}
	self.previousUnlockPro = {}

	self.data.isDirty = false
end

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

function BountyData:clear()
	self.startTime = 0
	self.endTime = 0
	-- 当前赛季数据
	self.season = 0
	self.level = 1
	self.exp = 0
	self.todayExp = 0
	self.receivedRewards = {}
	self.unlockPro = {}
	-- 上次赛季数据,如果已付费则可领奖
	self.previousSeason = 1
	self.previousLevel = 1
	self.previousExp = 0
	self.previousReceivedRewards = {}
	self.previousUnlockPro = {}

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

function BountyData:init(data)
	data = data or {}
	if EDITOR_MODE then
		Logger.logHighlight("战令数据")
		Logger.printTable(data)
	end

	self.startTime = data.open_at or 0-- 本赛季开启时间
	self.endTime = self.startTime + self:getCycleDay() * 86400
	-- 战令数据
	self.season = data.season or self:getMaxSeason()
	self.level = data.level or 1
	self.exp = data.exp or 0
	self.todayExp = data.today_exp or 0
	-- 领奖状态
	self.receivedRewards = {}
	self.receivedRewards[GConst.BountyConst.REWARD_GEAR.FREE] = data.claimed or 0
	self.receivedRewards[GConst.BountyConst.REWARD_GEAR.PRO_1] = data.pro_claimed or 0
	self.receivedRewards[GConst.BountyConst.REWARD_GEAR.PRO_2] = data.pro2_claimed or 0
	-- 解锁状态
	self.unlockPro = {}
	self.unlockPro[GConst.BountyConst.REWARD_GEAR.PRO_1] = data.bought or false
	self.unlockPro[GConst.BountyConst.REWARD_GEAR.PRO_2] = data.bought2 or false
	-- 上赛季
	local previousBounty = data.previous or {}
	self.previousSeason = previousBounty.season or 1
	self.previousLevel = previousBounty.level or 1
	self.previousExp = previousBounty.exp or 0
	self.previousReceivedRewards = {}
	self.previousReceivedRewards[GConst.BountyConst.REWARD_GEAR.FREE] = previousBounty.claimed or 0
	self.previousReceivedRewards[GConst.BountyConst.REWARD_GEAR.PRO_1] = previousBounty.pro_claimed or 0
	self.previousReceivedRewards[GConst.BountyConst.REWARD_GEAR.PRO_2] = previousBounty.pro2_claimed or 0
	self.previousUnlockPro = {}
	self.previousUnlockPro[GConst.BountyConst.REWARD_GEAR.PRO_1] = previousBounty.bought or false
	self.previousUnlockPro[GConst.BountyConst.REWARD_GEAR.PRO_2] = previousBounty.bought2 or false

	self:initConfig()
	self:fixLevel()

	self:setDirty()
	DataManager:registerCrossDayFunc("BountyData", function()
		self.todayExp = 0
		self:checkSeasonChange()
		self:setDirty()
    end)
	-- 如果功能未开启 注册功能开启监听
    if not self:isOpen() then
        DataManager:registerTryOpenFunc("BountyData", function()
            if not self:isOpen(false, true) then
                return
            end
            DataManager:unregisterTryOpenFunc("BountyData")

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

			self.startTime = Time:getDayBeginTimeStamp()
			self.endTime = self.startTime + self:getCycleDay() * 86400
			-- 战令数据
			self.season = self:getMaxSeason()
			self.level = 1
			self.exp = 0
			self.todayExp = 0
			-- 领奖状态
			self.receivedRewards = {}
			-- 解锁状态
			self.unlockPro = {}
			-- 上赛季
			self.previousSeason = 0
			self.previousLevel = 0
			self.previousExp = 0
			self.previousReceivedRewards = {}
			self.previousUnlockPro = {}

			self:initConfig()

			self:setDirty()
        end)
    end
end

-- 检查赛季改变（周期结束）
function BountyData:checkSeasonChange()
	if not self:isOpen() then
		return
	end
	if self:getRemainTime() > 0 then
		return
	end

	self.startTime = self.endTime
	self.endTime = self.startTime + self:getCycleDay() * 86400

	self.previousSeason = self.season
	self.previousLevel = self.level
	self.previousExp = self.exp
	self.previousReceivedRewards = self.receivedRewards
	self.previousUnlockPro = self.unlockPro

	self.season = math.min(self.season + 1, self:getMaxSeason())
	self.level = 1
	self.exp = 0
	self.receivedRewards = {}
	self.unlockPro = {}

	self:initConfig()
end

-- 服务器的等级不设上限,需要客户端修订
function BountyData:fixLevel()
	if self.level > self:getMaxLv() then
		self.level = self:getMaxLv()
	end
end

-- 是否开启
function BountyData:isOpen(showToast, noCheckTime)
	if GFunc.isShenhe() then
		return false
	end
	-- if not GFunc.IsGotServerTime() then
	-- 	return false
	-- end
	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.BOUNTY, not showToast) then
        return false
    end
	-- if not noCheckTime and (self:getStartTime() <= 0 or self:getEndTime() <= 0) then
	-- 	return false
	-- end
	
	return true
end

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

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

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

function BountyData:showRedPoint()
	if not self:isOpen() then
		return false
	end
	if self:canGetRewards() then
		return true
	end

	return false
end

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

function BountyData:initConfig()
	self.configList = {}
	for id, data in pairs(ConfigManager:getConfig("bounty_level")) 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 BountyData:getMaxSeason()
	if self.maxSeason == nil then
		self.maxSeason = 1
		for id, data in pairs(ConfigManager:getConfig("bounty_level")) do
			if data.season > self.maxSeason then
				self.maxSeason = data.season
			end
		end
	end
	return self.maxSeason
end

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

-- 获取升级所需经验
function BountyData:getLvUpExp()
	if self:getLevel() >= self:getMaxLv() then
		return 0
	end
	local curCfg = self.configList[self:getLevel()]
	return curCfg.exp
end

-- 获取奖励
function BountyData:getReward(type, lv)
	local cfg = self.configList[lv]
	if type == GConst.BountyConst.REWARD_GEAR.FREE then
		return cfg.reward
	elseif type == GConst.BountyConst.REWARD_GEAR.PRO_1 then
		return cfg.reward_pro
	elseif type == GConst.BountyConst.REWARD_GEAR.PRO_2 then
		return cfg.reward_pro_max
	end
end

-- 获取循环时间
function BountyData:getCycleDay()
	return GFunc.getConstIntValue("bounty_duration")
end

-- 获取每日最高所获经验
function BountyData:getDailyMaxExp()
	return GFunc.getConstIntValue("bounty_daily_limit")
end

-- 获取经验兑换比例
function BountyData:getExpRatio()
	return GFunc.getConstIntValue("bounty_point")
end

-- 获取高级奖励礼包id
function BountyData:getProRewardGiftId(type)
	return GConst.BountyConst.ACT_GIFT_IDS[type]
end

function BountyData:getSeason()
	return self.season	-- 测试
end

function BountyData:getLevel()
	return self.level
end

function BountyData:getExp()
	return self.exp
end

-- 今日已获得经验
function BountyData:getTodayExp()
	return self.todayExp
end

-- 获取已领取奖励等级
function BountyData:getReceivedLevel(type)
	return self.receivedRewards[type] or 0
end

-- 获取奖励解锁状态
function BountyData:isUnlockReward(type)
	if not self:isOpen() then
		return false
	end
	if type == GConst.BountyConst.REWARD_GEAR.FREE then
		return true
	end
	return self.unlockPro[type] or false
end

-- 获取上赛季等级
function BountyData:getPreviousLevel()
	return self.previousLevel or 0
end

-- 获取上赛季
function BountyData:getPreviousSeason()
	return self.previousSeason or 0
end

-- 获取上赛季领奖等级
function BountyData:getPreviousReceivedLevel(type)
	return self.previousReceivedRewards[type] or 0
end

-- 获取上赛季解锁状态
function BountyData:isPreviousUnlock(type)
	if type == GConst.BountyConst.REWARD_GEAR.FREE then
		return true
	end
	return self.previousUnlockPro[type] or false
end

function BountyData:getMoveIdx()
	local minLevel = self:getLevel()
	for k, gear in pairs(GConst.BountyConst.REWARD_GEAR) do
		if self:isUnlockReward(gear) then
			local lv = self:getReceivedLevel(gear) + 1
			minLevel = minLevel > lv and lv or minLevel
		end
	end
	return minLevel
end

-- 是否已领取奖励
function BountyData:isReceivedReward(type, level)
	return self:getReceivedLevel(type) >= level
end

-- 是否可领奖励
function BountyData:canClaimReward(type, level)
	if self:isReceivedReward(type, level) then
		return false
	end
	if not self:isUnlockReward(type) then
		return false
	end

	return self:getLevel() >= level
end

-- 是否可领取这赛季奖励
function BountyData:canGetRewards()
	if self:getLevel() > self:getReceivedLevel(GConst.BountyConst.REWARD_GEAR.FREE) then
		return true
	end
	if self:isUnlockReward(GConst.BountyConst.REWARD_GEAR.PRO_1) and self:getLevel() > self:getReceivedLevel(GConst.BountyConst.REWARD_GEAR.PRO_1) then
		return true
	end
	if self:isUnlockReward(GConst.BountyConst.REWARD_GEAR.PRO_2) and self:getLevel() > self:getReceivedLevel(GConst.BountyConst.REWARD_GEAR.PRO_2) then
		return true
	end
end

-- 是否可领取上赛季奖励
function BountyData:canGetPreviousRewards()
	if self:getPreviousLevel() > self:getPreviousReceivedLevel(GConst.BountyConst.REWARD_GEAR.FREE) then
		return true
	end
	if self:isPreviousUnlock(GConst.BountyConst.REWARD_GEAR.PRO_1) and self:getPreviousLevel() > self:getPreviousReceivedLevel(GConst.BountyConst.REWARD_GEAR.PRO_1) then
		return true
	end
	if self:isPreviousUnlock(GConst.BountyConst.REWARD_GEAR.PRO_2) and self:getPreviousLevel() > self:getPreviousReceivedLevel(GConst.BountyConst.REWARD_GEAR.PRO_2) then
		return true
	end
	return false
end

-- 解锁高级奖励
function BountyData:onUnlockPro(season, type)
	if self:getSeason() == season then
		self.unlockPro[type] = true
	elseif self:getPreviousSeason() == season then
		self.previousUnlockPro[type] = true
	end
	self:setDirty()
end

-- 领取所有可领取的奖励
function BountyData:onClaimAllRewards()
	self.receivedRewards[GConst.BountyConst.REWARD_GEAR.FREE] = self:getLevel()
	if self:isUnlockReward(GConst.BountyConst.REWARD_GEAR.PRO_1) then
		self.receivedRewards[GConst.BountyConst.REWARD_GEAR.PRO_1] = self:getLevel()
	end
	if self:isUnlockReward(GConst.BountyConst.REWARD_GEAR.PRO_2) then
		self.receivedRewards[GConst.BountyConst.REWARD_GEAR.PRO_2] = self:getLevel()
	end
	self:setDirty()
end

-- 领取遗留战令奖励
function BountyData:onGotBountyPreviousReward()
	self.previousReceivedRewards[GConst.BountyConst.REWARD_GEAR.FREE] = self.previousLevel
	if self:isPreviousUnlock(GConst.BountyConst.REWARD_GEAR.PRO_1) then
		self.previousReceivedRewards[GConst.BountyConst.REWARD_GEAR.PRO_1] = self.previousLevel
	end
	if self:isPreviousUnlock(GConst.BountyConst.REWARD_GEAR.PRO_2) then
		self.previousReceivedRewards[GConst.BountyConst.REWARD_GEAR.PRO_2] = self.previousLevel
	end
	self:setDirty()
end

-- 增加战令经验
function BountyData:addBountyExp(num)
	if not self:isOpen() then
		return
	end

	local exp = num * self:getExpRatio()
	self.exp = self.exp + exp
	self.todayExp = self.todayExp + exp

	local isLvUp = false
	if self.exp >= self:getLvUpExp() then
		self.exp = self.exp - self:getLvUpExp()
		self.level = self.level + 1
		isLvUp = true
	end
	-- 修订满级
	if self:getLevel() >= self:getMaxLv() then
		self.exp = 0
		self.level = self:getMaxLv()
	end

	if EDITOR_MODE then
		Logger.logHighlight("战令增加经验:" .. exp)
		Logger.logHighlight(" 当前等级:" .. self.level .. " 当前经验:" .. self.exp .. "今日获得经验:" .. self.todayExp .. "是否升级:" .. tostring(isLvUp))
	end

	BIReport:postBountyOpt(BIReport.BOUNTY_OPT.EXP_UP)
	if isLvUp then
		BIReport:postBountyOpt(BIReport.BOUNTY_OPT.LV_UP, BIReport.BOUNTY_LV_UP_TYPE.EXP)
	end

	self:setDirty()
end

function BountyData:getAllRewards(type)
	local rewards = {}
	for k, v in pairs(self.configList) do
		local reward = self:getReward(type,v.level)
		if rewards[reward.id] then
			rewards[reward.id].num = rewards[reward.id].num + reward.num
		else
			rewards[reward.id] = {num = reward.num} 
		end
	end
	return rewards
end

return BountyData