package de.winniepat.citrus.utils;

import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.sound.SoundEvents;

public class ChatSound {

    public static void init() {
        ClientReceiveMessageEvents.CHAT.register((message, signedMessage, sender, params, receptionTimestamp) -> checkForMention(message.getString()));

        ClientReceiveMessageEvents.GAME.register((message, overlay) -> {
            if (!overlay) {
                checkForMention(message.getString());
            }
        });
    }

    private static void checkForMention(String message) {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client == null || client.getSession() == null) return;

        String myName = client.getSession().getUsername();
        String cleanMessage = message.replaceAll("§.", "");

        if (cleanMessage.toLowerCase().contains(myName.toLowerCase())) {
            playMentionSound();
        }
    }

    private static void playMentionSound() {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client != null && client.player != null) {
            client.player.playSound(
                    SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP,
                    0.67F,
                    1.0F
            );
        }
    }
}