package net.minecraft.advancements.critereon;

import com.google.gson.JsonPrimitive;
import com.google.gson.JsonNull;
import net.minecraft.tags.Tag;
import com.google.gson.JsonSyntaxException;
import net.minecraft.core.Registry;
import net.minecraft.tags.SerializationTags;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import javax.annotation.Nullable;
import com.google.gson.JsonElement;
import net.minecraft.world.entity.EntityType;
import com.google.common.base.Joiner;

public abstract class EntityTypePredicate {
    public static final EntityTypePredicate ANY;
    private static final Joiner COMMA_JOINER;
    
    public abstract boolean matches(final EntityType<?> aoq);
    
    public abstract JsonElement serializeToJson();
    
    public static EntityTypePredicate fromJson(@Nullable final JsonElement jsonElement) {
        if (jsonElement == null || jsonElement.isJsonNull()) {
            return EntityTypePredicate.ANY;
        }
        final String string2 = GsonHelper.convertToString(jsonElement, "type");
        if (string2.startsWith("#")) {
            final ResourceLocation uh3 = new ResourceLocation(string2.substring(1));
            return new TagPredicate(SerializationTags.getInstance().getEntityTypes().getTagOrEmpty(uh3));
        }
        final ResourceLocation uh3 = new ResourceLocation(string2);
        final Object o;
        final Object obj;
        final EntityType<?> aoq4 = Registry.ENTITY_TYPE.getOptional(uh3).<Throwable>orElseThrow(() -> {
            new JsonSyntaxException("Unknown entity type '" + obj + "', valid types are: " + EntityTypePredicate.COMMA_JOINER.join(Registry.ENTITY_TYPE.keySet()));
            return o;
        });
        return new TypePredicate(aoq4);
    }
    
    public static EntityTypePredicate of(final EntityType<?> aoq) {
        return new TypePredicate(aoq);
    }
    
    public static EntityTypePredicate of(final Tag<EntityType<?>> adf) {
        return new TagPredicate(adf);
    }
    
    static {
        ANY = new EntityTypePredicate() {
            @Override
            public boolean matches(final EntityType<?> aoq) {
                return true;
            }
            
            @Override
            public JsonElement serializeToJson() {
                return JsonNull.INSTANCE;
            }
        };
        COMMA_JOINER = Joiner.on(", ");
    }
    
    static class TypePredicate extends EntityTypePredicate {
        private final EntityType<?> type;
        
        public TypePredicate(final EntityType<?> aoq) {
            this.type = aoq;
        }
        
        @Override
        public boolean matches(final EntityType<?> aoq) {
            return this.type == aoq;
        }
        
        @Override
        public JsonElement serializeToJson() {
            return new JsonPrimitive(Registry.ENTITY_TYPE.getKey(this.type).toString());
        }
    }
    
    static class TagPredicate extends EntityTypePredicate {
        private final Tag<EntityType<?>> tag;
        
        public TagPredicate(final Tag<EntityType<?>> adf) {
            this.tag = adf;
        }
        
        @Override
        public boolean matches(final EntityType<?> aoq) {
            return this.tag.contains(aoq);
        }
        
        @Override
        public JsonElement serializeToJson() {
            return new JsonPrimitive("#" + SerializationTags.getInstance().getEntityTypes().getIdOrThrow(this.tag));
        }
    }
}
