package eu.mhsl.minenet.minigames.server.tasks;

import eu.mhsl.minenet.minigames.util.Monitoring;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer;
import net.minestom.server.adventure.audience.Audiences;
import net.minestom.server.entity.Player;

import java.util.Collection;

public class TablistUpdateTask implements Runnable {
    @Override
    public void run() {
        Collection<Player> players = MinecraftServer.getConnectionManager().getOnlinePlayers();
        if (players.isEmpty()) return;

        final Component header =
                Component.newline()
                        .append(Component.text("MineNet Network", NamedTextColor.GOLD))
                        .append(Component.newline()).append(Component.text("Players: " + players.size()))
                        .append(Component.newline())
                        .append(Component.newline()).append(Component.text("RAM: " + Monitoring.getRamUsage() + " MB", NamedTextColor.GRAY))
                        .append(Component.newline()).append(Component.text("TICK: " + Monitoring.getTickMonitor().getTickTime() + "ms", NamedTextColor.GRAY))
                        .append(Component.newline());

        final Component footer =
                Component.newline()
                        .append(Component.text("mhsl.eu"))
                        .append(Component.newline());

        Audiences.players().sendPlayerListHeaderAndFooter(header, footer);
    }
}
