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

import net.minecraft.world.entity.LivingEntity;
import java.util.Optional;
import net.minecraft.core.Position;
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 speedModifier;
    private final int closeEnoughDist;
    private final int maxDistanceFromPoi;
    private long nextOkStartTime;
    @Nullable
    private GlobalPos targetPos;
    
    public StrollToPoiList(final MemoryModuleType<List<GlobalPos>> awp1, final float float2, final int integer3, final int integer4, final MemoryModuleType<GlobalPos> awp5) {
        super(ImmutableMap.<MemoryModuleType<WalkTarget>, MemoryStatus>of(MemoryModuleType.WALK_TARGET, MemoryStatus.REGISTERED, (MemoryModuleType<WalkTarget>)awp1, MemoryStatus.VALUE_PRESENT, (MemoryModuleType<WalkTarget>)awp5, MemoryStatus.VALUE_PRESENT));
        this.strollToMemoryType = awp1;
        this.speedModifier = float2;
        this.closeEnoughDist = integer3;
        this.maxDistanceFromPoi = integer4;
        this.mustBeCloseToMemoryType = awp5;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final Villager bdp) {
        final Optional<List<GlobalPos>> optional4 = bdp.getBrain().<List<GlobalPos>>getMemory(this.strollToMemoryType);
        final Optional<GlobalPos> optional5 = bdp.getBrain().<GlobalPos>getMemory(this.mustBeCloseToMemoryType);
        if (optional4.isPresent() && optional5.isPresent()) {
            final List<GlobalPos> list6 = optional4.get();
            if (!list6.isEmpty()) {
                this.targetPos = list6.get(zd.getRandom().nextInt(list6.size()));
                return this.targetPos != null && zd.dimension() == this.targetPos.dimension() && optional5.get().pos().closerThan(bdp.position(), this.maxDistanceFromPoi);
            }
        }
        return false;
    }
    
    @Override
    protected void start(final ServerLevel zd, final Villager bdp, final long long3) {
        if (long3 > this.nextOkStartTime && this.targetPos != null) {
            bdp.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(this.targetPos.pos(), this.speedModifier, this.closeEnoughDist));
            this.nextOkStartTime = long3 + 100L;
        }
    }
}
