package com.firefrostgaming.rules;

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

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 = new TextComponent("");
        String[] lines = processedText.split("\n");
        for (String line : lines) {
            MutableComponent lineComponent;
            if (line.startsWith("**") && line.endsWith("**")) {
                String cleanLine = line.replace("**", "");
                lineComponent = new TextComponent(cleanLine);
                lineComponent.withStyle(headerColor, ChatFormatting.BOLD);
            } else if (line.trim().startsWith("-") || line.trim().startsWith("\u2022")) {
                lineComponent = new TextComponent("  " + line.trim());
                lineComponent.withStyle(bodyColor);
            } else {
                lineComponent = new TextComponent(line);
                lineComponent.withStyle(bodyColor);
            }
            rootComponent.append(lineComponent).append(new TextComponent("\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}]", "");
    }
}
