using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;

public class Language
{
	
	private static Dictionary<string, string> lanDict;
	
	public static void init ()
	{
		lanDict = new Dictionary<string, string>();
	}
	
	public static string GetStr(string id)
	{
		if(lanDict == null) 
		{
			init ();
		}
		
		if(!lanDict.ContainsKey(id))
		{
			return "Topic missing id["+id+"]";
		}
		return lanDict[id];
	}








	private static SystemLanguage defaultLan = SystemLanguage.English;
	private static SystemLanguage currentLan;
	private static XmlDocument xml;
	
	public static void Init (SystemLanguage lan)
	{
		Debuger.Log("Init Language "+lan.ToString());

		currentLan = lan;
		TextAsset textAsset = (TextAsset)Resources.Load("XML/Language/"+lan.ToString());
		if(textAsset == null)
		{
			Debuger.LogError("Can not find language use default "+defaultLan);
			textAsset = (TextAsset)Resources.Load("XML/Language/"+defaultLan.ToString());
			currentLan = defaultLan;
		}
		
		if(xml == null)
			xml = new XmlDocument();
		xml.LoadXml(textAsset.text);
	}

	private static SystemLanguage GetSystemLanguage()
	{
		return SystemLanguage.English;
//		return SystemLanguage.Chinese;
	}

	public static SystemLanguage GetCurrentLanguage()
	{
		if(xml == null)
			Init (GetSystemLanguage());
		return currentLan;
	}
	
	public static string GetStr(string page, string id)
	{
		if(xml == null) 
		{
			Init (GetSystemLanguage());
		}
		
		try
		{
			XmlNode pageNode = xml.SelectSingleNode("lan/"+page);
			XmlNode node = pageNode.SelectSingleNode(id);
			if(node != null)
				return node.InnerText.Replace("\\n", "\n");
		}
		catch(Exception e)
		{
			Debuger.LogException(e);
		}
		
		string error = "Topic missing page["+page+"] id["+id+"]";
		Debuger.LogError(error);
		return error;
	}

}

