package net.minecraft.advancements.critereon;

import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.entity.Entity;
import javax.annotation.Nullable;
import net.minecraft.world.entity.AgableMob;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.server.level.ServerPlayer;
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;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        final EntityPredicate.Composite b2 = EntityPredicate.Composite.fromJson(jsonObject, "parent", av);
        final EntityPredicate.Composite b3 = EntityPredicate.Composite.fromJson(jsonObject, "partner", av);
        final EntityPredicate.Composite b4 = EntityPredicate.Composite.fromJson(jsonObject, "child", av);
        return new TriggerInstance(b, b2, b3, b4);
    }
    
    public void trigger(final ServerPlayer ze, final Animal ayk2, final Animal ayk3, @Nullable final AgableMob aok) {
        final LootContext dat6 = EntityPredicate.createContext(ze, ayk2);
        final LootContext dat7 = EntityPredicate.createContext(ze, ayk3);
        final LootContext dat8 = (aok != null) ? EntityPredicate.createContext(ze, aok) : null;
        this.trigger(ze, a -> a.matches(dat6, dat7, dat8));
    }
    
    static {
        ID = new ResourceLocation("bred_animals");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final EntityPredicate.Composite parent;
        private final EntityPredicate.Composite partner;
        private final EntityPredicate.Composite child;
        
        public TriggerInstance(final EntityPredicate.Composite b1, final EntityPredicate.Composite b2, final EntityPredicate.Composite b3, final EntityPredicate.Composite b4) {
            super(BredAnimalsTrigger.ID, b1);
            this.parent = b2;
            this.partner = b3;
            this.child = b4;
        }
        
        public static TriggerInstance bredAnimals() {
            return new TriggerInstance(EntityPredicate.Composite.ANY, EntityPredicate.Composite.ANY, EntityPredicate.Composite.ANY, EntityPredicate.Composite.ANY);
        }
        
        public static TriggerInstance bredAnimals(final EntityPredicate.Builder a) {
            return new TriggerInstance(EntityPredicate.Composite.ANY, EntityPredicate.Composite.ANY, EntityPredicate.Composite.ANY, EntityPredicate.Composite.wrap(a.build()));
        }
        
        public static TriggerInstance bredAnimals(final EntityPredicate be1, final EntityPredicate be2, final EntityPredicate be3) {
            return new TriggerInstance(EntityPredicate.Composite.ANY, EntityPredicate.Composite.wrap(be1), EntityPredicate.Composite.wrap(be2), EntityPredicate.Composite.wrap(be3));
        }
        
        public boolean matches(final LootContext dat1, final LootContext dat2, @Nullable final LootContext dat3) {
            return (this.child == EntityPredicate.Composite.ANY || (dat3 != null && this.child.matches(dat3))) && ((this.parent.matches(dat1) && this.partner.matches(dat2)) || (this.parent.matches(dat2) && this.partner.matches(dat1)));
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            jsonObject3.add("parent", this.parent.toJson(cg));
            jsonObject3.add("partner", this.partner.toJson(cg));
            jsonObject3.add("child", this.child.toJson(cg));
            return jsonObject3;
        }
    }
}
