package net.minecraft.world.level.storage.loot.predicates;

import com.google.gson.JsonParseException;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import net.minecraft.core.Registry;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.world.level.storage.loot.Serializer;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.item.ItemStack;
import com.google.common.collect.ImmutableSet;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParam;
import java.util.Set;
import net.minecraft.world.item.enchantment.Enchantment;

public class BonusLevelTableCondition implements LootItemCondition {
    private final Enchantment enchantment;
    private final float[] values;
    
    private BonusLevelTableCondition(final Enchantment bnw, final float[] arr) {
        this.enchantment = bnw;
        this.values = arr;
    }
    
    @Override
    public LootItemConditionType getType() {
        return LootItemConditions.TABLE_BONUS;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(LootContextParams.TOOL);
    }
    
    @Override
    public boolean test(final LootContext dat) {
        final ItemStack bki3 = dat.<ItemStack>getParamOrNull(LootContextParams.TOOL);
        final int integer4 = (bki3 != null) ? EnchantmentHelper.getItemEnchantmentLevel(this.enchantment, bki3) : 0;
        final float float5 = this.values[Math.min(integer4, this.values.length - 1)];
        return dat.getRandom().nextFloat() < float5;
    }
    
    public static Builder bonusLevelFlatChance(final Enchantment bnw, final float... arr) {
        return () -> new BonusLevelTableCondition(bnw, arr);
    }
    
    public static class Serializer implements net.minecraft.world.level.storage.loot.Serializer<BonusLevelTableCondition> {
        @Override
        public void serialize(final JsonObject jsonObject, final BonusLevelTableCondition ddd, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.addProperty("enchantment", Registry.ENCHANTMENT.getKey(ddd.enchantment).toString());
            jsonObject.add("chances", jsonSerializationContext.serialize(ddd.values));
        }
        
        @Override
        public BonusLevelTableCondition deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final ResourceLocation uh4 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "enchantment"));
            final Object o;
            final Object obj;
            final Enchantment bnw5 = Registry.ENCHANTMENT.getOptional(uh4).<Throwable>orElseThrow(() -> {
                new JsonParseException("Invalid enchantment id: " + obj);
                return o;
            });
            final float[] arr6 = GsonHelper.<float[]>getAsObject(jsonObject, "chances", jsonDeserializationContext, float[].class);
            return new BonusLevelTableCondition(bnw5, arr6, null);
        }
    }
}
