package eu.mhsl.minecraft.pixelblocks.utils;

import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

public class EntityTagUtil {
    public static void tag(@NotNull Entity entity, @NotNull NamespacedKey key, @NotNull UUID id) {
        entity.getPersistentDataContainer().set(key, PersistentDataType.STRING, id.toString());
    }

    public static <T extends Entity> @NotNull List<T> findTagged(@NotNull Location center, @NotNull Class<T> type, @NotNull NamespacedKey key, @NotNull UUID id) {
        String idString = id.toString();
        return center.getNearbyEntitiesByType(type, 1)
            .stream()
            .filter(entity -> Objects.equals(entity.getPersistentDataContainer().get(key, PersistentDataType.STRING), idString))
            .toList();
    }

    public static <T extends Entity> void removeTagged(@NotNull Location center, @NotNull Class<T> type, @NotNull NamespacedKey key, @NotNull UUID id) {
        findTagged(center, type, key, id).forEach(Entity::remove);
    }
}
