package com.bitnix.dirtguilds.util;

import com.bitnix.dirtguilds.DirtGuildsPlugin;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.command.CommandSender;

public class MessageUtil {

    private static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage();

    private final DirtGuildsPlugin plugin;

    public MessageUtil(DirtGuildsPlugin plugin) {
        this.plugin = plugin;
    }

    public static Component mm(String text) {
        if (text == null) {
            text = "";
        }
        return MINI_MESSAGE.deserialize(text);
    }

    public Component parse(String text) {
        return mm(text);
    }

    public String raw(String path) {
        return plugin.getConfig().getString(path, "");
    }

    public String format(String path, String... replacements) {
        String text = raw(path);

        for (int i = 0; i + 1 < replacements.length; i += 2) {
            text = text.replace(replacements[i], replacements[i + 1]);
        }

        return raw("messages.prefix") + text;
    }

    public void send(CommandSender sender, String path, String... replacements) {
        sender.sendMessage(parse(format(path, replacements)));
    }

    public void sendRaw(CommandSender sender, String text) {
        sender.sendMessage(parse(text));
    }
}
