package net.silentchaos512.lib.util;

import java.util.List;

import com.google.common.collect.Lists;

import net.minecraft.client.resources.I18n;
import net.minecraft.util.text.translation.LanguageMap;

public class LocalizationHelper {

  public final String modId;
  private boolean replacesAmpersandWithSectionSign = true;

  /**
   * Constructor. The mod ID is converted to lower case.
   * 
   * @param modId
   */
  public LocalizationHelper(String modId) {

    this.modId = modId.toLowerCase();
  }

  /**
   * Sets whether or not to replace ampersands with section signs. This allows formatting codes to easily be used in
   * localization files.
   */
  public LocalizationHelper setReplaceAmpersand(boolean value) {

    replacesAmpersandWithSectionSign = value;
    return this;
  }

  // ===============
  // General methods
  // ===============

  public String getLocalizedString(String key) {

    String str = I18n.format(key).trim();
    if (replacesAmpersandWithSectionSign) {
      str = str.replaceAll("&", "\u00a7");
    }
    return str;
  }

  public String getLocalizedString(String prefix, String key) {

    return getLocalizedString(prefix + "." + modId + ":" + key);
  }

  // ===============
  // Special methods
  // ===============

  public String getMiscText(String key) {

    return getLocalizedString("misc", key);
  }

  public String getWikiText(String key) {

    return getLocalizedString("wiki", key);
  }

  public String getBlockSubText(String blockName, String key) {

    return getLocalizedString("tile", blockName +"." + key);
  }

  public String getItemSubText(String itemName, String key) {

    return getLocalizedString("item", itemName + "." + key);
  }

  // =================
  // Description lines
  // =================

  public List<String> getBlockDescriptionLines(String blockName) {

    return getDescriptionLines("tile." + modId + ":" + blockName + ".desc");
  }

  public List<String> getItemDescriptionLines(String itemName) {

    return getDescriptionLines("item." + modId + ":" + itemName + ".desc");
  }

  public List<String> getDescriptionLines(String key) {

    List<String> list = Lists.newArrayList();
    int i = 1;
    String line = getLocalizedString(key + i);
    while (!line.equals(key + i)) {
      list.add(line);
      line = getLocalizedString(key + ++i);
    }

    if (list.isEmpty()) {
      line = getLocalizedString(key);
      if (!line.equals(key)) {
        list.add(line);
      }
    }

    return list;
  }
}

@@ -319,16 +319,60 @@
  = true;
+%0A  private boolean hideFormatErrors = false;
 %0A%0A  /**%0A
@@ -830,24 +830,142 @@
  this;%0A  %7D%0A%0A
+  public LocalizationHelper setHideFormatErrors(boolean value) %7B%0A%0A    hideFormatErrors = value;%0A    return this;%0A  %7D%0A%0A
   // =======
@@ -1053,32 +1053,54 @@
 tring(String key
+, Object... parameters
 ) %7B%0A%0A    String 
@@ -1120,16 +1120,28 @@
 rmat(key
+, parameters
 ).trim()
@@ -1138,24 +1138,25 @@
 rs).trim();%0A
+%0A
     if (repl
@@ -1184,18 +1184,16 @@
 ionSign)
- %7B
 %0A      s
@@ -1228,25 +1228,98 @@
 00a7%22);%0A    
-%7D
+if (hideFormatErrors)%0A      str = str.replaceFirst(%22Format error: %22, %22%22);%0A
 %0A    return 
@@ -1380,32 +1380,54 @@
 efix, String key
+, Object... parameters
 ) %7B%0A%0A    return 
@@ -1473,24 +1473,36 @@
  + %22:%22 + key
+, parameters
 );%0A  %7D%0A%0A  //
@@ -1591,32 +1591,54 @@
 cText(String key
+, Object... parameters
 ) %7B%0A%0A    return 
@@ -1663,24 +1663,36 @@
 (%22misc%22, key
+, parameters
 );%0A  %7D%0A%0A  pu
@@ -1717,32 +1717,54 @@
 iText(String key
+, Object... parameters
 ) %7B%0A%0A    return 
@@ -1789,24 +1789,36 @@
 (%22wiki%22, key
+, parameters
 );%0A  %7D%0A%0A  pu
@@ -1865,32 +1865,54 @@
 Name, String key
+, Object... parameters
 ) %7B%0A%0A    return 
@@ -1950,32 +1950,44 @@
 kName +%22.%22 + key
+, parameters
 );%0A  %7D%0A%0A  public
@@ -2028,32 +2028,54 @@
 Name, String key
+, Object... parameters
 ) %7B%0A%0A    return 
@@ -2121,16 +2121,28 @@
 .%22 + key
+, parameters
 );%0A  %7D%0A%0A
@@ -2561,24 +2561,106 @@
 ing key) %7B%0A%0A
+    boolean oldHideFormatErrors = hideFormatErrors;%0A    hideFormatErrors = true;%0A%0A
     List%3CStr
@@ -2994,24 +2994,69 @@
    %7D%0A    %7D%0A%0A
+    hideFormatErrors = oldHideFormatErrors;%0A%0A
     return l

Update LocalizationHelper for non-deprecated I18n.