local DungeonRuneData = class("DungeonRuneData", BaseData)

local TASK_TYPE = GConst.DungeonRuneConst.TASK_TYPE

function DungeonRuneData:ctor()
	self.data.isDirty = false
	self.maxPassedId = 0
	self.runeInfo = {}
end

function DungeonRuneData:clear()
	self.data.isDirty = false
	DataManager:unregisterCrossDayFunc("DungeonRuneData")
end

function DungeonRuneData:init(data)
	data = data or {}
	if EDITOR_MODE then
		Logger.logHighlight("-----DungeonRuneData------")
		Logger.printTable(data)
	end

	data.total_challenge_count = 0
	data.stat_counts = {}
	data.max_challenge_id = 1
	data.turns = {}
	data.heroes = {}
	data.buy_silver_count = 0

	self.totalChallengeCount = data.total_challenge_count or 0
	self.fightCountMap = data.stat_counts or {}
	self.maxPassedId = data.max_challenge_id and (data.max_challenge_id - 1) or 0
	self.runeInfo = data.turns or {}
	self.heroes = data.heroes or {}
	self.buySilverCount = data.buy_silver_count or 0

	DataManager.FormationData:initFormationByType(GConst.BattleConst.FORMATION_TYPE.DUNGEON_RUNE, self.heroes)
	DataManager:registerCrossDayFunc("DungeonRuneData", function()
		self:onCrossDay()
	end)
end

function DungeonRuneData:onCrossDay()
	self.buySilverCount = 0
	self:setDirty()
end

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

function DungeonRuneData:isOpen(showToast)
	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.RUNES_OPEN, not showToast) then
		return false
	end
	return true
end

function DungeonRuneData:getModuleKey()
	return ModuleManager.MODULE_KEY.RUNES_OPEN
end

function DungeonRuneData:getConfig(chapterId)
	return ConfigManager:getConfig(self:getConfigName())[chapterId]
end

function DungeonRuneData:getConfigName()
	return "chapter_dungeon_rune"
end

function DungeonRuneData:getTitleString()
	return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_TITLE)
end

function DungeonRuneData:getRuleString()
	return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_HELP)
end

function DungeonRuneData:getOpenWeekString()
	return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC)
end

function DungeonRuneData:getBanner()
	return "assets/arts/textures/background/dungeon/dungeon_bg_5.png"
end

function DungeonRuneData:getOpenTextColor()
	return "#FFFFFF"
end

function DungeonRuneData:isNoTotalLimit()
	return true
end

function DungeonRuneData:isNotShowLimitCount()
	return true
end

function DungeonRuneData:getIsAllTimeOpen()
	return true
end

function DungeonRuneData:onClickFight()
	ModuleManager.DungeonRuneManager:showMainUI()
end

function DungeonRuneData:getTodayRemainLimitCount()
	return DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_GLOD_WING)
end

function DungeonRuneData:getRebirthAddRoundCount()
	if not self.rebirthAddRoundCount then
		self.rebirthAddRoundCount = GFunc.getConstIntValue("dungeon_rune_revival")
	end
	return self.rebirthAddRoundCount
end

function DungeonRuneData:getBattleMaxlv()
	if not self.battleMaxLv then
		self.battleMaxLv = GFunc.getConstIntValue("dungeon_rune_lvlimit")
	end
	return self.battleMaxLv
end

function DungeonRuneData:getBuySliverWingCost()
	if not self.buySliverWingCost then
		self.buySliverWingCost = GFunc.getConstReward("dungeon_rune_cost")
	end
	return self.buySliverWingCost
end

function DungeonRuneData:getPassedMaxId()
	return self.maxPassedId
end

function DungeonRuneData:updatePassedMaxId(maxId)
	if not maxId then
		return
	end
	self.maxPassedId = math.max(maxId - 1, 1)
end

function DungeonRuneData:canSweep(id)
	if id > self.maxPassedId then
		return false
	end
	if not self:isBossChapter(id) then
		return false
	end
	return DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_SLIVER_WING) > 0
end

function DungeonRuneData:canFight(id)
	if self.maxPassedId + 1 >= id then
		return true
	end
	return false
end

function DungeonRuneData:passedChapter(id)
	return self.maxPassedId >= id
end

function DungeonRuneData:curFight(id)
	return id == self.maxPassedId + 1
end

function DungeonRuneData:getChapterPassRound(id)
	if not self.runeInfo[id] then
		return 0
	end
	return self.runeInfo[id] or 0
end

function DungeonRuneData:updatePassRound(id, round)
	local cur = self.runeInfo[id]
	if not cur or cur > round then
		self.runeInfo[id] = round
		self:tagRoundNew()
	end
end

function DungeonRuneData:tagRoundNew()
	self.tagNew = true
end

function DungeonRuneData:getTagRoundNew()
	return self.tagNew
end

function DungeonRuneData:getChapterRewards(id)
	return self:getConfig(id).first_reward
end

function DungeonRuneData:getChapterSweepRewards(id)
	return self:getConfig(id).sweep_reward
end

function DungeonRuneData:isBossChapter(id)
	local cfg = self:getConfig(id)
	return not cfg.monster[2] -- 只有一个怪物的就是boss关卡
end

function DungeonRuneData:getChapterCondition(id)
	local cfg = self:getConfig(id)
	if not self.cacheChaperCondition then
		self.cacheChaperCondition = {}
	end

	if not self.cacheChaperCondition[id] then
		self.cacheChaperCondition[id] = {}
		table.insert(self.cacheChaperCondition[id], {0, cfg.round, 0})
		if cfg.requirement then
			for _, condition in ipairs(cfg.requirement) do
				table.insert(self.cacheChaperCondition[id], GFunc.getTable(condition))
			end
		end
	end

	return self.cacheChaperCondition[id]
end

function DungeonRuneData:getConditionIcon(taskInfo)
	local iconSprite
	local iconConst = GConst.DungeonRuneConst.TASK_ICON[taskInfo[1]]
	if type(iconConst) == "table" then
		iconSprite = iconConst[taskInfo[2]]
	else
		iconSprite = iconConst
	end

	return GConst.ATLAS_PATH.UI_DUNGEON_RUNE, iconSprite
end

function DungeonRuneData:getConditionDesc(taskInfo, taskNum)
	local taskType = taskInfo[1]
	local taskParams1 = taskInfo[2]
	local taskParams2 = taskInfo[3]
	if taskType == TASK_TYPE.PASS_ROUND then
		if taskNum and taskNum > 0 then
			taskParams1 = taskNum
		end
		return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_QUEST_1, taskParams1)
	elseif taskType == TASK_TYPE.ELIMINATION_ELEMENT then
		if taskNum and taskNum > 0 then
			local num = taskParams2 - taskNum
			if num > 0 then
				taskParams2 = num
			end
		end
		local desc = ModuleManager.HeroManager:getMatchTypeName(taskParams1, true)
		return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_QUEST_2, taskParams2, desc)
	elseif taskType == TASK_TYPE.BREAK_GRID_TYPE then
		if taskNum and taskNum > 0 then
			local num = taskParams2 - taskNum
			if num > 0 then
				taskParams2 = num
			end
		end
		return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_QUEST_3, taskParams2)
	elseif taskType == TASK_TYPE.KILL_MONSTER then
		if taskNum and taskNum > 0 then
			local num = taskParams2 - taskNum
			if num > 0 then
				taskParams2 = num
			end
		end
		return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_QUEST_4, taskParams2)
	end
end

function DungeonRuneData:getChapterMonsterSpine(id)
	local cfg = self:getConfig(id)
	local monsterId = cfg.monster[1]
	local info = ConfigManager:getConfig("monster")[monsterId]
	return info.model_id, info.model_ui
end

function DungeonRuneData:getChapterMonsterI18N(id)
	local cfg = self:getConfig(id)
	local monsterId = cfg.monster[1]
	local info = ConfigManager:getConfig("monster")[monsterId]
	local monsterBase = info.monster_base
	return I18N:getConfig("monster_base")[monsterBase]
end

function DungeonRuneData:getCurFightChapterId()
	return self.curFightChapterId or 1
end

function DungeonRuneData:setCurFightChapterId(chapterId)
	self.curFightChapterId = chapterId
end

function DungeonRuneData:getChapterFightCount(id)
	return self.fightCountMap[id] or 0
end

function DungeonRuneData:getSliverWingBuyCount()
	return self.buySilverCount or 0
end

function DungeonRuneData:addSliverWingBuyCount(count)
	self.buySilverCount = self.buySilverCount + count
end

function DungeonRuneData:getRemainSliverWingCount()
	if not self.todayLimitSliverCount then
		self.todayLimitSliverCount = GFunc.getConstIntValue("dungeon_rune_buylimit")
	end
	local count = self.todayLimitSliverCount - self:getSliverWingBuyCount()
	if count <= 0 then
		count = 0
	end
	return count
end

function DungeonRuneData:getBuySliverCost()
	if not self.todayBuySliverCost then
		self.todayBuySliverCost = GFunc.getConstReward("dungeon_rune_cost")
	end

	return self.todayBuySliverCost
end

return DungeonRuneData