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

import com.google.gson.JsonSyntaxException;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import net.minecraft.resources.ResourceLocation;
import java.util.Iterator;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import net.minecraft.core.Registry;
import com.google.gson.JsonArray;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import com.google.common.collect.Maps;
import java.util.Random;
import net.minecraft.world.item.SuspiciousStewItem;
import com.google.common.collect.Iterables;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.item.ItemStack;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.RandomValueBounds;
import net.minecraft.world.effect.MobEffect;
import java.util.Map;

public class SetStewEffectFunction extends LootItemConditionalFunction {
    private final Map<MobEffect, RandomValueBounds> effectDurationMap;
    
    private SetStewEffectFunction(final LootItemCondition[] arr, final Map<MobEffect, RandomValueBounds> map) {
        super(arr);
        this.effectDurationMap = ImmutableMap.copyOf(map);
    }
    
    @Override
    public LootItemFunctionType getType() {
        return LootItemFunctions.SET_STEW_EFFECT;
    }
    
    public ItemStack run(final ItemStack bki, final LootContext dat) {
        if (bki.getItem() != Items.SUSPICIOUS_STEW || this.effectDurationMap.isEmpty()) {
            return bki;
        }
        final Random random4 = dat.getRandom();
        final int integer5 = random4.nextInt(this.effectDurationMap.size());
        final Map.Entry<MobEffect, RandomValueBounds> entry6 = Iterables.<Map.Entry<MobEffect, RandomValueBounds>>get(this.effectDurationMap.entrySet(), integer5);
        final MobEffect aoe7 = entry6.getKey();
        int integer6 = entry6.getValue().getInt(random4);
        if (!aoe7.isInstantenous()) {
            integer6 *= 20;
        }
        SuspiciousStewItem.saveMobEffect(bki, aoe7, integer6);
        return bki;
    }
    
    public static Builder stewEffect() {
        return new Builder();
    }
    
    public static class Builder extends LootItemConditionalFunction.Builder<Builder> {
        private final Map<MobEffect, RandomValueBounds> effectDurationMap;
        
        public Builder() {
            this.effectDurationMap = Maps.newHashMap();
        }
        
        @Override
        protected Builder getThis() {
            return this;
        }
        
        public Builder withEffect(final MobEffect aoe, final RandomValueBounds dbb) {
            this.effectDurationMap.put(aoe, dbb);
            return this;
        }
        
        @Override
        public LootItemFunction build() {
            return new SetStewEffectFunction(this.getConditions(), this.effectDurationMap, null);
        }
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<SetStewEffectFunction> {
        @Override
        public void serialize(final JsonObject jsonObject, final SetStewEffectFunction dct, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, dct, jsonSerializationContext);
            if (!dct.effectDurationMap.isEmpty()) {
                final JsonArray jsonArray5 = new JsonArray();
                for (final MobEffect aoe7 : dct.effectDurationMap.keySet()) {
                    final JsonObject jsonObject2 = new JsonObject();
                    final ResourceLocation uh9 = Registry.MOB_EFFECT.getKey(aoe7);
                    if (uh9 == null) {
                        throw new IllegalArgumentException("Don't know how to serialize mob effect " + aoe7);
                    }
                    jsonObject2.add("type", new JsonPrimitive(uh9.toString()));
                    jsonObject2.add("duration", jsonSerializationContext.serialize(dct.effectDurationMap.get(aoe7)));
                    jsonArray5.add(jsonObject2);
                }
                jsonObject.add("effects", jsonArray5);
            }
        }
        
        @Override
        public SetStewEffectFunction deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final Map<MobEffect, RandomValueBounds> map5 = Maps.newHashMap();
            if (jsonObject.has("effects")) {
                final JsonArray jsonArray6 = GsonHelper.getAsJsonArray(jsonObject, "effects");
                for (final JsonElement jsonElement8 : jsonArray6) {
                    final String string9 = GsonHelper.getAsString(jsonElement8.getAsJsonObject(), "type");
                    final Object o;
                    final String str;
                    final MobEffect aoe10 = Registry.MOB_EFFECT.getOptional(new ResourceLocation(string9)).<Throwable>orElseThrow(() -> {
                        new JsonSyntaxException("Unknown mob effect '" + str + "'");
                        return o;
                    });
                    final RandomValueBounds dbb11 = GsonHelper.<RandomValueBounds>getAsObject(jsonElement8.getAsJsonObject(), "duration", jsonDeserializationContext, RandomValueBounds.class);
                    map5.put(aoe10, dbb11);
                }
            }
            return new SetStewEffectFunction(arr, map5, null);
        }
    }
}
