package com.firefrostgaming.rules;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;

public class DiscordFormatter {
    public static MutableComponent formatRules(String rawDiscordText) {
        String processedText = convertEmojis(rawDiscordText);
        String lowerText = processedText.toLowerCase();
        ChatFormatting headerColor = ChatFormatting.DARK_PURPLE;
        ChatFormatting bodyColor = ChatFormatting.LIGHT_PURPLE;
        if (lowerText.contains("fire") || lowerText.contains("[fire]")) {
            headerColor = ChatFormatting.GOLD;
            bodyColor = ChatFormatting.YELLOW;
        } else if (lowerText.contains("frost") || lowerText.contains("[frost]")) {
            headerColor = ChatFormatting.AQUA;
            bodyColor = ChatFormatting.DARK_AQUA;
        }
        MutableComponent rootComponent = Component.empty();
        String[] lines = processedText.split("\n");
        for (String line : lines) {
            MutableComponent lineComponent;
            if (line.startsWith("**") && line.endsWith("**")) {
                String cleanLine = line.replace("**", "");
                lineComponent = Component.literal(cleanLine).withStyle(headerColor, ChatFormatting.BOLD);
            } else if (line.trim().startsWith("-") || line.trim().startsWith("\u2022")) {
                lineComponent = Component.literal("  " + line.trim()).withStyle(bodyColor);
            } else {
                lineComponent = Component.literal(line).withStyle(bodyColor);
            }
            rootComponent.append(lineComponent).append(Component.literal("\n"));
        }
        return rootComponent;
    }

    private static String convertEmojis(String text) {
        if (text == null) return "";
        return text.replace("\uD83D\uDD25", "[Fire]").replace("\u2744\uFE0F", "[Frost]")
                   .replace("\uD83D\uDC9C", "[Arcane]").replaceAll("[\\x{1F300}-\\x{1F9FF}]", "");
    }
}
