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

import net.minecraft.world.phys.Vec3;
import java.util.Iterator;
import java.util.List;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import java.util.EnumSet;
import net.minecraft.world.entity.animal.horse.Llama;

public class LlamaFollowCaravanGoal extends Goal {
    public final Llama llama;
    private double speedModifier;
    private int distCheckCounter;
    
    public LlamaFollowCaravanGoal(final Llama azp, final double double2) {
        this.llama = azp;
        this.speedModifier = double2;
        this.setFlags(EnumSet.<Flag>of(Flag.MOVE));
    }
    
    @Override
    public boolean canUse() {
        if (this.llama.isLeashed() || this.llama.inCaravan()) {
            return false;
        }
        final EntityType<?> aoq2;
        final List<Entity> list2 = this.llama.level.getEntities(this.llama, this.llama.getBoundingBox().inflate(9.0, 4.0, 9.0), aom -> {
            aoq2 = aom.getType();
            return aoq2 == EntityType.LLAMA || aoq2 == EntityType.TRADER_LLAMA;
        });
        Llama azp3 = null;
        double double4 = Double.MAX_VALUE;
        for (final Entity aom2 : list2) {
            final Llama azp4 = (Llama)aom2;
            if (azp4.inCaravan()) {
                if (azp4.hasCaravanTail()) {
                    continue;
                }
                final double double5 = this.llama.distanceToSqr(azp4);
                if (double5 > double4) {
                    continue;
                }
                double4 = double5;
                azp3 = azp4;
            }
        }
        if (azp3 == null) {
            for (final Entity aom2 : list2) {
                final Llama azp4 = (Llama)aom2;
                if (!azp4.isLeashed()) {
                    continue;
                }
                if (azp4.hasCaravanTail()) {
                    continue;
                }
                final double double5 = this.llama.distanceToSqr(azp4);
                if (double5 > double4) {
                    continue;
                }
                double4 = double5;
                azp3 = azp4;
            }
        }
        if (azp3 == null) {
            return false;
        }
        if (double4 < 4.0) {
            return false;
        }
        if (!azp3.isLeashed() && !this.firstIsLeashed(azp3, 1)) {
            return false;
        }
        this.llama.joinCaravan(azp3);
        return true;
    }
    
    @Override
    public boolean canContinueToUse() {
        if (!this.llama.inCaravan() || !this.llama.getCaravanHead().isAlive() || !this.firstIsLeashed(this.llama, 0)) {
            return false;
        }
        final double double2 = this.llama.distanceToSqr(this.llama.getCaravanHead());
        if (double2 > 676.0) {
            if (this.speedModifier <= 3.0) {
                this.speedModifier *= 1.2;
                this.distCheckCounter = 40;
                return true;
            }
            if (this.distCheckCounter == 0) {
                return false;
            }
        }
        if (this.distCheckCounter > 0) {
            --this.distCheckCounter;
        }
        return true;
    }
    
    @Override
    public void stop() {
        this.llama.leaveCaravan();
        this.speedModifier = 2.1;
    }
    
    @Override
    public void tick() {
        if (!this.llama.inCaravan()) {
            return;
        }
        final Llama azp2 = this.llama.getCaravanHead();
        final double double3 = this.llama.distanceTo(azp2);
        final float float5 = 2.0f;
        final Vec3 dem6 = new Vec3(azp2.getX() - this.llama.getX(), azp2.getY() - this.llama.getY(), azp2.getZ() - this.llama.getZ()).normalize().scale(Math.max(double3 - 2.0, 0.0));
        this.llama.getNavigation().moveTo(this.llama.getX() + dem6.x, this.llama.getY() + dem6.y, this.llama.getZ() + dem6.z, this.speedModifier);
    }
    
    private boolean firstIsLeashed(final Llama azp, int integer) {
        return integer <= 8 && azp.inCaravan() && (azp.getCaravanHead().isLeashed() || this.firstIsLeashed(azp.getCaravanHead(), ++integer));
    }
}
