package protocolsupport.protocol.serializer.chat;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.gson.JsonObject;

import protocolsupport.api.TranslationAPI;
import protocolsupport.api.chat.components.BaseComponent;
import protocolsupport.api.chat.components.TextComponent;
import protocolsupport.api.chat.components.TranslateComponent;
import protocolsupport.protocol.utils.json.SimpleJsonTreeSerializer;

public class ServerTranslateTranslateComponentContentSerializer implements ComponentContentSerializer<TranslateComponent> {

	public static final ServerTranslateTranslateComponentContentSerializer INSTANCE = new ServerTranslateTranslateComponentContentSerializer();

	protected static final Pattern langFormat = Pattern.compile("\\%(\\d?)\\$?([%s])");

	@Override
	public void serialize(SimpleJsonTreeSerializer<String> serializer, JsonObject json, TranslateComponent component, String locale) {
		TextComponent translatedComponent = new TextComponent("");
		String translation = TranslationAPI.getTranslationString(locale, component.getTranslationKey());
		List<BaseComponent> translationArgs = component.getTranslationArgs();
		Matcher matcher = langFormat.matcher(translation);
		int matchEndIndex = 0;
		int translationArgIndex = 0;
		while (matcher.find()) {
			translatedComponent.addSibling(new TextComponent(translation.substring(matchEndIndex, matcher.start())));
			matchEndIndex = matcher.end();
			String group2 = matcher.group(2);
			if (group2.equals("%")) {
				translatedComponent.addSibling(new TextComponent("%"));
			} else if (group2.equals("s")) {
				int lTranslationArgIndex = translationArgIndex;
				String group1 = matcher.group(1);
				if (!group1.isEmpty()) {
					try {
						lTranslationArgIndex = Integer.parseInt(group1) - 1;
					} catch (NumberFormatException e) {
						lTranslationArgIndex = -1;
					}
				}
				if ((lTranslationArgIndex >= 0) && (lTranslationArgIndex < translationArgs.size())) {
					translatedComponent.addSibling(translationArgs.get(lTranslationArgIndex));
				}
			}
			translationArgIndex++;
		}
		if (matchEndIndex < translation.length()) {
			translatedComponent.addSibling(new TextComponent(translation.substring(matchEndIndex)));
		}
		ComponentSerializer.serializeAndAdd(serializer, json, translatedComponent, locale);
	}

}

@@ -2258,16 +2258,76 @@
 ));%0A%09%09%7D%0A
+%09%09translatedComponent.addSiblings(component.getSiblings());%0A
 %09%09Compon

Fix translating translate component with extra components