package cpw.mods.fml.common.registry;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import net.minecraft.src.Block;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.StringTranslate;

public class LanguageRegistry
{
    private static final LanguageRegistry INSTANCE = new LanguageRegistry();

    private Map<String,Properties> modLanguageData=new HashMap<String,Properties>();

    public static LanguageRegistry instance()
    {
        return INSTANCE;
    }

    public String getStringLocalization(String key)
    {
        return getStringLocalization(key, StringTranslate.getInstance().getCurrentLanguage());
    }
    
    public String getStringLocalization(String key, String lang)
    {
        String localizedString = "";
        Properties langPack = modLanguageData.get(lang);
        
        if (langPack != null) {
            if (langPack.getProperty(key) != null) {
                localizedString = langPack.getProperty(key); 
            }
        }
        
        return localizedString;
    }
    
    public void addStringLocalization(String key, String value)
    {
        addStringLocalization(key, "en_US", value);
    }
    public void addStringLocalization(String key, String lang, String value)
    {
        Properties langPack=modLanguageData.get(lang);
        if (langPack==null) {
            langPack=new Properties();
            modLanguageData.put(lang, langPack);
        }
        langPack.put(key,value);
    }
    
    public void addStringLocalization(Properties langPackAdditions) {
        addStringLocalization(langPackAdditions, "en_US");
    }
    
    public void addStringLocalization(Properties langPackAdditions, String lang) {
        Properties langPack = modLanguageData.get(lang);
        if (langPack == null) {
            langPack = new Properties();
            modLanguageData.put(lang, langPack);
        }
        if (langPackAdditions != null) {
            langPack.putAll(langPackAdditions);
        }
    }

    public static void reloadLanguageTable()
    {
        // reload language table by forcing lang to null and reloading the properties file
        String lang = StringTranslate.getInstance().getCurrentLanguage();
        StringTranslate.getInstance().currentLanguage = null;
        StringTranslate.getInstance().setLanguage(lang);
    }


    public void addNameForObject(Object objectToName, String lang, String name)
    {
        String objectName;
        if (objectToName instanceof Item) {
            objectName=((Item)objectToName).getItemName();
        } else if (objectToName instanceof Block) {
            objectName=((Block)objectToName).getBlockName();
        } else if (objectToName instanceof ItemStack) {
            objectName=((ItemStack)objectToName).getItem().getItemNameIS((ItemStack)objectToName);
        } else {
            throw new IllegalArgumentException(String.format("Illegal object for naming %s",objectToName));
        }
        objectName+=".name";
        addStringLocalization(objectName, lang, name);
    }
    
    public static void addName(Object objectToName, String name)
    {
        instance().addNameForObject(objectToName, "en_US", name);
    }

    public void loadLanguageTable(Properties languagePack, String lang)
    {
        Properties usPack=modLanguageData.get("en_US");
        if (usPack!=null) {
            languagePack.putAll(usPack);
        }
        Properties langPack=modLanguageData.get(lang);
        if (langPack==null) {
            return;
        }
        languagePack.putAll(langPack);
    }
    
    public void loadLocalization(String localizationFile, String lang, boolean isXML)
    {
        loadLocalization(this.getClass().getResource(localizationFile), lang, isXML);
    }
    
    public void loadLocalization(URL localizationFile, String lang, boolean isXML)
    {
        InputStream langStream = null;
        Properties langPack = new Properties();
        
        try    {
            langStream = localizationFile.openStream();
            
            if (isXML) {
                langPack.loadFromXML(langStream);
            }
            else {
                langPack.load(langStream);
            }
            
            addStringLocalization(langPack, lang);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally    {
            try    {
                if (langStream != null)    {
                    langStream.close();
                }
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

@@ -729,40 +729,35 @@
 ate.
-getInstance().getCurrentLanguage
+func_74808_a().func_74811_c
 ());
@@ -755,36 +755,32 @@
 811_c());%0A    %7D%0A
-    
 %0A    public Stri
@@ -929,24 +929,16 @@
 (lang);%0A
-        
 %0A       
@@ -1075,17 +1075,16 @@
 ty(key);
- 
 %0A       
@@ -1100,24 +1100,16 @@
       %7D%0A
-        
 %0A       
@@ -1131,36 +1131,32 @@
 edString;%0A    %7D%0A
-    
 %0A    public void
@@ -1565,36 +1565,32 @@
 y,value);%0A    %7D%0A
-    
 %0A    public void
@@ -1701,36 +1701,32 @@
 %22en_US%22);%0A    %7D%0A
-    
 %0A    public void
@@ -2272,97 +2272,91 @@
 ate.
-getInstance().getCurrentLanguage();%0A        StringTranslate.getInstance().currentLanguage
+func_74808_a().func_74811_c();%0A        StringTranslate.func_74808_a().field_74813_d
  = n
@@ -2388,33 +2388,35 @@
 ate.
-getInstance().setLanguage
+func_74808_a().func_74810_a
 (lan
@@ -2628,27 +2628,28 @@
 ToName).
-getItemName
+func_77658_a
 ();%0A    
@@ -2745,20 +2745,20 @@
 me).
-getBlockName
+func_71917_a
 ();%0A
@@ -2866,31 +2866,35 @@
 me).
-getItem().getItemNameIS
+func_77973_b().func_77667_c
 ((It
@@ -3133,36 +3133,32 @@
 g, name);%0A    %7D%0A
-    
 %0A    public stat
@@ -3651,36 +3651,32 @@
 angPack);%0A    %7D%0A
-    
 %0A    public void
@@ -3844,20 +3844,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -4021,24 +4021,16 @@
 ties();%0A
-        
 %0A       
@@ -4095,28 +4095,16 @@
 ream();%0A
-            
 %0A       
@@ -4261,28 +4261,16 @@
       %7D%0A
-            
 %0A       

Fix language registry additions, closes #113