package net.minecraft.world.entity.ai.goal;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import java.util.EnumSet;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;

public class BreedGoal extends Goal {
    private static final TargetingConditions PARTNER_TARGETING;
    protected final Animal animal;
    private final Class<? extends Animal> partnerClass;
    protected final Level level;
    protected Animal partner;
    private int loveTime;
    private final double speedModifier;
    
    public BreedGoal(final Animal ayk, final double double2) {
        this(ayk, double2, ayk.getClass());
    }
    
    public BreedGoal(final Animal ayk, final double double2, final Class<? extends Animal> class3) {
        this.animal = ayk;
        this.level = ayk.level;
        this.partnerClass = class3;
        this.speedModifier = double2;
        this.setFlags(EnumSet.<Flag>of(Flag.MOVE, Flag.LOOK));
    }
    
    @Override
    public boolean canUse() {
        if (!this.animal.isInLove()) {
            return false;
        }
        this.partner = this.getFreePartner();
        return this.partner != null;
    }
    
    @Override
    public boolean canContinueToUse() {
        return this.partner.isAlive() && this.partner.isInLove() && this.loveTime < 60;
    }
    
    @Override
    public void stop() {
        this.partner = null;
        this.loveTime = 0;
    }
    
    @Override
    public void tick() {
        this.animal.getLookControl().setLookAt(this.partner, 10.0f, (float)this.animal.getMaxHeadXRot());
        this.animal.getNavigation().moveTo(this.partner, this.speedModifier);
        ++this.loveTime;
        if (this.loveTime >= 60 && this.animal.distanceToSqr(this.partner) < 9.0) {
            this.breed();
        }
    }
    
    @Nullable
    private Animal getFreePartner() {
        final List<Animal> list2 = this.level.<Animal>getNearbyEntities(this.partnerClass, BreedGoal.PARTNER_TARGETING, (LivingEntity)this.animal, this.animal.getBoundingBox().inflate(8.0));
        double double3 = Double.MAX_VALUE;
        Animal ayk5 = null;
        for (final Animal ayk6 : list2) {
            if (this.animal.canMate(ayk6) && this.animal.distanceToSqr(ayk6) < double3) {
                ayk5 = ayk6;
                double3 = this.animal.distanceToSqr(ayk6);
            }
        }
        return ayk5;
    }
    
    protected void breed() {
        this.animal.spawnChildFromBreeding(this.level, this.partner);
    }
    
    static {
        PARTNER_TARGETING = new TargetingConditions().range(8.0).allowInvulnerable().allowSameTeam().allowUnseeable();
    }
}
