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

import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonParseException;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import java.lang.reflect.Type;
import com.google.gson.JsonElement;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonDeserializer;
import com.google.common.collect.Maps;
import java.util.function.Predicate;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;

public class LootItemConditions {
    private static final Map<ResourceLocation, LootItemCondition.Serializer<?>> CONDITIONS_BY_NAME;
    private static final Map<Class<? extends LootItemCondition>, LootItemCondition.Serializer<?>> CONDITIONS_BY_CLASS;
    
    public static <T extends LootItemCondition> void register(final LootItemCondition.Serializer<? extends T> b) {
        final ResourceLocation sm2 = b.getName();
        final Class<T> class3 = (Class<T>)b.getPredicateClass();
        if (LootItemConditions.CONDITIONS_BY_NAME.containsKey(sm2)) {
            throw new IllegalArgumentException("Can't re-register item condition name " + sm2);
        }
        if (LootItemConditions.CONDITIONS_BY_CLASS.containsKey(class3)) {
            throw new IllegalArgumentException("Can't re-register item condition class " + class3.getName());
        }
        LootItemConditions.CONDITIONS_BY_NAME.put(sm2, b);
        LootItemConditions.CONDITIONS_BY_CLASS.put(class3, b);
    }
    
    public static LootItemCondition.Serializer<?> getSerializer(final ResourceLocation sm) {
        final LootItemCondition.Serializer<?> b2 = LootItemConditions.CONDITIONS_BY_NAME.get(sm);
        if (b2 == null) {
            throw new IllegalArgumentException("Unknown loot item condition '" + sm + "'");
        }
        return b2;
    }
    
    public static <T extends LootItemCondition> LootItemCondition.Serializer<T> getSerializer(final T cum) {
        final LootItemCondition.Serializer<T> b2 = (LootItemCondition.Serializer<T>)LootItemConditions.CONDITIONS_BY_CLASS.get(cum.getClass());
        if (b2 == null) {
            throw new IllegalArgumentException("Unknown loot item condition " + cum);
        }
        return b2;
    }
    
    public static <T> Predicate<T> andConditions(final Predicate<T>[] arr) {
        switch (arr.length) {
            case 0: {
                return object -> true;
            }
            case 1: {
                return arr[0];
            }
            case 2: {
                return arr[0].and(arr[1]);
            }
            default: {
                final int length;
                int i = 0;
                Predicate<Object> predicate6;
                return object -> {
                    length = arr.length;
                    while (i < length) {
                        predicate6 = (Predicate<Object>)arr[i];
                        if (!predicate6.test(object)) {
                            return false;
                        }
                        else {
                            ++i;
                        }
                    }
                    return true;
                };
            }
        }
    }
    
    public static <T> Predicate<T> orConditions(final Predicate<T>[] arr) {
        switch (arr.length) {
            case 0: {
                return object -> false;
            }
            case 1: {
                return arr[0];
            }
            case 2: {
                return arr[0].or(arr[1]);
            }
            default: {
                final int length;
                int i = 0;
                Predicate<Object> predicate6;
                return object -> {
                    length = arr.length;
                    while (i < length) {
                        predicate6 = (Predicate<Object>)arr[i];
                        if (predicate6.test(object)) {
                            return true;
                        }
                        else {
                            ++i;
                        }
                    }
                    return false;
                };
            }
        }
    }
    
    static {
        CONDITIONS_BY_NAME = Maps.newHashMap();
        CONDITIONS_BY_CLASS = Maps.newHashMap();
        LootItemConditions.<LootItemCondition>register(new InvertedLootItemCondition.Serializer());
        LootItemConditions.<LootItemCondition>register(new AlternativeLootItemCondition.Serializer());
        LootItemConditions.<LootItemCondition>register(new LootItemRandomChanceCondition.Serializer());
        LootItemConditions.<LootItemCondition>register(new LootItemRandomChanceWithLootingCondition.Serializer());
        LootItemConditions.<LootItemCondition>register(new LootItemEntityPropertyCondition.Serializer());
        LootItemConditions.<LootItemCondition>register(new LootItemKilledByPlayerCondition.Serializer());
        LootItemConditions.<LootItemCondition>register(new EntityHasScoreCondition.Serializer());
        LootItemConditions.<LootItemCondition>register(new LootItemBlockStatePropertyCondition.Serializer());
        LootItemConditions.<LootItemCondition>register(new MatchTool.Serializer());
        LootItemConditions.<LootItemCondition>register(new BonusLevelTableCondition.Serializer());
        LootItemConditions.<LootItemCondition>register(new ExplosionCondition.Serializer());
        LootItemConditions.<LootItemCondition>register(new DamageSourceCondition.Serializer());
        LootItemConditions.<LootItemCondition>register(new LocationCheck.Serializer());
        LootItemConditions.<LootItemCondition>register(new WeatherCheck.Serializer());
        LootItemConditions.<LootItemCondition>register(new ConditionReference.Serializer());
        LootItemConditions.<LootItemCondition>register(new TimeCheck.Serializer());
    }
    
    public static class Serializer implements JsonDeserializer<LootItemCondition>, JsonSerializer<LootItemCondition> {
        @Override
        public LootItemCondition deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
            final JsonObject jsonObject5 = GsonHelper.convertToJsonObject(jsonElement, "condition");
            final ResourceLocation sm6 = new ResourceLocation(GsonHelper.getAsString(jsonObject5, "condition"));
            LootItemCondition.Serializer<?> b7;
            try {
                b7 = LootItemConditions.getSerializer(sm6);
            }
            catch (IllegalArgumentException illegalArgumentException8) {
                throw new JsonSyntaxException("Unknown condition '" + sm6 + "'");
            }
            return (LootItemCondition)b7.deserialize(jsonObject5, jsonDeserializationContext);
        }
        
        @Override
        public JsonElement serialize(final LootItemCondition cum, final Type type, final JsonSerializationContext jsonSerializationContext) {
            final LootItemCondition.Serializer<LootItemCondition> b5 = LootItemConditions.<LootItemCondition>getSerializer(cum);
            final JsonObject jsonObject6 = new JsonObject();
            jsonObject6.addProperty("condition", b5.getName().toString());
            b5.serialize(jsonObject6, cum, jsonSerializationContext);
            return jsonObject6;
        }
    }
}
