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

import net.minecraft.world.entity.LivingEntity;
import java.util.Optional;
import net.minecraft.core.Position;
import java.util.Objects;
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 javax.annotation.Nullable;
import net.minecraft.core.GlobalPos;
import java.util.List;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.npc.Villager;

public class StrollToPoiList extends Behavior<Villager> {
    private final MemoryModuleType<List<GlobalPos>> strollToMemoryType;
    private final MemoryModuleType<GlobalPos> mustBeCloseToMemoryType;
    private final float speed;
    private final int closeEnoughDist;
    private final int maxDistanceFromPoi;
    private long nextOkStartTime;
    @Nullable
    private GlobalPos targetPos;
    
    public StrollToPoiList(final MemoryModuleType<List<GlobalPos>> arl1, final float float2, final int integer3, final int integer4, final MemoryModuleType<GlobalPos> arl5) {
        super(ImmutableMap.<MemoryModuleType<WalkTarget>, MemoryStatus>of(MemoryModuleType.WALK_TARGET, MemoryStatus.REGISTERED, (MemoryModuleType<WalkTarget>)arl1, MemoryStatus.VALUE_PRESENT, (MemoryModuleType<WalkTarget>)arl5, MemoryStatus.VALUE_PRESENT));
        this.strollToMemoryType = arl1;
        this.speed = float2;
        this.closeEnoughDist = integer3;
        this.maxDistanceFromPoi = integer4;
        this.mustBeCloseToMemoryType = arl5;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel xd, final Villager axw) {
        final Optional<List<GlobalPos>> optional4 = axw.getBrain().<List<GlobalPos>>getMemory(this.strollToMemoryType);
        final Optional<GlobalPos> optional5 = axw.getBrain().<GlobalPos>getMemory(this.mustBeCloseToMemoryType);
        if (optional4.isPresent() && optional5.isPresent()) {
            final List<GlobalPos> list6 = optional4.get();
            if (!list6.isEmpty()) {
                this.targetPos = list6.get(xd.getRandom().nextInt(list6.size()));
                return this.targetPos != null && Objects.equals(xd.getDimension().getType(), this.targetPos.dimension()) && optional5.get().pos().closerThan(axw.position(), this.maxDistanceFromPoi);
            }
        }
        return false;
    }
    
    @Override
    protected void start(final ServerLevel xd, final Villager axw, final long long3) {
        if (long3 > this.nextOkStartTime && this.targetPos != null) {
            axw.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(this.targetPos.pos(), this.speed, this.closeEnoughDist));
            this.nextOkStartTime = long3 + 100L;
        }
    }
}
