package us.myles.ViaVersion.protocols.protocol1_12to1_11_1;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.rewriters.ComponentRewriter;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.data.AchievementTranslationMapping;

public class TranslateRewriter {

    private final static ComponentRewriter achievementTextRewriter = new ComponentRewriter() {
        @Override
        protected void handleTranslate(JsonObject object, String translate) {
            String text = AchievementTranslationMapping.get(translate);
            if (text != null) {
                object.addProperty("translate", text);
            }
        }

        @Override
        protected void handleHoverEvent(JsonObject hoverEvent) {
            String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
            if (!action.equals("show_achievement")) {
                super.handleHoverEvent(hoverEvent);
                return;
            }

            String value = hoverEvent.getAsJsonPrimitive("value").getAsString();
            if (AchievementTranslationMapping.get(value) == null) {
                JsonObject invalidText = new JsonObject();
                invalidText.addProperty("text", "Invalid statistic/achievement!");
                invalidText.addProperty("color", "red");
                hoverEvent.addProperty("action", "show_text");
                hoverEvent.add("value", invalidText);
                super.handleHoverEvent(hoverEvent);
                return;
            }

            try {
                JsonObject newLine = new JsonObject();
                newLine.addProperty("text", "\n");
                JsonArray baseArray = new JsonArray();
                baseArray.add("");
                JsonObject namePart = new JsonObject();
                JsonObject typePart = new JsonObject();
                baseArray.add(namePart);
                baseArray.add(newLine);
                baseArray.add(typePart);
                if (value.startsWith("achievement")) {
                    namePart.addProperty("translate", value);
                    namePart.addProperty("color", AchievementTranslationMapping.isSpecial(value) ? "dark_purple" : "green");
                    typePart.addProperty("translate", "stats.tooltip.type.achievement");
                    JsonObject description = new JsonObject();
                    typePart.addProperty("italic", true);
                    description.addProperty("translate", value + ".desc");
                    baseArray.add(newLine);
                    baseArray.add(description);
                } else if (value.startsWith("stat")) {
                    namePart.addProperty("translate", value);
                    namePart.addProperty("color", "gray");
                    typePart.addProperty("translate", "stats.tooltip.type.statistic");
                    typePart.addProperty("italic", true);
                }
                hoverEvent.addProperty("action", "show_text");
                hoverEvent.add("value", baseArray);
            } catch (Exception e) {
                Via.getPlatform().getLogger().warning("Error rewriting show_achievement: " + hoverEvent);
                e.printStackTrace();
                JsonObject invalidText = new JsonObject();
                invalidText.addProperty("text", "Invalid statistic/achievement!");
                invalidText.addProperty("color", "red");
                hoverEvent.addProperty("action", "show_text");
                hoverEvent.add("value", invalidText);
            }
            super.handleHoverEvent(hoverEvent);
        }
    };

    public static void toClient(JsonElement element, UserConnection user) {
        if (element instanceof JsonObject) {
            JsonObject obj = (JsonObject) element;
            JsonElement translate = obj.get("translate");
            if (translate != null) {
                if (translate.getAsString().startsWith("chat.type.achievement")) {
                    achievementTextRewriter.processText(obj);
                }
            }
        }
    }

}

@@ -1145,26 +1145,249 @@
 ing 
-value = hoverEvent
+textValue;%0A            JsonElement value = hoverEvent.get(%22value%22);%0A            if (value.isJsonObject()) %7B%0A                textValue = value.getAsJsonObject().get(%22text%22).getAsString();%0A            %7D else %7B%0A                textValue = value
 .get
@@ -1402,23 +1402,16 @@
 imitive(
-%22value%22
 ).getAsS
@@ -1411,32 +1411,47 @@
 .getAsString();%0A
+            %7D%0A%0A
             if (
@@ -1484,17 +1484,21 @@
 ing.get(
-v
+textV
 alue) ==
@@ -2377,25 +2377,29 @@
         if (
-v
+textV
 alue.startsW
@@ -2466,33 +2466,37 @@
 ty(%22translate%22, 
-v
+textV
 alue);%0A         
@@ -2576,17 +2576,21 @@
 Special(
-v
+textV
 alue) ? 
@@ -3015,25 +3015,29 @@
  %7D else if (
-v
+textV
 alue.startsW
@@ -3101,25 +3101,29 @@
 translate%22, 
-v
+textV
 alue);%0A     

Fix 1.11.1 -> 1.12 show_achievement translation (#2120)