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

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.ai.util.RandomPos;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.core.Vec3i;
import net.minecraft.world.entity.ai.navigation.PathNavigation;
import java.util.Optional;
import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.ai.memory.WalkTarget;
import net.minecraft.server.level.ServerLevel;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.core.BlockPos;
import javax.annotation.Nullable;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.entity.Mob;

public class MoveToTargetSink extends Behavior<Mob> {
    @Nullable
    private Path path;
    @Nullable
    private BlockPos lastTargetPos;
    private float speed;
    private int remainingDelay;
    
    public MoveToTargetSink(final int integer) {
        super(ImmutableMap.<MemoryModuleType<Path>, MemoryStatus>of(MemoryModuleType.PATH, MemoryStatus.VALUE_ABSENT, (MemoryModuleType<Path>)MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_PRESENT), integer);
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel xd, final Mob ala) {
        final Brain<?> alp4 = ala.getBrain();
        final WalkTarget arn5 = alp4.<WalkTarget>getMemory(MemoryModuleType.WALK_TARGET).get();
        if (!this.reachedTarget(ala, arn5) && this.tryComputePath(ala, arn5, xd.getGameTime())) {
            this.lastTargetPos = arn5.getTarget().getPos();
            return true;
        }
        alp4.<WalkTarget>eraseMemory(MemoryModuleType.WALK_TARGET);
        return false;
    }
    
    @Override
    protected boolean canStillUse(final ServerLevel xd, final Mob ala, final long long3) {
        if (this.path == null || this.lastTargetPos == null) {
            return false;
        }
        final Optional<WalkTarget> optional6 = ala.getBrain().<WalkTarget>getMemory(MemoryModuleType.WALK_TARGET);
        final PathNavigation arr7 = ala.getNavigation();
        return !arr7.isDone() && optional6.isPresent() && !this.reachedTarget(ala, optional6.get());
    }
    
    @Override
    protected void stop(final ServerLevel xd, final Mob ala, final long long3) {
        ala.getNavigation().stop();
        ala.getBrain().<WalkTarget>eraseMemory(MemoryModuleType.WALK_TARGET);
        ala.getBrain().<Path>eraseMemory(MemoryModuleType.PATH);
        this.path = null;
    }
    
    @Override
    protected void start(final ServerLevel xd, final Mob ala, final long long3) {
        ala.getBrain().<Path>setMemory(MemoryModuleType.PATH, this.path);
        ala.getNavigation().moveTo(this.path, this.speed);
        this.remainingDelay = xd.getRandom().nextInt(10);
    }
    
    @Override
    protected void tick(final ServerLevel xd, final Mob ala, final long long3) {
        --this.remainingDelay;
        if (this.remainingDelay > 0) {
            return;
        }
        final Path cqq6 = ala.getNavigation().getPath();
        final Brain<?> alp7 = ala.getBrain();
        if (this.path != cqq6) {
            this.path = cqq6;
            alp7.<Path>setMemory(MemoryModuleType.PATH, cqq6);
        }
        if (cqq6 == null || this.lastTargetPos == null) {
            return;
        }
        final WalkTarget arn8 = alp7.<WalkTarget>getMemory(MemoryModuleType.WALK_TARGET).get();
        if (arn8.getTarget().getPos().distSqr(this.lastTargetPos) > 4.0 && this.tryComputePath(ala, arn8, xd.getGameTime())) {
            this.lastTargetPos = arn8.getTarget().getPos();
            this.start(xd, ala, long3);
        }
    }
    
    private boolean tryComputePath(final Mob ala, final WalkTarget arn, final long long3) {
        final BlockPos fk6 = arn.getTarget().getPos();
        this.path = ala.getNavigation().createPath(fk6, 0);
        this.speed = arn.getSpeed();
        if (!this.reachedTarget(ala, arn)) {
            final Brain<?> alp7 = ala.getBrain();
            final boolean boolean8 = this.path != null && this.path.canReach();
            if (boolean8) {
                alp7.<Long>setMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE, Optional.<Long>empty());
            }
            else if (!alp7.hasMemoryValue(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE)) {
                alp7.<Long>setMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE, long3);
            }
            if (this.path != null) {
                return true;
            }
            final Vec3 cvl9 = RandomPos.getPosTowards((PathfinderMob)ala, 10, 7, new Vec3(fk6));
            if (cvl9 != null) {
                this.path = ala.getNavigation().createPath(cvl9.x, cvl9.y, cvl9.z, 0);
                return this.path != null;
            }
        }
        return false;
    }
    
    private boolean reachedTarget(final Mob ala, final WalkTarget arn) {
        return arn.getTarget().getPos().distManhattan(new BlockPos(ala)) <= arn.getCloseEnoughDist();
    }
}
