package de.viper.survivalplus.commands;

import de.viper.survivalplus.Manager.BlockManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import java.util.stream.Collectors;

public class BlockListCommand implements CommandExecutor {

    private final BlockManager blockManager;
    private final FileConfiguration config;

    public BlockListCommand(BlockManager blockManager, FileConfiguration config) {
        this.blockManager = blockManager;
        this.config = config;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

        if (!(sender instanceof Player player)) {
            // Fallback: "Nur Spieler..." falls Key fehlt
            sender.sendMessage(color(config.getString("messages.general.only_players", "§cNur Spieler können diesen Befehl ausführen!")));
            return true;
        }

        var blocked = blockManager.getBlockedPlayers(player);
        if (blocked.isEmpty()) {
            player.sendMessage(color(config.getString("messages.blocklist.no_blocked_players", "§cDu hast keine Spieler blockiert.")));
            return true;
        }

        String list = blocked.stream()
                .map(Bukkit::getOfflinePlayer)
                .map(p -> p.getName() != null ? p.getName() : "§eUnbekannt")
                .collect(Collectors.joining(", "));

        // WICHTIG: Default-String als zweiten Parameter, um NullPointerException zu vermeiden
        String rawMessage = config.getString("messages.blocklist.blocked_players", "&eBlockierte Spieler: %list%");
        player.sendMessage(color(rawMessage.replace("%list%", list)));
        return true;
    }
    
    private String color(String msg) {
        if (msg == null) return "";
        return ChatColor.translateAlternateColorCodes('&', msg);
    }
}