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 auh, final double double2) {
        this.llama = auh;
        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<?> aku2;
        final List<Entity> list2 = this.llama.level.getEntities(this.llama, this.llama.getBoundingBox().inflate(9.0, 4.0, 9.0), akq -> {
            aku2 = akq.getType();
            return aku2 == EntityType.LLAMA || aku2 == EntityType.TRADER_LLAMA;
        });
        Llama auh3 = null;
        double double4 = Double.MAX_VALUE;
        for (final Entity akq2 : list2) {
            final Llama auh4 = (Llama)akq2;
            if (auh4.inCaravan()) {
                if (auh4.hasCaravanTail()) {
                    continue;
                }
                final double double5 = this.llama.distanceToSqr(auh4);
                if (double5 > double4) {
                    continue;
                }
                double4 = double5;
                auh3 = auh4;
            }
        }
        if (auh3 == null) {
            for (final Entity akq2 : list2) {
                final Llama auh4 = (Llama)akq2;
                if (!auh4.isLeashed()) {
                    continue;
                }
                if (auh4.hasCaravanTail()) {
                    continue;
                }
                final double double5 = this.llama.distanceToSqr(auh4);
                if (double5 > double4) {
                    continue;
                }
                double4 = double5;
                auh3 = auh4;
            }
        }
        if (auh3 == null) {
            return false;
        }
        if (double4 < 4.0) {
            return false;
        }
        if (!auh3.isLeashed() && !this.firstIsLeashed(auh3, 1)) {
            return false;
        }
        this.llama.joinCaravan(auh3);
        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 auh2 = this.llama.getCaravanHead();
        final double double3 = this.llama.distanceTo(auh2);
        final float float5 = 2.0f;
        final Vec3 cvl6 = new Vec3(auh2.getX() - this.llama.getX(), auh2.getY() - this.llama.getY(), auh2.getZ() - this.llama.getZ()).normalize().scale(Math.max(double3 - 2.0, 0.0));
        this.llama.getNavigation().moveTo(this.llama.getX() + cvl6.x, this.llama.getY() + cvl6.y, this.llama.getZ() + cvl6.z, this.speedModifier);
    }
    
    private boolean firstIsLeashed(final Llama auh, int integer) {
        return integer <= 8 && auh.inCaravan() && (auh.getCaravanHead().isLeashed() || this.firstIsLeashed(auh.getCaravanHead(), ++integer));
    }
}
