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

import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.Entity;
import java.util.Iterator;
import java.util.List;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.vehicle.Boat;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.PathfinderMob;

public class FollowBoatGoal extends Goal {
    private int timeToRecalcPath;
    private final PathfinderMob mob;
    private LivingEntity following;
    private BoatGoals currentGoal;
    
    public FollowBoatGoal(final PathfinderMob alg) {
        this.mob = alg;
    }
    
    @Override
    public boolean canUse() {
        final List<Boat> list2 = this.mob.level.<Boat>getEntitiesOfClass(Boat.class, this.mob.getBoundingBox().inflate(5.0));
        boolean boolean3 = false;
        for (final Boat azz5 : list2) {
            final Entity akq6 = azz5.getControllingPassenger();
            if (akq6 instanceof LivingEntity && (Mth.abs(((LivingEntity)akq6).xxa) > 0.0f || Mth.abs(((LivingEntity)akq6).zza) > 0.0f)) {
                boolean3 = true;
                break;
            }
        }
        return (this.following != null && (Mth.abs(this.following.xxa) > 0.0f || Mth.abs(this.following.zza) > 0.0f)) || boolean3;
    }
    
    @Override
    public boolean isInterruptable() {
        return true;
    }
    
    @Override
    public boolean canContinueToUse() {
        return this.following != null && this.following.isPassenger() && (Mth.abs(this.following.xxa) > 0.0f || Mth.abs(this.following.zza) > 0.0f);
    }
    
    @Override
    public void start() {
        final List<Boat> list2 = this.mob.level.<Boat>getEntitiesOfClass(Boat.class, this.mob.getBoundingBox().inflate(5.0));
        for (final Boat azz4 : list2) {
            if (azz4.getControllingPassenger() != null && azz4.getControllingPassenger() instanceof LivingEntity) {
                this.following = (LivingEntity)azz4.getControllingPassenger();
                break;
            }
        }
        this.timeToRecalcPath = 0;
        this.currentGoal = BoatGoals.GO_TO_BOAT;
    }
    
    @Override
    public void stop() {
        this.following = null;
    }
    
    @Override
    public void tick() {
        final boolean boolean2 = Mth.abs(this.following.xxa) > 0.0f || Mth.abs(this.following.zza) > 0.0f;
        final float float3 = (this.currentGoal == BoatGoals.GO_IN_BOAT_DIRECTION) ? (boolean2 ? 0.17999999f : 0.0f) : 0.135f;
        this.mob.moveRelative(float3, new Vec3(this.mob.xxa, this.mob.yya, this.mob.zza));
        this.mob.move(MoverType.SELF, this.mob.getDeltaMovement());
        final int timeToRecalcPath = this.timeToRecalcPath - 1;
        this.timeToRecalcPath = timeToRecalcPath;
        if (timeToRecalcPath > 0) {
            return;
        }
        this.timeToRecalcPath = 10;
        if (this.currentGoal == BoatGoals.GO_TO_BOAT) {
            BlockPos fk4 = new BlockPos(this.following).relative(this.following.getDirection().getOpposite());
            fk4 = fk4.offset(0, -1, 0);
            this.mob.getNavigation().moveTo(fk4.getX(), fk4.getY(), fk4.getZ(), 1.0);
            if (this.mob.distanceTo(this.following) < 4.0f) {
                this.timeToRecalcPath = 0;
                this.currentGoal = BoatGoals.GO_IN_BOAT_DIRECTION;
            }
        }
        else if (this.currentGoal == BoatGoals.GO_IN_BOAT_DIRECTION) {
            final Direction fp4 = this.following.getMotionDirection();
            final BlockPos fk5 = new BlockPos(this.following).relative(fp4, 10);
            this.mob.getNavigation().moveTo(fk5.getX(), fk5.getY() - 1, fk5.getZ(), 1.0);
            if (this.mob.distanceTo(this.following) > 12.0f) {
                this.timeToRecalcPath = 0;
                this.currentGoal = BoatGoals.GO_TO_BOAT;
            }
        }
    }
}
