local IdleData = class("IdleData", BaseData)

function IdleData:ctor()
	self.data.dirty = false
	self.idleRewardMap = {}
	self.idleQuickPreviewRewardMap = {}
end

function IdleData:init(data)
	data = data or GConst.EMPTY_TABLE
	self.data.dropTime = (data.claim_at or 0) // 1000
	self.data.adCount = data.ad_count or 0
	self.data.energyCount = data.energy_count or 0
end

function IdleData:onGetIdleShowRewrad(data)
	data = data or GConst.EMPTY_TABLE
	self.data.dropTime = (data.claim_at or 0) // 1000
	self.data.adCount = data.ad_count or 0
	self.data.energyCount = data.energy_count or 0
	self:updateIdleRewards(data.items)
	self:markDirty()
end

function IdleData:updateIdleRewards(items)
	if items == nil then
		return
	end
	for k, v in pairs(self.idleRewardMap) do
		self.idleRewardMap[k].num = 0
	end
	for id, num in pairs(items) do
		if self.idleRewardMap[id] then
			self.idleRewardMap[id].num = num
		else
			self.idleRewardMap[id] = {
				id = id,
				num = num,
				type = GConst.REWARD_TYPE.ITEM
			}
		end
	end
	self:markDirty()
end

function IdleData:getIdleRewards()
	return self.idleRewardMap
end

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

function IdleData:getLastDropTime()
	return self.data.dropTime
end

function IdleData:onGetIdleRewrad(time)
	self.data.dropTime = time // 1000
	for k, v in pairs(self.idleRewardMap) do
		self.idleRewardMap[k].num = 0
	end
	self:markDirty()
end

function IdleData:onGetIdleQuickRewrad(data)
	self.data.adCount = data.ad_count or 0
	self.data.energyCount = data.energy_count or 0
	self:markDirty()
end

function IdleData:getIdleQuickDropPreviewRewards()
	for k, v in pairs(self.idleQuickPreviewRewardMap) do
		self.idleQuickPreviewRewardMap[k].num = 0
	end

    local chapterId = DataManager.ChapterData:getMaxChapterId()
    local cfg = ConfigManager:getConfig("chapter")[chapterId]
    if cfg == nil then
		return self.idleQuickPreviewRewardMap
    end
	local constCfg = ConfigManager:getConfig("const")
    local coinPerTime = constCfg.idle_exp_drop_time.value
    local expPerTime = constCfg.idle_gold_drop_time.value
	local itemPerTime = constCfg.idle_item_drop_time.value
    local time = self:getQuickIdleTime()

	local itemGold = self.idleQuickPreviewRewardMap[GConst.ItemConst.ITEM_ID_GOLD]
	if itemGold == nil then
		itemGold = {}
		itemGold.id = GConst.ItemConst.ITEM_ID_GOLD
		itemGold.type = GConst.REWARD_TYPE.ITEM
		self.idleQuickPreviewRewardMap[GConst.ItemConst.ITEM_ID_GOLD] = itemGold
	end
	itemGold.num = math.floor(cfg.idle_gold * (time // coinPerTime))

	local itemExp = self.idleQuickPreviewRewardMap[GConst.ItemConst.ITEM_ID_EXP]
	if itemExp == nil then
		itemExp = {}
		itemExp.id = GConst.ItemConst.ITEM_ID_EXP
		itemExp.type = GConst.REWARD_TYPE.ITEM
		self.idleQuickPreviewRewardMap[GConst.ItemConst.ITEM_ID_EXP] = itemExp
	end
	itemExp.num = math.floor(cfg.idle_exp * (time // expPerTime))

	local itemCard = self.idleQuickPreviewRewardMap[GConst.ItemConst.ITEM_ID_RANDOM_FRAGMENT]
	if itemCard == nil then
		itemCard = {}
		itemCard.id = GConst.ItemConst.ITEM_ID_RANDOM_FRAGMENT
		itemCard.type = GConst.REWARD_TYPE.ITEM
		self.idleQuickPreviewRewardMap[GConst.ItemConst.ITEM_ID_RANDOM_FRAGMENT] = itemCard
	end
	itemCard.num = math.floor((time // itemPerTime))

	return self.idleQuickPreviewRewardMap
end

function IdleData:getIsOpen()
	return ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.IDLE_DROP, true)
end

function IdleData:getIdleGoldDropTime()
	if self.idleGoldDropTime == nil then
		self.idleGoldDropTime = ConfigManager:getConfig("const")["idle_gold_drop_time"].value
	end
	return self.idleGoldDropTime
end

function IdleData:getIdleExpDropTime()
	if self.idleExpDropTime == nil then
		self.idleExpDropTime = ConfigManager:getConfig("const")["idle_exp_drop_time"].value
	end
	return self.idleExpDropTime
end

function IdleData:getIdleMaxTime()
	if self.idleMaxTime == nil then
		self.idleMaxTime = ConfigManager:getConfig("const")["idle_maxtime"].value
	end
	return self.idleMaxTime
end

function IdleData:getGoldPerHour()
	local goldDrop = DataManager.ChapterData:getGoldDrop()
	local time = self:getIdleGoldDropTime()
	return math.floor(goldDrop * 3600 / time)
end

function IdleData:getExpPerHour()
	local expDrop = DataManager.ChapterData:getExpDrop()
	local time = self:getIdleExpDropTime()
	return math.floor(expDrop * 3600 / time)
end

function IdleData:getQuickIdleMaxAdTimes()
	if self.quickIdleMaxAdTimes == nil then
		self.quickIdleMaxAdTimes = ConfigManager:getConfig("const")["idle_drop_fast_times_1"].value
	end
	return self.quickIdleMaxAdTimes
end

function IdleData:getQuickIdleAdRemainTimes()
	return self:getQuickIdleMaxAdTimes() - self.data.adCount
end

function IdleData:getQuickIdleMaxVitTimes()
	if self.quickIdleMaxVitTimes == nil then
		self.quickIdleMaxVitTimes = ConfigManager:getConfig("const")["idle_drop_fast_times_2"].value
	end
	return self.quickIdleMaxVitTimes
end

function IdleData:getQuickIdleVitRemainTimes()
	return self:getQuickIdleMaxVitTimes() - self.data.energyCount
end

function IdleData:getQuickIdleRemainTimes()
	return self:getQuickIdleMaxAdTimes() + self:getQuickIdleMaxVitTimes() - self.data.adCount - self.data.energyCount
end

function IdleData:getQuickIdleCost()
	if self.quickIdleCost == nil then
		self.quickIdleCost = ConfigManager:getConfig("const")["idle_drop_fast_cost"].reward
	end
	return self.quickIdleCost
end

function IdleData:getQuickIdleTime()
	if self.quickIdleTime == nil then
		self.quickIdleTime = ConfigManager:getConfig("const")["idle_drop_fast_time"].value
	end
	return self.quickIdleTime
end

return IdleData