package eu.mhsl.minenet.minigames.handler.global;

import eu.mhsl.minenet.minigames.message.Icon;
import eu.mhsl.minenet.minigames.message.type.ChatMessage;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.event.EventListener;
import net.minestom.server.event.player.PlayerChatEvent;
import org.jetbrains.annotations.NotNull;

public class ChatFormatHandler implements EventListener<PlayerChatEvent> {
    @Override
    public @NotNull Class<PlayerChatEvent> eventType() {
        return PlayerChatEvent.class;
    }

    @Override
    public @NotNull Result run(@NotNull PlayerChatEvent event) {
        NamedTextColor color = event.getPlayer().getPermissionLevel() > 0 ? NamedTextColor.AQUA : NamedTextColor.GRAY;
        event.setFormattedMessage(
            new ChatMessage(Icon.CHAT)
                .appendStatic(Component.text(event.getPlayer().getUsername(), color))
                .appendStatic(Component.text(" > ", NamedTextColor.DARK_GRAY))
                .appendStatic(event.getRawMessage())
                .build(event.getPlayer())
        );
        return Result.SUCCESS;
    }
}
