
local E_LANGUAGE_CN = 0	-- 简体
local E_LANGUAGE_ARB = 1 -- 阿语
local E_LANGUAGE_EN = 2	-- 英语
local E_LANGUAGE_SP = 3	-- 西语
local E_LANGUAGE_FT = 4	--繁体
local E_LANGUAGE_UNKNOW = 6

local LanguageInfo = {
	["Info"] = {
		["Amount"] = 3,
	},
	["Language"] = {
		{
			["id"] 		= E_LANGUAGE_EN,
			["name"] 	= "STR_E_LANGUAGE_EN",
			["key"] 	= "En",	--语言简写
			-- c3光效后缀
			-- ["c3_effect"] = "",
			--原先ani配置
			["control"] = "ani/control.ani",
			["control1"] = "ani/control1.ani",	-- 皮肤1
			["control3"] = "ani/control3.ani",--皮肤2 ani
			-- ["magic"] 	= "ani/magic.ani",
			-- ["magic1"] = "ani/magic1.ani",
			["codepage"] = 936,
			-- 配置(手动Reload)
			["ini/fontsetting.ini"] 			= "ini/fontsetting.ini",				-- 原先fontsetting.ini
			["ini/cn_res.ini"] 				= "ini/cn_res.ini",				-- 原先cn_res.ini
			["ini/server_cn_res.ini"]		= "ini/server_cn_Res.ini",		-- 原先server_cn_res.ini
			["ini/strres.ini"] 				= "ini/strres.ini",				-- 原先StrRes.ini
			["ini/ustrres.ini"] 			= "ini/ustrres.ini",			-- UStrRes.ini
			["ini/region.ini"] 				= "ini/region.ini",
			["ini/magictype.ini"] 			= "ini/magictype.ini",
			["ini/magiceffect.ini"] 		= "ini/magiceffect.ini",
			["ini/monster.dat"] 			= "ini/monster.dat",			-- 原先怪物配置Monster.dat
			["ini/npc.ini"] 				= "ini/npc.ini",
			["ini/npcx.ini"] 				= "ini/npcx.ini",
			["ini/itemtype.dat"] 			= "ini/itemtype.dat",
			["ini/itemtypesub.dat"] 		= "ini/ItemtypeSub.dat",
			["ini/mapdestination.dat"] 		= "ini/MapDestination.dat",
			["ini/questinfo.ini"] 			= "ini/questinfo.ini",

			["ini/questinfo_nontrade.ini"]	= "ini/questinfo_nontrade.ini",
			["ini/iap.ini"]					= "ini/iap.ini",
			["ini/mapdestination.ini"]		= "ini/mapdestination.ini",
			["ini/monstersay.ini"]			= "ini/monstersay.ini",
			["ini/restrain.ini"]			= "ini/restrain.ini",
			["ini/gamemapex.ini"]			= "ini/gamemapex.ini",
			["ini/gamesocial.ini"]			= "ini/gamesocial.ini",
			["ini/gameannounce.ini"]		= "ini/gameannounce.ini",
			["ini/pkprotocol.ini"]			= "ini/pkprotocol.ini",
			["ini/Common.ini"]	= "ini/Common.ini",
			-- 自动reload的lua配置
			["Lua"] = {
				{
					["File"] = "ini/server_key.lua",
					["Section"] = "ini/server_key.lua",
					["Unload"] = "ServerKey_Unload",
				},
				-- {
					-- ["File"] = "ini/en/competitiveweek.lua",
					-- ["Section"] = "ini/competitiveweek.lua",
					-- ["Unload"] = "CompetitiveWeek_Unload",
				-- },
				-- {
					-- ["File"] = "ini/en/ZHWarRank.lua",
					-- ["Section"] = "ini/ZHWarRank.lua",
					-- ["Unload"] = "ZHWarRank_UnLoad",
				-- },
				-- {
					-- ["File"] = "ini/en/18ChangeGoal.lua",
					-- ["Section"] = "ini/18ChangeGoal.lua",
					-- ["Unload"] = "ChangeGoalGoal_Unload",
				-- },
				-- {
					-- ["File"] = "ini/en/questinfo.lua",
					-- ["Section"] = "ini/questinfo.lua",
					-- ["Unload"] = "Questinfo_Unload",
				-- },
				-- {
					-- ["File"] = "ini/en/Activity.lua",
					-- ["Section"] = "ini/Activity.lua",
					-- ["Unload"] = "Activity_Unload",
				-- },
				-- {
					-- ["File"] = "ini/en/BattlePassTask.lua",
					-- ["Section"] = "ini/BattlePassTask.lua",
					-- ["Unload"] = "BattlePassTask_Unload",
				-- },
				-- {
					-- ["File"] = "ini/en/BattlePK.lua",
					-- ["Section"] = "ini/BattlePK.lua",
					-- ["Unload"] = "BattlePK_Unload",
				-- },
				{
					["File"] = "ini/GouYu.lua",
					["Section"] = "ini/GouYu.lua",
					["Unload"] = "GouYu_UnLoad",
				},
				{
					["File"] = "ini/NewSynWar.lua",
					["Section"] = "ini/NewSynWar.lua",
					["Unload"] = "NewSynWar_Unload",
				},
				{
					["File"] = "ini/DaoistWords.lua",
					["Section"] = "ini/DaoistWords.lua",
					["Unload"] = "DaoistWords_Unload",
				},
				{
					["File"] = "ini/profile.lua",
					["Section"] = "ini/profile.lua",
					["Unload"] = "Profile_Cfg_Unload",
				},
				{
					["File"] = "ini/newguide.lua",
					["Section"] = "ini/newguide.lua",
					["Unload"] = "NewGuide_Unload",
				},
				
				
			},
			-- 自动Reload的Ini配置
			["Ini"] = {},
		},		
		{
			["id"] 		= E_LANGUAGE_SP,
			["name"] 	= "STR_E_LANGUAGE_SP",
			["key"] 	= "SP",	--语言简写
			-- c3光效后缀
			-- ["c3_effect"] = "",
			--原先ani配置
			["control"] = "ani/sp/control.ani",
			["control1"] = "ani/sp/control1.ani",	-- 皮肤1
			-- ["magic"] 	= "ani/magic.ani",
			-- ["magic1"] = "ani/magic1.ani",
			["codepage"] = 936,
			-- 配置(手动Reload)
			["ini/fontsetting.ini"] 			= "ini/sp/fontsetting.ini",				-- 原先fontsetting.ini
			["ini/cn_res.ini"] 				= "ini/sp/cn_res.ini",				-- 原先cn_res.ini
			["ini/server_cn_res.ini"]		= "ini/sp/server_cn_Res.ini",		-- 原先server_cn_res.ini
			["ini/strres.ini"] 				= "ini/sp/strres.ini",				-- 原先StrRes.ini
			["ini/ustrres.ini"] 			= "ini/sp/ustrres.ini",			-- UStrRes.ini
			["ini/region.ini"] 				= "ini/sp/region.ini",
			["ini/magictype.ini"] 			= "ini/sp/magictype.ini",
			["ini/magiceffect.ini"] 		= "ini/sp/magiceffect.ini",
			["ini/monster.dat"] 			= "ini/sp/monster.dat",			-- 原先怪物配置Monster.dat
			["ini/npc.ini"] 				= "ini/sp/npc.ini",
			["ini/npcx.ini"] 				= "ini/sp/npcx.ini",
			["ini/itemtype.dat"] 			= "ini/sp/itemtype.dat",
			["ini/itemtypesub.dat"] 		= "ini/sp/ItemtypeSub.dat",
			["ini/mapdestination.dat"] 		= "ini/sp/MapDestination.dat",
			["ini/questinfo.ini"] 			= "ini/sp/questinfo.ini",
			["ini/questinfo_nontrade.ini"]	= "ini/sp/questinfo_nontrade.ini",
			["ini/iap.ini"]					= "ini/sp/iap.ini",
			["ini/mapdestination.ini"]		= "ini/sp/mapdestination.ini",
			["ini/monstersay.ini"]			= "ini/sp/monstersay.ini",
			["ini/restrain.ini"]			= "ini/sp/restrain.ini",
			["ini/gamemapex.ini"]			= "ini/sp/gamemapex.ini",
			["ini/gamesocial.ini"]			= "ini/sp/gamesocial.ini",
			["ini/gameannounce.ini"]		= "ini/sp/gameannounce.ini",
			["ini/pkprotocol.ini"]			= "ini/sp/pkprotocol.ini",
			["ini/Common.ini"]	= "ini/Common.ini",
			-- 自动reload的lua配置
			["Lua"] = {
				{
					["File"] = "ini/sp/server_key.lua",
					["Section"] = "ini/server_key.lua",
					["Unload"] = "ServerKey_Unload",
				},
				-- {
					-- ["File"] = "ini/en/competitiveweek.lua",
					-- ["Section"] = "ini/competitiveweek.lua",
					-- ["Unload"] = "CompetitiveWeek_Unload",
				-- },
				-- {
					-- ["File"] = "ini/en/ZHWarRank.lua",
					-- ["Section"] = "ini/ZHWarRank.lua",
					-- ["Unload"] = "ZHWarRank_UnLoad",
				-- },
				-- {
					-- ["File"] = "ini/en/18ChangeGoal.lua",
					-- ["Section"] = "ini/18ChangeGoal.lua",
					-- ["Unload"] = "ChangeGoalGoal_Unload",
				-- },
				-- {
					-- ["File"] = "ini/en/questinfo.lua",
					-- ["Section"] = "ini/questinfo.lua",
					-- ["Unload"] = "Questinfo_Unload",
				-- },
				-- {
					-- ["File"] = "ini/en/Activity.lua",
					-- ["Section"] = "ini/Activity.lua",
					-- ["Unload"] = "Activity_Unload",
				-- },
				-- {
					-- ["File"] = "ini/en/BattlePassTask.lua",
					-- ["Section"] = "ini/BattlePassTask.lua",
					-- ["Unload"] = "BattlePassTask_Unload",
				-- },
				-- {
					-- ["File"] = "ini/en/BattlePK.lua",
					-- ["Section"] = "ini/BattlePK.lua",
					-- ["Unload"] = "BattlePK_Unload",
				-- },
				{
					["File"] = "ini/sp/GouYu.lua",
					["Section"] = "ini/GouYu.lua",
					["Unload"] = "GouYu_UnLoad",
				},
				{
					["File"] = "ini/sp/NewSynWar.lua",
					["Section"] = "ini/NewSynWar.lua",
					["Unload"] = "NewSynWar_Unload",
				},
				{
					["File"] = "ini/sp/DaoistWords.lua",
					["Section"] = "ini/DaoistWords.lua",
					["Unload"] = "DaoistWords_Unload",
				},
				{
					["File"] = "ini/sp/profile.lua",
					["Section"] = "ini/profile.lua",
					["Unload"] = "Profile_Cfg_Unload",
				},
				{
					["File"] = "ini/sp/newguide.lua",
					["Section"] = "ini/newguide.lua",
					["Unload"] = "NewGuide_Unload",
				},
				
				
				
			},
			-- 自动Reload的Ini配置
			["Ini"] = {},
		},		
		{
			["id"] 		= E_LANGUAGE_FT,
			["name"] 	= "STR_E_LANGUAGE_FT",
			["key"] 	= "ft",	--语言简写 c3光效后缀
			["control"] = "ani/ft/control.ani",--原先ani配置
			["control1"] = "ani/ft/control1.ani",--皮肤1 ani
			-- ["magic"] 	= "ani/magic.ani",
			-- ["magic1"] = "ani/magic1.ani",
			["codepage"] = 936,
			-- 配置(手动Reload)
			["ini/fontsetting.ini"] 			= "ini/ft/fontsetting.ini",				-- 原先fontsetting.ini
			["ini/cn_res.ini"] 				= "ini/ft/cn_res.ini",				-- 原先cn_res.ini
			["ini/server_cn_Res.ini"]		= "ini/ft/server_cn_Res.ini",		-- 原先server_cn_res.ini
			["ini/strres.ini"] 				= "ini/ft/strres.ini",				-- 原先StrRes.ini
			["ini/ustrres.ini"] 			= "ini/ft/ustrres.ini",			-- UStrRes.ini
			["ini/region.ini"] 				= "ini/ft/region.ini",
			["ini/magictype.ini"] 			= "ini/ft/magictype.ini",
			["ini/magiceffect.ini"] 			= "ini/ft/magiceffect.ini",
			["ini/monster.dat"] 			= "ini/ft/monster.dat",			-- 原先怪物配置Monster.dat
			["ini/npc.ini"] 				= "ini/ft/npc.ini",
			["ini/npcx.ini"] 				= "ini/ft/npcx.ini",
			["ini/itemtype"] 				= "ini/ft/itemtype",
			["ini/questinfo.ini"] 			= "ini/ft/questinfo.ini",
			["ini/questinfo_nontrade.ini"]	= "ini/ft/questinfo_nontrade.ini",
			["ini/iap.ini"]	=	"ini/ft/iap.ini",
			["ini/mapdestination.ini"]	= "ini/ft/mapdestination.ini",
			["ini/monstersay.ini"]	= "ini/ft/monstersay.ini",
			["ini/restrain.ini"]	= "ini/ft/restrain.ini",
			["ini/gamemapex.ini"]	= "ini/ft/gamemapex.ini",
			["ini/gamesocial.ini"]	= "ini/ft/gamesocial.ini",
			["ini/gameannounce.ini"]	= "ini/ft/gameannounce.ini",
			["ini/pkprotocol.ini"]	= "ini/ft/pkprotocol.ini",
			["ini/Common.ini"]	= "ini/Common.ini",
			-- 自动Reload的Lua配置
			["Lua"] = {
				{
					["File"] = "ini/ft/server_key.lua",
					["Section"] = "ini/server_key.lua",
					["Unload"] = "ServerKey_Unload",
				},
				{
					["File"] = "ini/ft/NewSynWar.lua",
					["Section"] = "ini/NewSynWar.lua",
					["Unload"] = "NewSynWar_Unload",
				},
				{
					["File"] = "ini/ft/DaoistWords.lua",
					["Section"] = "ini/DaoistWords.lua",
					["Unload"] = "DaoistWords_Unload",
				},
				{
					["File"] = "ini/ft/profile.lua",
					["Section"] = "ini/profile.lua",
					["Unload"] = "Profile_Cfg_Unload",
				},
				-- 引导配置
				{
					["File"] = "ini/ft/newguide.lua",
					["Section"] = "ini/newguide.lua",
					["Unload"] = "NewGuide_Unload",
				},
				-- {
					-- ["File"] = "ini/ZHWarRank.lua",
					-- ["Section"] = "ini/ZHWarRank.lua",
					-- ["Unload"] = "ZHWarRank_UnLoad",
				-- },
				{
					["File"] = "ini/ft/GouYu.lua",
					["Section"] = "ini/GouYu.lua",
					["Unload"] = "GouYu_UnLoad",
				},
				
			},
			-- 自动Reload的Ini配置
			["Ini"] = {},
		},

	}
}

-- 当前strLanguage仅为Android下的.请尽可能罗列出所有. 例如中文可能为zh, cn, zh_cn,
-- 提供给多语言用.返回的ID必须LanguageInfo有相应配置
local function GetLanguageID(strLanguage)
	local language = E_LANGUAGE_EN
	strLanguage = string.lower(strLanguage)
	-- 阿语
	if strLanguage == "ar" then
		language = E_LANGUAGE_ARB
	end
	
	-- 英语
	if strLanguage == "en" then
		language = E_LANGUAGE_EN
	end
	
	-- 西语
	if strLanguage == "es" then
		language = E_LANGUAGE_SP
	end
	
	-- 法语
	if strLanguage == "fr" then
		language = E_LANGUAGE_EN
	end

	-- 他加禄 
	-- tl_PH 为Tagalog语言 他加禄
	-- en-PH 为
	if strLanguage == "ta" or strLanguage == "tl_ph" or strLanguage == "en-ph" or strLanguage == "fil" or strLanguage == "tl" then
		language = E_LANGUAGE_TA
	end

	-- 中文
	if strLanguage == "cn" or strLanguage == "zh" or strLanguage == "zh_cn" then
		language = E_LANGUAGE_CN
	end
	
	-- 未知
	return language
end

function writefile(content, mode)
      mode = mode or "w+b"
	  local filename = "debug/language.log"
	  local path = filename --cc.FileUtils:getInstance():getWritablePath()..filename
      local file = io.open(path, mode)
      if file then
        if file:write(content) == nil then return false end
        io.close(file)
        return true
      else
        return false
      end
end

-- 提供给SDK收集用.返回的ID范围在E_LANGUAGE_**即可
local function GetLanguageParam(strLanguage)
	local language = E_LANGUAGE_UNKNOW
	strLanguage = string.lower(strLanguage)
	-- 阿语
	if strLanguage == "ar" then
		language = E_LANGUAGE_ARB
	end
	
	-- 英语
	if strLanguage == "en" then
		language = E_LANGUAGE_EN
	end
	
	-- 西语
	if strLanguage == "es" then
		language = E_LANGUAGE_SP
	end
	
	-- 法语
	if strLanguage == "fr" then
		language = E_LANGUAGE_EN
	end
	
	-- 他加禄
	if strLanguage == "ta" or strLanguage == "tl_ph" or strLanguage == "en-ph" or strLanguage == "fil" or strLanguage == "tl" then
		language = E_LANGUAGE_EN
	end

	-- 中文
	if strLanguage == "cn" or strLanguage == "zh" or strLanguage == "zh_cn" then
		language = E_LANGUAGE_CN
	end
	
	return string.format("\"language\":%d", language)
end

local function GetLanguageInfo(key, index, skin_id, default)

	if key == "Amount" then
		return #LanguageInfo["Language"]
	end
	
	
	key = string.lower(key)
	-- 换肤影响control.ani,magic.ani加载
	if (key == "control" or key == "magic") and skin_id > 0 then
		key = string.format("%s%d", key, skin_id)
	end
	local tInfo = nil
	
	if type(index) == "number" then
		if LanguageInfo["Language"] == nil then
			return default
		end
		tInfo = LanguageInfo["Language"][index+1]
	end
	
	if tInfo == nil or tInfo[key] == nil then
		return default
	end
	
	return tInfo[key]
end

local function GetLanguageInfoByID(key, language_id, skin_id, default)
	
	if LanguageInfo["Language"] == nil then
		return default
	end
	
	key = string.lower(key)
	-- 换肤影响control.ani,magic.ani加载
	if (key == "control" or key == "magic") and skin_id > 0 then
		key = string.format("%s%d", key, skin_id)
	end

	for i,v in ipairs(LanguageInfo["Language"]) do
		if v~= nil and v["id"] == language_id then
			if v[key] ~= nil then
				return v[key]
			end
			
			for j,k in ipairs(v["Lua"]) do
				if string.lower(k["Section"]) == key then
					return k["File"]
				end
			end
			
			return default
		end
	end
    
    return default
end

local function GetLoadLuaInfo(key, index, language_id, skin_id, default)
	
	if LanguageInfo["Language"] == nil then
		return default
	end

	if key == "Amount" then
		for i,v in ipairs(LanguageInfo["Language"]) do
			if v~= nil and v["id"] == language_id and v["Lua"] ~= nil then
				return #(v["Lua"])
			end
		end
		return 0
	end
	
	if type(index) == "number" then
		index = index + 1
	end
	
	for i,v in ipairs(LanguageInfo["Language"]) do
		if v~= nil and v["id"] == language_id then
			if v["Lua"] ~= nil and v["Lua"][index] ~= nil and v["Lua"][index][key] ~= nil then
				return v["Lua"][index][key]
			end
			return default
		end
	end
	
	return default
end

local function GetLanguageDefault()
	return E_LANGUAGE_EN
end

-- 清理变量释放内存
local function Unload()
	GetLanguageParam = nil
	GetLanguageID = nil
	GetLanguageInfoByID = nil
	GetLanguageInfo = nil
	GetLoadLuaInfo = nil
	GetLanguageDefault = nil
end

-- 通过require传给其他Lua脚本使用
local LanguageCfg = {
	GetLanguageDefault = GetLanguageDefault,
	GetLanguageParam = GetLanguageParam,
	GetLanguageID = GetLanguageID,
    GetLanguageInfo = GetLanguageInfo,
    GetLanguageInfoByID = GetLanguageInfoByID,
    GetLoadLuaInfo = GetLoadLuaInfo,
    Unload = Unload,
}

-- C++程序使用的global函数名
LanguageCfg_GetLanguageDefault = LanguageCfg.GetLanguageDefault
LanguageCfg_GetLanguageParam = LanguageCfg.GetLanguageParam
LanguageCfg_GetLanguageID = LanguageCfg.GetLanguageID
LanguageCfg_GetLanguageInfo = LanguageCfg.GetLanguageInfo
LanguageCfg_GetLanguageInfoByID = LanguageCfg.GetLanguageInfoByID
LanguageCfg_GetLoadLuaInfo = LanguageCfg.GetLoadLuaInfo
LanguageCfg_Unload = LanguageCfg.Unload

-- require返回值
return LanguageCfg