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

import java.util.Iterator;
import java.util.Objects;
import net.minecraft.core.Position;
import net.minecraft.world.level.pathfinder.Node;
import java.util.Optional;
import net.minecraft.core.Vec3i;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.ai.util.RandomPos;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.world.entity.ai.village.poi.PoiType;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
import java.util.EnumSet;
import com.google.common.collect.Lists;
import java.util.function.BooleanSupplier;
import java.util.List;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.entity.PathfinderMob;

public class MoveThroughVillageGoal extends Goal {
    protected final PathfinderMob mob;
    private final double speedModifier;
    private Path path;
    private BlockPos poiPos;
    private final boolean onlyAtNight;
    private final List<BlockPos> visited;
    private final int distanceToPoi;
    private final BooleanSupplier canDealWithDoors;
    
    public MoveThroughVillageGoal(final PathfinderMob alg, final double double2, final boolean boolean3, final int integer, final BooleanSupplier booleanSupplier) {
        this.visited = Lists.newArrayList();
        this.mob = alg;
        this.speedModifier = double2;
        this.onlyAtNight = boolean3;
        this.distanceToPoi = integer;
        this.canDealWithDoors = booleanSupplier;
        this.setFlags(EnumSet.<Flag>of(Flag.MOVE));
        if (!(alg.getNavigation() instanceof GroundPathNavigation)) {
            throw new IllegalArgumentException("Unsupported mob for MoveThroughVillageGoal");
        }
    }
    
    @Override
    public boolean canUse() {
        this.updateVisited();
        if (this.onlyAtNight && this.mob.level.isDay()) {
            return false;
        }
        final ServerLevel xd2 = (ServerLevel)this.mob.level;
        final BlockPos fk4 = new BlockPos(this.mob);
        if (!xd2.closeToVillage(fk4, 6)) {
            return false;
        }
        final ServerLevel serverLevel;
        Optional<BlockPos> optional5;
        final Vec3i gg;
        final Vec3 cvl4 = RandomPos.getLandPos(this.mob, 15, 7, fk3 -> {
            if (!serverLevel.isVillage(fk3)) {
                return Double.NEGATIVE_INFINITY;
            }
            else {
                optional5 = serverLevel.getPoiManager().find(PoiType.ALL, this::hasNotVisited, fk3, 10, PoiManager.Occupancy.IS_OCCUPIED);
                if (!optional5.isPresent()) {
                    return Double.NEGATIVE_INFINITY;
                }
                else {
                    return -optional5.get().distSqr(gg);
                }
            }
        });
        if (cvl4 == null) {
            return false;
        }
        final Optional<BlockPos> optional6 = xd2.getPoiManager().find(PoiType.ALL, this::hasNotVisited, new BlockPos(cvl4), 10, PoiManager.Occupancy.IS_OCCUPIED);
        if (!optional6.isPresent()) {
            return false;
        }
        this.poiPos = optional6.get().immutable();
        final GroundPathNavigation arq6 = (GroundPathNavigation)this.mob.getNavigation();
        final boolean boolean7 = arq6.canOpenDoors();
        arq6.setCanOpenDoors(this.canDealWithDoors.getAsBoolean());
        this.path = arq6.createPath(this.poiPos, 0);
        arq6.setCanOpenDoors(boolean7);
        if (this.path == null) {
            final Vec3 cvl5 = RandomPos.getPosTowards(this.mob, 10, 7, new Vec3(this.poiPos));
            if (cvl5 == null) {
                return false;
            }
            arq6.setCanOpenDoors(this.canDealWithDoors.getAsBoolean());
            this.path = this.mob.getNavigation().createPath(cvl5.x, cvl5.y, cvl5.z, 0);
            arq6.setCanOpenDoors(boolean7);
            if (this.path == null) {
                return false;
            }
        }
        for (int integer8 = 0; integer8 < this.path.getSize(); ++integer8) {
            final Node cqo9 = this.path.get(integer8);
            final BlockPos fk5 = new BlockPos(cqo9.x, cqo9.y + 1, cqo9.z);
            if (DoorInteractGoal.isDoor(this.mob.level, fk5)) {
                this.path = this.mob.getNavigation().createPath(cqo9.x, cqo9.y, cqo9.z, 0);
                break;
            }
        }
        return this.path != null;
    }
    
    @Override
    public boolean canContinueToUse() {
        return !this.mob.getNavigation().isDone() && !this.poiPos.closerThan(this.mob.position(), this.mob.getBbWidth() + this.distanceToPoi);
    }
    
    @Override
    public void start() {
        this.mob.getNavigation().moveTo(this.path, this.speedModifier);
    }
    
    @Override
    public void stop() {
        if (this.mob.getNavigation().isDone() || this.poiPos.closerThan(this.mob.position(), this.distanceToPoi)) {
            this.visited.add(this.poiPos);
        }
    }
    
    private boolean hasNotVisited(final BlockPos fk) {
        for (final BlockPos fk2 : this.visited) {
            if (Objects.equals(fk, fk2)) {
                return false;
            }
        }
        return true;
    }
    
    private void updateVisited() {
        if (this.visited.size() > 15) {
            this.visited.remove(0);
        }
    }
}
