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.EntityTypeTags;
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<?> aku);
    
    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 sm3 = new ResourceLocation(string2.substring(1));
            final Tag<EntityType<?>> aaz4 = EntityTypeTags.getAllTags().getTagOrEmpty(sm3);
            return new TagPredicate(aaz4);
        }
        final ResourceLocation sm3 = new ResourceLocation(string2);
        final Object o;
        final Object obj;
        final EntityType<?> aku4 = Registry.ENTITY_TYPE.getOptional(sm3).<Throwable>orElseThrow(() -> {
            new JsonSyntaxException("Unknown entity type '" + obj + "', valid types are: " + EntityTypePredicate.COMMA_JOINER.join(Registry.ENTITY_TYPE.keySet()));
            return o;
        });
        return new TypePredicate(aku4);
    }
    
    public static EntityTypePredicate of(final EntityType<?> aku) {
        return new TypePredicate(aku);
    }
    
    public static EntityTypePredicate of(final Tag<EntityType<?>> aaz) {
        return new TagPredicate(aaz);
    }
    
    static {
        ANY = new EntityTypePredicate() {
            @Override
            public boolean matches(final EntityType<?> aku) {
                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<?> aku) {
            this.type = aku;
        }
        
        @Override
        public boolean matches(final EntityType<?> aku) {
            return this.type == aku;
        }
        
        @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<?>> aaz) {
            this.tag = aaz;
        }
        
        @Override
        public boolean matches(final EntityType<?> aku) {
            return this.tag.contains(aku);
        }
        
        @Override
        public JsonElement serializeToJson() {
            return new JsonPrimitive("#" + this.tag.getId().toString());
        }
    }
}
