package com.firefrostgaming.rules;

import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.IFormattableTextComponent;

public class DiscordFormatter {
    public static IFormattableTextComponent formatRules(String rawDiscordText) {
        String processedText = convertEmojis(rawDiscordText);
        String lowerText = processedText.toLowerCase();
        TextFormatting headerColor = TextFormatting.DARK_PURPLE;
        TextFormatting bodyColor = TextFormatting.LIGHT_PURPLE;
        if (lowerText.contains("fire") || lowerText.contains("[fire]")) {
            headerColor = TextFormatting.GOLD;
            bodyColor = TextFormatting.YELLOW;
        } else if (lowerText.contains("frost") || lowerText.contains("[frost]")) {
            headerColor = TextFormatting.AQUA;
            bodyColor = TextFormatting.DARK_AQUA;
        }
        IFormattableTextComponent rootComponent = new StringTextComponent("");
        String[] lines = processedText.split("\n");
        for (String line : lines) {
            IFormattableTextComponent lineComponent;
            if (line.startsWith("**") && line.endsWith("**")) {
                String cleanLine = line.replace("**", "");
                lineComponent = new StringTextComponent(cleanLine);
                lineComponent.withStyle(headerColor, TextFormatting.BOLD);
            } else if (line.trim().startsWith("-") || line.trim().startsWith("\u2022")) {
                lineComponent = new StringTextComponent("  " + line.trim());
                lineComponent.withStyle(bodyColor);
            } else {
                lineComponent = new StringTextComponent(line);
                lineComponent.withStyle(bodyColor);
            }
            rootComponent.append(lineComponent).append(new StringTextComponent("\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]");
    }
}
