package eu.mhsl.minenet.minigames.util;

import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.item.Material;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Predicate;

public class MaterialUtil {
    public static Material fromString(String name, Material def) {
        return Material.values().stream().filter(material -> material.name().equals(name)).findFirst().orElse(def);
    }

    public static Material getRandomFullBlock(Predicate<Material> filter) {
        List<Material> blocks = Material.values().stream()
            .filter(filter)
            .filter(Material::isBlock)
            .filter(material -> material.block().isSolid())
            .filter(material -> Arrays.stream(BlockFace.values())
                .allMatch(face -> material.block().registry().collisionShape().isFaceFull(face))
            )
            .toList();

        return blocks.get(ThreadLocalRandom.current().nextInt(blocks.size()));
    }
}
