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

import java.util.Random;
import net.minecraft.world.entity.ai.navigation.PathNavigation;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.core.Vec3i;
import net.minecraft.core.Position;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.ai.util.RandomPos;
import net.minecraft.core.SectionPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerLevel;
import java.util.EnumSet;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.PathfinderMob;

public class StrollThroughVillageGoal extends Goal {
    private final PathfinderMob mob;
    private final int interval;
    @Nullable
    private BlockPos wantedPos;
    
    public StrollThroughVillageGoal(final PathfinderMob alg, final int integer) {
        this.mob = alg;
        this.interval = integer;
        this.setFlags(EnumSet.<Flag>of(Flag.MOVE));
    }
    
    @Override
    public boolean canUse() {
        if (this.mob.isVehicle()) {
            return false;
        }
        if (this.mob.level.isDay()) {
            return false;
        }
        if (this.mob.getRandom().nextInt(this.interval) != 0) {
            return false;
        }
        final ServerLevel xd2 = (ServerLevel)this.mob.level;
        final BlockPos fk2 = new BlockPos(this.mob);
        if (!xd2.closeToVillage(fk2, 6)) {
            return false;
        }
        final Vec3 cvl4 = RandomPos.getLandPos(this.mob, 15, 7, fk -> -xd2.sectionsToVillage(SectionPos.of(fk)));
        this.wantedPos = ((cvl4 == null) ? null : new BlockPos(cvl4));
        return this.wantedPos != null;
    }
    
    @Override
    public boolean canContinueToUse() {
        return this.wantedPos != null && !this.mob.getNavigation().isDone() && this.mob.getNavigation().getTargetPos().equals(this.wantedPos);
    }
    
    @Override
    public void tick() {
        if (this.wantedPos == null) {
            return;
        }
        final PathNavigation arr2 = this.mob.getNavigation();
        if (arr2.isDone() && !this.wantedPos.closerThan(this.mob.position(), 10.0)) {
            Vec3 cvl3 = new Vec3(this.wantedPos);
            final Vec3 cvl4 = this.mob.position();
            final Vec3 cvl5 = cvl4.subtract(cvl3);
            cvl3 = cvl5.scale(0.4).add(cvl3);
            final Vec3 cvl6 = cvl3.subtract(cvl4).normalize().scale(10.0).add(cvl4);
            BlockPos fk7 = new BlockPos(cvl6);
            fk7 = this.mob.level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, fk7);
            if (!arr2.moveTo(fk7.getX(), fk7.getY(), fk7.getZ(), 1.0)) {
                this.moveRandomly();
            }
        }
    }
    
    private void moveRandomly() {
        final Random random2 = this.mob.getRandom();
        final BlockPos fk3 = this.mob.level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, new BlockPos(this.mob).offset(-8 + random2.nextInt(16), 0, -8 + random2.nextInt(16)));
        this.mob.getNavigation().moveTo(fk3.getX(), fk3.getY(), fk3.getZ(), 1.0);
    }
}
