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.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 apg, final int integer) {
        this.mob = apg;
        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 zd2 = (ServerLevel)this.mob.level;
        final BlockPos fu2 = this.mob.blockPosition();
        if (!zd2.isCloseToVillage(fu2, 6)) {
            return false;
        }
        final Vec3 dem4 = RandomPos.getLandPos(this.mob, 15, 7, fu -> -zd2.sectionsToVillage(SectionPos.of(fu)));
        this.wantedPos = ((dem4 == null) ? null : new BlockPos(dem4));
        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 awv2 = this.mob.getNavigation();
        if (awv2.isDone() && !this.wantedPos.closerThan(this.mob.position(), 10.0)) {
            Vec3 dem3 = Vec3.atBottomCenterOf(this.wantedPos);
            final Vec3 dem4 = this.mob.position();
            final Vec3 dem5 = dem4.subtract(dem3);
            dem3 = dem5.scale(0.4).add(dem3);
            final Vec3 dem6 = dem3.subtract(dem4).normalize().scale(10.0).add(dem4);
            BlockPos fu7 = new BlockPos(dem6);
            fu7 = this.mob.level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, fu7);
            if (!awv2.moveTo(fu7.getX(), fu7.getY(), fu7.getZ(), 1.0)) {
                this.moveRandomly();
            }
        }
    }
    
    private void moveRandomly() {
        final Random random2 = this.mob.getRandom();
        final BlockPos fu3 = this.mob.level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, this.mob.blockPosition().offset(-8 + random2.nextInt(16), 0, -8 + random2.nextInt(16)));
        this.mob.getNavigation().moveTo(fu3.getX(), fu3.getY(), fu3.getZ(), 1.0);
    }
}
