package net.minecraft.world.level;

import java.util.function.Consumer;
import javax.annotation.Nullable;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.component.CustomData;

public interface Spawner {
    void setEntityId(EntityType<?> entityType, RandomSource random);

    static void appendHoverText(CustomData customData, Consumer<Component> tooltipAdder, String spawnDataKey) {
        Component spawnEntityDisplayName = getSpawnEntityDisplayName(customData, spawnDataKey);
        if (spawnEntityDisplayName != null) {
            tooltipAdder.accept(spawnEntityDisplayName);
        } else {
            tooltipAdder.accept(CommonComponents.EMPTY);
            tooltipAdder.accept(Component.translatable("block.minecraft.spawner.desc1").withStyle(ChatFormatting.GRAY));
            tooltipAdder.accept(CommonComponents.space().append(Component.translatable("block.minecraft.spawner.desc2").withStyle(ChatFormatting.BLUE)));
        }
    }

    @Nullable
    static Component getSpawnEntityDisplayName(CustomData customData, String spawnDataKey) {
        return customData.getUnsafe()
            .getCompound(spawnDataKey)
            .flatMap(compoundTag -> compoundTag.getCompound("entity"))
            .flatMap(compoundTag -> compoundTag.read("id", EntityType.CODEC))
            .map(entityType -> Component.translatable(entityType.getDescriptionId()).withStyle(ChatFormatting.GRAY))
            .orElse(null);
    }
}
