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

import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.core.GlobalPos;
import net.minecraft.core.Position;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.ai.village.poi.PoiType;
import net.minecraft.server.level.ServerLevel;
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 net.minecraft.core.BlockPos;
import java.util.Optional;
import net.minecraft.world.entity.LivingEntity;

public class LocateHidingPlace extends Behavior<LivingEntity> {
    private final float speed;
    private final int radius;
    private final int closeEnoughDist;
    private Optional<BlockPos> currentPos;
    
    public LocateHidingPlace(final int integer1, final float float2, final int integer3) {
        super(ImmutableMap.<MemoryModuleType<WalkTarget>, MemoryStatus>of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT, (MemoryModuleType<WalkTarget>)MemoryModuleType.HOME, MemoryStatus.REGISTERED, (MemoryModuleType<WalkTarget>)MemoryModuleType.HIDING_PLACE, MemoryStatus.REGISTERED));
        this.currentPos = Optional.<BlockPos>empty();
        this.radius = integer1;
        this.speed = float2;
        this.closeEnoughDist = integer3;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel xd, final LivingEntity akz) {
        final Optional<BlockPos> optional4 = xd.getPoiManager().find(asu -> asu == PoiType.HOME, fk -> true, new BlockPos(akz), this.closeEnoughDist + 1, PoiManager.Occupancy.ANY);
        if (optional4.isPresent() && optional4.get().closerThan(akz.position(), this.closeEnoughDist)) {
            this.currentPos = optional4;
        }
        else {
            this.currentPos = Optional.<BlockPos>empty();
        }
        return true;
    }
    
    @Override
    protected void start(final ServerLevel xd, final LivingEntity akz, final long long3) {
        final Brain<?> alp6 = akz.getBrain();
        Optional<BlockPos> optional7 = this.currentPos;
        if (!optional7.isPresent()) {
            optional7 = xd.getPoiManager().getRandom(asu -> asu == PoiType.HOME, fk -> true, PoiManager.Occupancy.ANY, new BlockPos(akz), this.radius, akz.getRandom());
            if (!optional7.isPresent()) {
                final Optional<GlobalPos> optional8 = alp6.<GlobalPos>getMemory(MemoryModuleType.HOME);
                if (optional8.isPresent()) {
                    optional7 = Optional.<BlockPos>of(optional8.get().pos());
                }
            }
        }
        if (optional7.isPresent()) {
            alp6.<Path>eraseMemory(MemoryModuleType.PATH);
            alp6.<PositionWrapper>eraseMemory(MemoryModuleType.LOOK_TARGET);
            alp6.<Villager>eraseMemory(MemoryModuleType.BREED_TARGET);
            alp6.<LivingEntity>eraseMemory(MemoryModuleType.INTERACTION_TARGET);
            alp6.<GlobalPos>setMemory(MemoryModuleType.HIDING_PLACE, GlobalPos.of(xd.getDimension().getType(), optional7.get()));
            if (!optional7.get().closerThan(akz.position(), this.closeEnoughDist)) {
                alp6.<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(optional7.get(), this.speed, this.closeEnoughDist));
            }
        }
    }
}
