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

import net.minecraft.world.level.pathfinder.Path;
import java.util.stream.Stream;
import java.util.function.Predicate;
import net.minecraft.network.protocol.game.DebugPackets;
import java.util.Optional;
import net.minecraft.core.Vec3i;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.ai.village.poi.PoiType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.server.level.ServerLevel;
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
import java.util.Map;
import net.minecraft.world.entity.ai.memory.WalkTarget;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import it.unimi.dsi.fastutil.longs.Long2LongMap;
import net.minecraft.world.entity.LivingEntity;

public class SetClosestHomeAsWalkTarget extends Behavior<LivingEntity> {
    private final float speed;
    private final Long2LongMap batchCache;
    private int triedCount;
    private long lastUpdate;
    
    public SetClosestHomeAsWalkTarget(final float float1) {
        super(ImmutableMap.<MemoryModuleType<WalkTarget>, MemoryStatus>of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT, (MemoryModuleType<WalkTarget>)MemoryModuleType.HOME, MemoryStatus.VALUE_ABSENT));
        this.batchCache = (Long2LongMap)new Long2LongOpenHashMap();
        this.speed = float1;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel xd, final LivingEntity akz) {
        if (xd.getGameTime() - this.lastUpdate < 20L) {
            return false;
        }
        final PathfinderMob alg4 = (PathfinderMob)akz;
        final PoiManager asr5 = xd.getPoiManager();
        final Optional<BlockPos> optional6 = asr5.findClosest(PoiType.HOME.getPredicate(), fk -> true, new BlockPos(akz), 48, PoiManager.Occupancy.ANY);
        return optional6.isPresent() && optional6.get().distSqr(new BlockPos(alg4)) > 4.0;
    }
    
    @Override
    protected void start(final ServerLevel xd, final LivingEntity akz, final long long3) {
        this.triedCount = 0;
        this.lastUpdate = xd.getGameTime() + xd.getRandom().nextInt(20);
        final PathfinderMob alg6 = (PathfinderMob)akz;
        final PoiManager asr7 = xd.getPoiManager();
        final long long4;
        final Predicate<BlockPos> predicate8 = fk -> {
            long4 = fk.asLong();
            if (this.batchCache.containsKey(long4)) {
                return false;
            }
            else if (++this.triedCount >= 5) {
                return false;
            }
            else {
                this.batchCache.put(long4, this.lastUpdate + 40L);
                return true;
            }
        };
        final Stream<BlockPos> stream9 = asr7.findAll(PoiType.HOME.getPredicate(), predicate8, new BlockPos(akz), 48, PoiManager.Occupancy.ANY);
        final Path cqq10 = alg6.getNavigation().createPath(stream9, PoiType.HOME.getValidRange());
        if (cqq10 != null && cqq10.canReach()) {
            final BlockPos fk2 = cqq10.getTarget();
            final Optional<PoiType> optional12 = asr7.getType(fk2);
            if (optional12.isPresent()) {
                akz.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(fk2, this.speed, 1));
                DebugPackets.sendPoiTicketCountPacket(xd, fk2);
            }
        }
        else if (this.triedCount < 5) {
            this.batchCache.long2LongEntrySet().removeIf(entry -> entry.getLongValue() < this.lastUpdate);
        }
    }
}
