local DungeonData = class("DungeonData", BaseData)

-- 所有活动副本数据
function DungeonData:ctor()
	self.data.isDirty = false
end

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

function DungeonData:clear()
	DataManager:unregisterCrossDayFunc("DungeonData")
end

function DungeonData:init()
	DataManager:registerCrossDayFunc("DungeonData", function()
		self:onDayChange()
	end)
end

-- 跨天处理
function DungeonData:onDayChange()
	self:setDirty()
end

-- 是否开启任意一个副本
function DungeonData:isOpenAnyone(showToast)
	local moduleKey = nil
	for i, key in pairs(self:getDungeonList(false)) do
		if self:isOpen(key) then
			return true
		end
		if moduleKey == nil then
			moduleKey = key
		end
	end
	if showToast then
		if moduleKey == GConst.DungeonConst.MODULE_KEY_DUNGEON_DAILY then
			moduleKey = DataManager.DungeonDailyData:getNotShowModuleKey()
		end
		if moduleKey then
			GFunc.showToast(ModuleManager:getNotOpenStr(moduleKey))
		end
	end
	return false
end

function DungeonData:getNotOpenStr(moduleKey)
	 if moduleKey == GConst.DungeonConst.MODULE_KEY_DUNGEON_DAILY then
		moduleKey = DataManager.DungeonDailyData:getNotShowModuleKey()
	end
	return ModuleManager:getNotOpenStr(moduleKey)
end

-- 某副本是否已开启
function DungeonData:isOpen(moduleKey, showToast)
	if moduleKey == GConst.DungeonConst.MODULE_KEY_DUNGEON_DAILY and DataManager.DungeonDailyData:isOpen(showToast) then
		return true
	elseif moduleKey == ModuleManager.MODULE_KEY.RUNES_OPEN and DataManager.DungeonRuneData:isOpen(showToast) then
		return true
	end
	return false
end

function DungeonData:isRankOpen(rankId)
	-- if rankId == GConst.DungeonConst.RANK_TYPE.TOWER then
	-- 	return self:isOpen(ModuleManager.MODULE_KEY.TOWER)
	-- elseif rankId == GConst.DungeonConst.RANK_TYPE.WORLD_BOSS then
	-- 	return self:isOpen(ModuleManager.MODULE_KEY.WORLD_BOSS)
	-- elseif rankId == GConst.DungeonConst.RANK_TYPE.PRISON_PHYSICS or
	-- 		rankId == GConst.DungeonConst.RANK_TYPE.PRISON_WIND or
	-- 		rankId == GConst.DungeonConst.RANK_TYPE.PRISON_LIGHT or
	-- 		rankId == GConst.DungeonConst.RANK_TYPE.PRISON_FIRE or
	-- 		rankId == GConst.DungeonConst.RANK_TYPE.PRISON_ELECTRIC or
	-- 		rankId == GConst.DungeonConst.RANK_TYPE.PRISON_ICE then
	-- 	return self:isOpen(ModuleManager.MODULE_KEY.PRISON)
	-- end
	return false
end

function DungeonData:getModuleByRankId(id)
	for moduleKey, moduleRankId in pairs(GConst.DungeonConst.IDS) do
		if moduleRankId == id then
			return moduleKey
		end
	end
	return nil
end

-- 是否有红点
function DungeonData:hasRedPoint(moduleKey)
	if moduleKey == nil then
		for moduleKey, id in pairs(GConst.DungeonConst.IDS) do
			if self:hasRedPoint(moduleKey) then
				return true
			end
		end
	elseif moduleKey == GConst.DungeonConst.MODULE_KEY_DUNGEON_DAILY then
		return DataManager.DungeonDailyData:getIsShowRedPoint()
	-- elseif moduleKey == ModuleManager.MODULE_KEY.ELITE_CHALLENGE then
	-- 	return DataManager.EliteChallengeData:showRedPoint()
	else
		if not self:isOpen(moduleKey) then
			return false
		end
		-- 有次数
		if self:getHasTime(moduleKey) > 0 then
			return true
		end
		-- 有奖励
		if self:canGetReward(moduleKey) then
			return true
		end
		if self:hasAdRp(moduleKey) then
			return true
		end
	end
	return false
end

function DungeonData:getConfig(id)
	if id then
		return ConfigManager:getConfig("dungeon")[id]
	else
		return ConfigManager:getConfig("dungeon")
	end
end

-- 获取展示奖励
function DungeonData:getShowRewards(moduleKey)
	if moduleKey == GConst.DungeonConst.MODULE_KEY_DUNGEON_DAILY then
		return GFunc.getConstCost("dungeon_show_reward", true)
	elseif moduleKey == ModuleManager.MODULE_KEY.RUNES_OPEN then
		return GFunc.getConstCost("dungeon_show_reward", true)
	-- elseif moduleKey == ModuleManager.MODULE_KEY.TOWER then
	-- 	return GFunc.getConstCost("tower_show_reward", true)
	-- elseif moduleKey == ModuleManager.MODULE_KEY.WORLD_BOSS then
	-- 	return GFunc.getConstCost("worldboss_show_reward", true)
	-- elseif moduleKey == ModuleManager.MODULE_KEY.ELITE_CHALLENGE then
	-- 	return GFunc.getConstCost("elite_challenge_reward", true)
	-- elseif moduleKey == ModuleManager.MODULE_KEY.ELEMENT_DUNGEON then
	-- 	return GFunc.getConstCost("element_dungeon_show_reward", true)
	else
		return GFunc.getConstCost("dungeon_show_reward", true)
	end
end

-- 返回副本列表，sort
function DungeonData:getDungeonList(checkOpen)
	local sortDungeons = {}
	for moduleKey, id in pairs(GConst.DungeonConst.IDS) do
		if not checkOpen then -- or self:isOpen(moduleKey)
			local temp = {module = moduleKey, sort = id}
			table.insert(sortDungeons, temp)
		end
	end
	table.sort(sortDungeons, function(a, b)
		local isOpenA = self:isOpen(a.module)
		local isOpenB = self:isOpen(b.module)
		if isOpenA and isOpenB then
			return a.sort < b.sort
		elseif isOpenA ~= isOpenB then
			if isOpenA then
				return true
			else
				return false
			end
		else
			return a.sort < b.sort
		end
	end)
	if EDITOR_MODE then
		Logger.logHighlight("开启副本:")
		Logger.printTable(sortDungeons)
	end

	local moduleNames = {}
	for i, info in ipairs(sortDungeons) do
		table.insert(moduleNames, info.module)
	end

	return moduleNames
end

-- 当前副本次数
function DungeonData:getHasTime(moduleKey)
	-- if moduleKey == ModuleManager.MODULE_KEY.TOWER then
	-- 	return DataManager.TowerData:getRemainChallengeCount()
	-- elseif moduleKey == ModuleManager.MODULE_KEY.WORLD_BOSS then
	-- 	return DataManager.WorldBossData:getRemainChallengeCount()
	-- elseif moduleKey == ModuleManager.MODULE_KEY.DOOR then
	-- 	return DataManager.DoorData:getRemainChallengeCount()
	-- elseif moduleKey == ModuleManager.MODULE_KEY.ELEMENT_DUNGEON then
	-- 	return DataManager.ElementData:getTodayRemainTimes()
	-- end
	return 0
end

-- 每日免费次数
function DungeonData:getFreeTime(moduleKey)
	-- if moduleKey == ModuleManager.MODULE_KEY.TOWER then
	-- 	return DataManager.TowerData:getDailyFreeTime()
	-- elseif moduleKey == ModuleManager.MODULE_KEY.WORLD_BOSS then
	-- 	return DataManager.WorldBossData:getDailyFreeTime()
	-- elseif moduleKey == ModuleManager.MODULE_KEY.ELEMENT_DUNGEON then
	-- 	return DataManager.ElementData:getTodayMaxTimes()
	-- end
	return 0
end

function DungeonData:canGetReward(moduleKey)
	-- if moduleKey == ModuleManager.MODULE_KEY.TOWER then
	-- 	return DataManager.TowerData:hasAvailableReward()
	-- elseif moduleKey == ModuleManager.MODULE_KEY.WORLD_BOSS then
	-- 	return DataManager.WorldBossData:hasRankRewardRp()
	-- elseif moduleKey == ModuleManager.MODULE_KEY.PRISON then
	-- 	return DataManager.PrisonData:hasRedPoint()
	-- elseif moduleKey == ModuleManager.MODULE_KEY.ELEMENT_DUNGEON then
	-- 	return DataManager.ElementData:hasRankRewardRp()
	-- end
	return false
end

-- 有广告红点
function DungeonData:hasAdRp(moduleKey)
	return false
end

function DungeonData:getDungeonBIStr()
	local str
	-- if not self.dataDungeons then
	-- 	return GConst.EMPTY_STRING
	-- end
	-- for moduleKey, entity in pairs(self.dataDungeons) do
	-- 	if str then
	-- 		str = str .. "|"
	-- 	else
	-- 		str = GConst.EMPTY_STRING
	-- 	end
	-- 	Logger.logHighlight(moduleKey)
	-- 	str = str .. string.gsub(moduleKey, "_open", "") .. ":" .. entity:getPassedMaxId()
	-- end
	return str
end

return DungeonData