package com.yourname.premiumah.config;

import com.yourname.premiumah.util.TextUtil;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class MessageManager {
    private final JavaPlugin plugin;
    private final ConfigManager configManager;
    private File file;
    private YamlConfiguration messages;

    public MessageManager(JavaPlugin plugin, ConfigManager configManager) {
        this.plugin = plugin;
        this.configManager = configManager;
    }

    public void reload() {
        this.file = new File(plugin.getDataFolder(), "messages.yml");
        if (!file.exists()) {
            plugin.saveResource("messages.yml", false);
        }
        this.messages = YamlConfiguration.loadConfiguration(file);
    }

    public void send(CommandSender sender, String key) {
        send(sender, key, Collections.emptyMap());
    }

    public void send(CommandSender sender, String key, Map<String, String> placeholders) {
        sender.sendMessage(message(key, placeholders));
    }

    public void action(Player player, String key, Map<String, String> placeholders) {
        player.sendActionBar(message(key, placeholders));
    }

    public Component message(String key, Map<String, String> placeholders) {
        String raw = messages.getString("messages." + key, "<#ef4444>Missing message: " + key);
        return component(apply(raw, placeholders));
    }

    public List<Component> guiLore(String key, Map<String, String> placeholders) {
        return messages.getStringList("gui-lore." + key).stream()
                .map(line -> component(apply(line, placeholders)))
                .toList();
    }

    public List<String> rawGuiLore(String key) {
        return messages.getStringList("gui-lore." + key);
    }

    public Component component(String raw) {
        return TextUtil.component(apply(raw, Collections.emptyMap()));
    }

    public String legacy(String raw) {
        return TextUtil.legacy(apply(raw, Collections.emptyMap()));
    }

    public String legacyWithPlaceholders(String raw, Map<String, String> placeholders) {
        return TextUtil.legacy(apply(raw, placeholders));
    }

    public Map<String, String> placeholders(String... pairs) {
        Map<String, String> placeholders = new HashMap<>();
        for (int i = 0; i + 1 < pairs.length; i += 2) {
            placeholders.put(pairs[i], pairs[i + 1]);
        }
        return placeholders;
    }

    public String apply(String raw, Map<String, String> placeholders) {
        String result = raw == null ? "" : raw;
        result = result.replace("{prefix}", configManager.prefix());
        for (Map.Entry<String, String> entry : placeholders.entrySet()) {
            result = result.replace("{" + entry.getKey() + "}", entry.getValue());
        }
        return result;
    }
}
