package us.myles.ViaVersion.protocols.protocol1_13to1_12_2;

import com.google.gson.JsonElement;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import us.myles.ViaVersion.api.rewriters.ComponentRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ComponentRewriter1_13;
import us.myles.ViaVersion.util.GsonUtil;

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

public class ChatRewriter {
    private static final Pattern URL = Pattern.compile("^(?:(https?)://)?([-\\w_.]{2,}\\.[a-z]{2,4})(/\\S*)?$");
    private static final BaseComponent[] EMPTY_COMPONENTS = new BaseComponent[0];
    private static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter1_13();

    // Based on https://github.com/SpigotMC/BungeeCord/blob/master/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java
    public static JsonElement fromLegacyText(String message, ChatColor defaultColor) {
        List<BaseComponent> components = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        TextComponent component = new TextComponent();
        Matcher matcher = URL.matcher(message);

        for (int i = 0; i < message.length(); i++) {
            char c = message.charAt(i);
            if (c == ChatColor.COLOR_CHAR) {
                if (++i >= message.length()) {
                    break;
                }
                c = message.charAt(i);
                if (c >= 'A' && c <= 'Z') {
                    c += 32;
                }
                ChatColor format = ChatColor.getByChar(c);
                if (format == null) {
                    continue;
                }
                if (builder.length() > 0) {
                    TextComponent old = component;
                    component = new TextComponent(old);
                    old.setText(builder.toString());
                    builder = new StringBuilder();
                    components.add(old);
                }
                if (ChatColor.BOLD.equals(format)) {
                    component.setBold(true);
                } else if (ChatColor.ITALIC.equals(format)) {
                    component.setItalic(true);
                } else if (ChatColor.UNDERLINE.equals(format)) {
                    component.setUnderlined(true);
                } else if (ChatColor.STRIKETHROUGH.equals(format)) {
                    component.setStrikethrough(true);
                } else if (ChatColor.MAGIC.equals(format)) {
                    component.setObfuscated(true);
                } else if (ChatColor.RESET.equals(format)) {
                    format = defaultColor;

                    component = new TextComponent();
                    component.setColor(format);
                    // ViaVersion start - Items have style default to italic
                    component.setBold(false);
                    component.setItalic(false);
                    component.setUnderlined(false);
                    component.setStrikethrough(false);
                    component.setObfuscated(false);
                    // ViaVersion end
                } else {
                    component = new TextComponent();
                    component.setColor(format);
                    // ViaVersion start- Items have style default to italic
                    component.setBold(false);
                    component.setItalic(false);
                    component.setUnderlined(false);
                    component.setStrikethrough(false);
                    component.setObfuscated(false);
                    // ViaVersion end
                }
                continue;
            }
            int pos = message.indexOf(' ', i);
            if (pos == -1) {
                pos = message.length();
            }
            if (matcher.region(i, pos).find()) { //Web link handling

                if (builder.length() > 0) {
                    TextComponent old = component;
                    component = new TextComponent(old);
                    old.setText(builder.toString());
                    builder = new StringBuilder();
                    components.add(old);
                }

                TextComponent old = component;
                component = new TextComponent(old);
                String urlString = message.substring(i, pos);
                component.setText(urlString);
                component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL,
                        urlString.startsWith("http") ? urlString : "http://" + urlString));
                components.add(component);
                i += pos - i - 1;
                component = old;
                continue;
            }
            builder.append(c);
        }

        component.setText(builder.toString());
        components.add(component);

        final String serializedComponents = ComponentSerializer.toString(components.toArray(EMPTY_COMPONENTS));
        return GsonUtil.getJsonParser().parse(serializedComponents);
    }

    public static JsonElement legacyTextToJson(String legacyText) {
        return fromLegacyText(legacyText, ChatColor.WHITE);
    }

    public static String jsonTextToLegacy(String value) {
        return TextComponent.toLegacyText(ComponentSerializer.parse(value));
    }

    public static void processTranslate(JsonElement value) {
        COMPONENT_REWRITER.processText(value);
    }
}

@@ -652,121 +652,8 @@
 r %7B%0A
-    private static final Pattern URL = Pattern.compile(%22%5E(?:(https?)://)?(%5B-%5C%5Cw_.%5D%7B2,%7D%5C%5C.%5Ba-z%5D%7B2,4%7D)(/%5C%5CS*)?$%22);%0A
     
@@ -1212,56 +1212,8 @@
 t();
-%0A        Matcher matcher = URL.matcher(message);
 %0A%0A  
@@ -3716,1055 +3716,8 @@
   %7D%0A
-            int pos = message.indexOf(' ', i);%0A            if (pos == -1) %7B%0A                pos = message.length();%0A            %7D%0A            if (matcher.region(i, pos).find()) %7B //Web link handling%0A%0A                if (builder.length() %3E 0) %7B%0A                    TextComponent old = component;%0A                    component = new TextComponent(old);%0A                    old.setText(builder.toString());%0A                    builder = new StringBuilder();%0A                    components.add(old);%0A                %7D%0A%0A                TextComponent old = component;%0A                component = new TextComponent(old);%0A                String urlString = message.substring(i, pos);%0A                component.setText(urlString);%0A                component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL,%0A                        urlString.startsWith(%22http%22) ? urlString : %22http://%22 + urlString));%0A                components.add(component);%0A                i += pos - i - 1;%0A                component = old;%0A                continue;%0A            %7D%0A
     

Remove useless URL detection and event addition from 1.13->1.12.2 ChatRewriter