local GoldPigData = class("GoldPigData", BaseData)

function GoldPigData:ctor()
	self.data.dirty = false
	self.data.boughtFlag = false
end

function GoldPigData:clear()
	DataManager:unregisterDataCd("GoldPigData")
end

function GoldPigData:init(data, initOnLogin)
	data = data or GConst.EMPTY_TABLE
	self.data.id = data.current_id or 0
	if self.data.id == 0 then
		self.data.id = self:getFirstLevelId()
	end
	self.data.count = data.add_diamond or 0
	self.lastCount = self.data.count
	self.data.goldCount = data.add_gold or 0
	self.lastGoldCount = self.data.goldCount
	self.maxCount = self:getMaxCount()
	self.data.fullTime = (data.full_at or 0) // 1000
	self.data.buyTime = (data.buy_at or 0) // 1000
	self.endTime = 0
	self.nextShowTime = 0
	self.popFlag = false
	DataManager:unregisterDataCd("GoldPigData")
	self:checkOpen()
	if not initOnLogin then
		self:markDirty()
	end
end

function GoldPigData:onBought(data)
	self:init(data)
	self.data.boughtFlag = not self.data.boughtFlag
end

function GoldPigData:markDirty()
	self.data.dirty = not self.data.dirty
end

function GoldPigData:checkOpen()
	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.GOLD_PIG, true) then
		self.data.isOpen = false
		return
	end
	local initialCount = self:getInitialCount()
	if self.data.count <= initialCount then -- 激活后一定大于初始值,否则说明还没开
		self.data.isOpen = false
		local nextShowTime1 = 0
		if self.data.buyTime > 0 then
			nextShowTime1 = self.data.buyTime + self:getBoughtCD()
		end
		local nextShowTime2 = 0
		if self.data.fullTime > 0 then
			nextShowTime2 = self.data.fullTime + self:getDuration() + self:getTimeOverCD()
		end
		if nextShowTime1 == 0 then
			self.nextShowTime = nextShowTime2
		elseif nextShowTime2 == 0 then
			self.nextShowTime = nextShowTime1
		else
			if self.data.buyTime > self.data.fullTime then
				self.nextShowTime = nextShowTime1
			else
				self.nextShowTime = nextShowTime2
			end
		end
	elseif self.data.count >= self.maxCount then -- 满了
		self.endTime = self.data.fullTime + self:getDuration()
		self.nextShowTime = self.endTime + self:getTimeOverCD()
		if Time:getServerTime() >= self.endTime then -- 超过时间该消失了
			self.data.isOpen = false
			self:levelDown()
		else
			self.data.isOpen = true
			DataManager:registerDataCd("GoldPigData")
		end
	else
		self.data.isOpen = true
	end
end

function GoldPigData:getId()
	return self.data.id
end

function GoldPigData:getIsOpen()
	if GFunc.isShenhe() then
		return false
	end
	return self.data.isOpen
end

function GoldPigData:getPopFlag()
	if not self:getIsOpen() then
		return false
	end
	return self.popFlag
end

function GoldPigData:markPop()
	self.popFlag = false
end

function GoldPigData:addGoldPigCount()
	if not self.data.isOpen and not self:tryActiveGoldPig() then
		return
	end
	local info = self:getCurrLevelInfo()
	if info == nil then
		return
	end
	if self.data.count >= info.max_diamond then -- 已经满了
		self.lastCount = self.data.count
		self.lastGoldCount = self.data.goldCount
		return
	end
	self.lastCount = self.data.count
	self.lastGoldCount = self.data.goldCount
	self.data.count = self.data.count + info.battle_diamond
	self.data.goldCount = self.data.goldCount + info.battle_gold
	if self.data.count >= info.max_diamond then -- 满了
		self.data.count = info.max_diamond
		self.data.fullTime = Time:getServerTime()
		self.endTime = self.data.fullTime + self:getDuration()
		self.nextShowTime = self.endTime + self:getTimeOverCD()
		self.popFlag = true
		DataManager:registerDataCd("GoldPigData")
	end
end

function GoldPigData:tryActiveGoldPig()
	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.GOLD_PIG, true) then
		return false
	end
	if self.nextShowTime > 0 and Time:getServerTime() < self.nextShowTime then
		return false
	end
	self.data.isOpen = true
	return true
end

function GoldPigData:getLastCount()
	return self.lastCount
end

function GoldPigData:getCount()
	return self.data.count
end

function GoldPigData:getGoldCount()
	return self.data.goldCount
end

function GoldPigData:getIsFull()
	return self.data.count >= self.maxCount
end

function GoldPigData:getInitialCount()
	local info = self:getCurrLevelInfo()
	if info == nil then
		return 1
	end
	return info.min_diamond
end

function GoldPigData:getMaxCount()
	local info = self:getCurrLevelInfo()
	if info == nil then
		return 1
	end
	return info.max_diamond
end

function GoldPigData:getRechargeId()
	local info = self:getCurrLevelInfo()
	if info == nil then
		return 0
	end
	return info.recharge_id
end

function GoldPigData:getFirstLevelId()
	if self.firstLevelId == nil then
		self.firstLevelId = ConfigManager:getConfig("const")["act_gold_pig_first_id"].value
	end
	return self.firstLevelId
end

function GoldPigData:getTimeOverCD()
	if self.timeOverCD == nil then
		self.timeOverCD = ConfigManager:getConfig("const")["act_gold_pig_cd"].value * 3600
	end
	return self.timeOverCD
end

function GoldPigData:getBoughtCD()
	if self.boughtCD == nil then
		self.boughtCD = ConfigManager:getConfig("const")["act_gold_pig_buy_cd"].value * 3600
	end
	return self.boughtCD
end

function GoldPigData:getCfg()
	if self.cfg == nil then
		self.cfg = ConfigManager:getConfig("act_gold_pig")
	end
	return self.cfg
end

function GoldPigData:getDuration()
	if self.duration == nil then
		self.duration = ConfigManager:getConfig("const")["act_gold_pig_full_cd"].value * 3600
	end
	return self.duration
end

function GoldPigData:getCurrLevelInfo()
	local cfg = self:getCfg()
	return cfg[self.data.id]
end

function GoldPigData:levelDown()
	local levelInfo = self:getCurrLevelInfo()
	self.data.id = levelInfo.before_id

	local currLevelInfo = self:getCurrLevelInfo()
	self.data.count = currLevelInfo.min_diamond
	self.data.goldCount = currLevelInfo.min_gold
	self.lastCount = self.data.count
	self.lastGoldCount = self.data.goldCount
	self.maxCount = self:getMaxCount()
	self.popFlag = false
end

function GoldPigData:getEndTime()
	return self.endTime
end

function GoldPigData:updateCd()
	if not self.data.isOpen then
		DataManager:unregisterDataCd("GoldPigData")
		return
	end
	if self.endTime > 0 and Time:getServerTime() > self.endTime then -- 到时间该消失了
		DataManager:unregisterDataCd("GoldPigData")
		self:levelDown()
		self.data.isOpen = false
		self:markDirty()
	end
end

return GoldPigData