package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import net.minecraft.world.entity.Entity;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.world.entity.AgableMob;
import javax.annotation.Nullable;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class BredAnimalsTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return BredAnimalsTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final EntityPredicate bb4 = EntityPredicate.fromJson(jsonObject.get("parent"));
        final EntityPredicate bb5 = EntityPredicate.fromJson(jsonObject.get("partner"));
        final EntityPredicate bb6 = EntityPredicate.fromJson(jsonObject.get("child"));
        return new TriggerInstance(bb4, bb5, bb6);
    }
    
    public void trigger(final ServerPlayer xe, final Animal atc2, @Nullable final Animal atc3, @Nullable final AgableMob ako) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe, atc2, atc3, ako));
    }
    
    static {
        ID = new ResourceLocation("bred_animals");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final EntityPredicate parent;
        private final EntityPredicate partner;
        private final EntityPredicate child;
        
        public TriggerInstance(final EntityPredicate bb1, final EntityPredicate bb2, final EntityPredicate bb3) {
            super(BredAnimalsTrigger.ID);
            this.parent = bb1;
            this.partner = bb2;
            this.child = bb3;
        }
        
        public static TriggerInstance bredAnimals() {
            return new TriggerInstance(EntityPredicate.ANY, EntityPredicate.ANY, EntityPredicate.ANY);
        }
        
        public static TriggerInstance bredAnimals(final EntityPredicate.Builder a) {
            return new TriggerInstance(a.build(), EntityPredicate.ANY, EntityPredicate.ANY);
        }
        
        public boolean matches(final ServerPlayer xe, final Animal atc2, @Nullable final Animal atc3, @Nullable final AgableMob ako) {
            return this.child.matches(xe, ako) && ((this.parent.matches(xe, atc2) && this.partner.matches(xe, atc3)) || (this.parent.matches(xe, atc3) && this.partner.matches(xe, atc2)));
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("parent", this.parent.serializeToJson());
            jsonObject2.add("partner", this.partner.serializeToJson());
            jsonObject2.add("child", this.child.serializeToJson());
            return jsonObject2;
        }
    }
}
