package land.chipmunk.chipmunkmod.modules.custom_chat;

import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.modules.Chat;
import land.chipmunk.chipmunkmod.modules.CommandCore;
import land.chipmunk.chipmunkmod.modules.KaboomCheck;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;

import java.util.Map;
import java.util.regex.Pattern;

public class CustomChat {
    public static final CustomChat INSTANCE = new CustomChat(MinecraftClient.getInstance());

    private static final GsonComponentSerializer GSON = GsonComponentSerializer.gson();
    private static final CustomChatComponentRenderer RENDERER = new CustomChatComponentRenderer();
    private static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer

            // https://sus.red/abc?a=b&c=d will still break the click events, though
            .legacyAmpersand()

            .toBuilder()

            .extractUrls(
                    Pattern
                            .compile("((https?://(ww(w|\\d)\\.)?|ww(w|\\d))[-a-zA-Z0-9@:%._+~#=]{1,256}"
                                             + "\\.[a-zA-Z0-9]{1,63}\\b([-a-zA-Z0-9@:%_+.~#?&/=]*))"),
                    Style.style(
                            NamedTextColor.BLUE,
                            TextDecoration.UNDERLINED,
                            HoverEvent.showText(
                                    Component
                                            .text("Click here to open the URL")
                                            .color(NamedTextColor.BLUE)
                            )
                    )
            )

            // > `this.useTerriblyStupidHexFormat = true`
            // i know it is stupid indeed,
            // but i also want the compatibility to the kaboom chat
            .useUnusualXRepeatedCharacterHexFormat() // &x&1&2&3&4&5&6abc

            .build();

    private final MinecraftClient client;
    public boolean enabled = true;

    public Component format = ChipmunkMod.CONFIG.customChat.format;

    public CustomChat (final MinecraftClient client) {
        this.client = client;
    }

    public void chat (final String message) {
        final ClientPlayerEntity player = client.player;
        if (player == null) return;
        if (!enabled || !player.hasPermissionLevel(2) || !player.isCreative()) {
            Chat.sendChatMessage(message, true);
            return;
        }

        final Component styledMessage = SERIALIZER.deserialize(message);

        final String username = MinecraftClient.getInstance().getSession().getUsername();

        final CustomChatContext context = new CustomChatContext(player.getUuidAsString(), styledMessage,
                                                                Map.of("MESSAGE", message, "USERNAME", username));
        final Component renderedFormat = RENDERER.render(format, context)
                .compact();
        final String json = GSON.serialize(renderedFormat);

        CommandCore.INSTANCE.run((KaboomCheck.INSTANCE.isKaboom ? "minecraft:tellraw @a " : "tellraw @a ") + json);
    }
}
