local ConfigManager = {
	configs = {}
}

local CONFIG_PATH = "app/config/"

function ConfigManager:_getConfig(configName)
	local config = require(CONFIG_PATH .. configName)
	self.configs[configName] = config
	return config
end

function ConfigManager:getConfig(configName)
	if CS.BF.BFMain.IsShenhe and configName == "mall_treasure" then
		return self:getMallTreasureConfig().data
	end
	local config = self.configs[configName]
	if config == nil then
		config = self:_getConfig(configName)
	end
	return config.data
end

function ConfigManager:getItemConfig()
	if self.itemCfg == nil then
		self.itemCfg = self:getConfig("item")
	end
	return self.itemCfg
end

if NOT_PUBLISH then
	ConfigManager.__getConfig = ConfigManager.getConfig
	function ConfigManager:getConfig(configName)
		if string.lower(configName) ~= configName then
			Logger.logFatal("ConfigManager:getConfig 传入的表名不能有大写 " .. configName)
		end
		return self:__getConfig(configName)
	end
end

function ConfigManager:reloadConfig(configName)
	self:clearConfigCache(configName)
	self.configs[configName] = nil
	self:getConfig(configName)
end

function ConfigManager:getConfigNum(configName)
	local config = self.configs[configName]
	if config == nil then
		config = self:_getConfig(configName)
	end
	return config.count
end

function ConfigManager:getConfigWithOtherKey(configName, keyName)
	local config = self.configs[configName]
	if config == nil then
		config = self:_getConfig(configName)
	end
	if config.keys == nil then
		return nil
	end
	return config.keys[keyName]
end

function ConfigManager:getMainKeyWithOtherKey(configName, keyName, keyValue)
	local config = self.configs[configName]
	if config == nil then
		config = self:_getConfig(configName)
	end
	if config.keys == nil then
		return nil
	end
	local kv = config.keys[keyName]
	if kv[keyValue] then
		return kv[keyValue].id
	end
	return nil
end

function ConfigManager:reloadAllConfig()
	for configName, v in pairs(self.configs) do
		self:reloadConfig(configName)
	end
	self:preLoadConfig()
end

function ConfigManager:preLoadConfig()
	local monsterBase = self:_getConfig("monster_base")
	self:clearConfigCache("monster_base")
	local baseData = monsterBase.data
	local monsterFullData = {}
	local count = 0
	local function handleMonsterGrow(name)
		local monsterGrowConfig = self:_getConfig(name)
		local growData = monsterGrowConfig.data
		for k, v in pairs(growData) do
			monsterFullData[k] = v
			local data = baseData[v.monster_base]
			if data then
				monsterFullData[k].model_id = data.model_id
				monsterFullData[k].hurt_num = data.hurt_num
				monsterFullData[k].body = data.body
				monsterFullData[k].model_ui = data.model_ui
			-- else
				-- Logger.logHighlight("not data monster_baseid = " .. v.monster_baseid)
			end
			count = count + 1
		end
		self:clearConfigCache(name)
	end
	handleMonsterGrow("monster_chapter")
	handleMonsterGrow("monster_daily_challenge")
	handleMonsterGrow("monster_dungeon_gold")
	handleMonsterGrow("monster_dungeon_shards")
	handleMonsterGrow("monster_dungeon_equip")
	handleMonsterGrow("monster_dungeon_armor")
	handleMonsterGrow("monster_activity")
	handleMonsterGrow("monster_dungeon_rune")

	self.configs["monster"] = {
		data = monsterFullData,
		count = count
	}

	if EDITOR_MODE then
		local realCount = 0
		for k, v in pairs(monsterFullData) do
			realCount = realCount + 1
		end
		if count ~= realCount then
			Logger.logFatal("same id in monster config")
		end
	end
end

function ConfigManager:getChapterConfig(id)
	if not id then
		return
	end
	local idx = math.ceil(id/5000)
	local cfg = ConfigManager:getConfig("chapter" .. idx)
	return cfg[id], cfg
end

function ConfigManager:clearConfigCache(configName)
	package.loaded[CONFIG_PATH .. configName] = nil
end

function ConfigManager:getMallTreasureConfig()
	local mallTreasure = {
		[2]={
			["recharge_id"]=1,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=30
				}
			},
			["limit"]=1
		},
		[3]={
			["recharge_id"]=2,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=60
				}
			},
			["limit"]=1
		},
		[4]={
			["recharge_id"]=3,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=90
				}
			},
			["limit"]=2
		},
		[5]={
			["recharge_id"]=4,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=120
				}
			},
			["limit"]=2
		},
		[6]={
			["recharge_id"]=5,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=150
				}
			},
			["limit"]=2
		},
		[7]={
			["recharge_id"]=6,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=180
				}
			},
			["limit"]=3
		},
		[8]={
			["recharge_id"]=7,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=210
				}
			},
			["limit"]=3
		},
		[9]={
			["recharge_id"]=8,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=240
				}
			},
			["limit"]=3
		},
		[10]={
			["recharge_id"]=9,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=270
				}
			},
			["limit"]=3
		},
		[11]={
			["recharge_id"]=10,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=300
				}
			},
			["limit"]=3
		},
		[12]={
			["recharge_id"]=11,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=360
				}
			},
			["limit"]=3
		},
		[13]={
			["recharge_id"]=12,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=450
				}
			},
			["limit"]=3
		},
		[14]={
			["recharge_id"]=13,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=600
				}
			},
			["limit"]=3
		},
		[15]={
			["recharge_id"]=14,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=750
				}
			},
			["limit"]=3
		},
		[16]={
			["recharge_id"]=15,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=900
				}
			},
			["limit"]=3
		},
		[17]={
			["recharge_id"]=16,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=1500
				}
			},
			["limit"]=3
		},
		[18]={
			["recharge_id"]=17,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=2100
				}
			},
			["limit"]=3
		},
		[19]={
			["recharge_id"]=18,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=3000
				}
			},
			["limit"]=3
		},
		[20]={
			["recharge_id"]=19,
			["reward"]={
				{
					["type"]=1,
					["id"]=2,
					["num"]=6000
				}
			},
			["limit"]=3
		}
	}
	local config = {
		data=mallTreasure,count=12
	}
	return config
end

return ConfigManager