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

import com.google.gson.JsonParseException;
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.resources.ResourceLocation;
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 bhx, final float[] arr) {
        this.enchantment = bhx;
        this.values = arr;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(LootContextParams.TOOL);
    }
    
    @Override
    public boolean test(final LootContext crx) {
        final ItemStack ben3 = crx.<ItemStack>getParamOrNull(LootContextParams.TOOL);
        final int integer4 = (ben3 != null) ? EnchantmentHelper.getItemEnchantmentLevel(this.enchantment, ben3) : 0;
        final float float5 = this.values[Math.min(integer4, this.values.length - 1)];
        return crx.getRandom().nextFloat() < float5;
    }
    
    public static Builder bonusLevelFlatChance(final Enchantment bhx, final float... arr) {
        return () -> new BonusLevelTableCondition(bhx, arr);
    }
    
    public static class Serializer extends LootItemCondition.Serializer<BonusLevelTableCondition> {
        public Serializer() {
            super(new ResourceLocation("table_bonus"), BonusLevelTableCondition.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final BonusLevelTableCondition cud, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.addProperty("enchantment", Registry.ENCHANTMENT.getKey(cud.enchantment).toString());
            jsonObject.add("chances", jsonSerializationContext.serialize(cud.values));
        }
        
        @Override
        public BonusLevelTableCondition deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final ResourceLocation sm4 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "enchantment"));
            final Object o;
            final Object obj;
            final Enchantment bhx5 = Registry.ENCHANTMENT.getOptional(sm4).<Throwable>orElseThrow(() -> {
                new JsonParseException("Invalid enchantment id: " + obj);
                return o;
            });
            final float[] arr6 = GsonHelper.<float[]>getAsObject(jsonObject, "chances", jsonDeserializationContext, float[].class);
            return new BonusLevelTableCondition(bhx5, arr6, null);
        }
    }
}
