package net.blay09.mods.bmc.balyware.textcomponent;

import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.translation.I18n;

public abstract class TextComponentTransformer {

	private static final String PATTERN_ARGUMENT = "%[sdf]";

	public void begin(ITextComponent chatComponent) {}

	public String transformText(ITextComponent component, String text) {
		return text;
	}

	public ITextComponent transformStyle(ITextComponent component) {
		return component;
	}

	public void finish(ITextComponent chatComponent, ITextComponent transformedComponent) {}

	public final ITextComponent walkTextComponent(ITextComponent chatComponent) {
		begin(chatComponent);
		ITextComponent transformedComponent = walkTextComponentInternal(chatComponent);
		finish(chatComponent, transformedComponent);
		return transformedComponent;
	}

	private ITextComponent walkTextComponentInternal(ITextComponent chatComponent) {
		if(chatComponent instanceof TextComponentString) {
			return walkTextComponentString((TextComponentString) chatComponent);
		} else if(chatComponent instanceof TextComponentTranslation) {
			return walkTextComponentTranslation((TextComponentTranslation) chatComponent);
		}
		return null;
	}

	private ITextComponent walkTextComponentString(TextComponentString chatComponent) {
		String newText = transformText(chatComponent, chatComponent.getChatComponentText_TextValue());
		TextComponentString transformedComponent = new TextComponentString(newText);
		transformedComponent.setChatStyle(chatComponent.getChatStyle());
		transformStyle(transformedComponent);
		for(Object object : chatComponent.getSiblings()) {
			ITextComponent adjustedComponent = walkTextComponentInternal((ITextComponent) object);
			if(adjustedComponent != null) {
				transformedComponent.appendSibling(adjustedComponent);
			}
		}
		return transformedComponent;
	}

	private ITextComponent walkTextComponentTranslation(TextComponentTranslation chatComponent) {
		return walkTextComponentString(convertTranslationComponent(chatComponent));
	}

	public static TextComponentString convertTranslationComponent(TextComponentTranslation chatComponent) {
		Object[] args = chatComponent.getFormatArgs();
		String[] splitKey = I18n.translateToLocal(chatComponent.getKey()).split("(?<=" + PATTERN_ARGUMENT + ")|(?=" + PATTERN_ARGUMENT + ")");
		TextComponentString root = null;
		int currentArg = 0;
		for(String key : splitKey) {
			if(key.matches(PATTERN_ARGUMENT)) {
				if(root == null) {
					root = new TextComponentString("");
					root.setChatStyle(chatComponent.getChatStyle().createShallowCopy());
				}
				if(args.length > currentArg) {
					if(args[currentArg] instanceof TextComponentString) {
						root.appendSibling(((TextComponentString) args[currentArg]).createCopy());
					} else if(args[currentArg] instanceof TextComponentTranslation) {
						root.appendSibling(convertTranslationComponent((TextComponentTranslation) args[currentArg]));
					} else {
						root.appendText(args[currentArg] == null ? "null" : String.valueOf(args[currentArg]));
					}
					currentArg++;
				}
			} else {
				if(root == null) {
					root = new TextComponentString(key);
					root.setChatStyle(chatComponent.getChatStyle().createShallowCopy());
				} else {
					root.appendSibling(new TextComponentString(key));
				}
			}
		}
		return root;
	}
}

@@ -252,16 +252,81 @@
 .I18n;%0A%0A
+import java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0A
 public a
@@ -389,22 +389,23 @@
 c final 
-String
+Pattern
  PATTERN
@@ -420,16 +420,133 @@
 T = 
-%22%25%5Bsdf%5D%22
+Pattern.compile(%22(?:%25(%5B0-9%5D)%5C%5C$%5Bsdf%5D%7C%25%5Bsdf%5D)%22);%0A%09private static final Matcher MATCHER_ARGUMENT = PATTERN_ARGUMENT.matcher(%22%22)
 ;%0A%0A%09
@@ -2339,21 +2339,22 @@
 ;%0A%09%7D%0A%0A%09p
-ublic
+rivate
  static 
@@ -2724,30 +2724,50 @@
 %0A%09%09%09
-if(key.matches(PATT
+MATCHER_ARGUMENT.reset(key);%0A%09%09%09if(MATCH
 ER
-N
 _ARG
@@ -2771,16 +2771,25 @@
 ARGUMENT
+.matches(
 )) %7B%0A%09%09%09
@@ -2925,24 +2925,168 @@
 y());%0A%09%09%09%09%7D%0A
+%09%09%09%09int thisArg = currentArg;%0A%09%09%09%09if(MATCHER_ARGUMENT.group(1) != null) %7B%0A%09%09%09%09%09thisArg = Integer.parseInt(MATCHER_ARGUMENT.group(1)) - 1;%0A%09%09%09%09%7D%0A
 %09%09%09%09if(args.
@@ -3094,23 +3094,20 @@
 ength %3E 
-current
+this
 Arg) %7B%0A%09
@@ -3110,39 +3110,36 @@
  %7B%0A%09%09%09%09%09if(args%5B
-current
+this
 Arg%5D instanceof 
@@ -3210,31 +3210,28 @@
 tring) args%5B
-current
+this
 Arg%5D).create
@@ -3255,31 +3255,28 @@
 lse if(args%5B
-current
+this
 Arg%5D instanc
@@ -3388,31 +3388,28 @@
 ation) args%5B
-current
+this
 Arg%5D));%0A%09%09%09%09
@@ -3441,31 +3441,28 @@
 ndText(args%5B
-current
+this
 Arg%5D == null
@@ -3493,23 +3493,20 @@
 Of(args%5B
-current
+this
 Arg%5D));%0A
@@ -3508,24 +3508,58 @@
 %5D));%0A%09%09%09%09%09%7D%0A
+%09%09%09%09%09if(thisArg == currentArg) %7B%0A%09
 %09%09%09%09%09current
@@ -3565,16 +3565,23 @@
 tArg++;%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A%09%09

Fix death messages not being formatted properly.