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

import net.minecraft.world.entity.ai.village.poi.PoiType;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.core.BlockPos;
import java.util.Optional;
import net.minecraft.network.protocol.game.DebugPackets;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.schedule.Activity;
import net.minecraft.server.level.ServerLevel;
import java.util.Map;
import net.minecraft.core.GlobalPos;
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.world.entity.npc.Villager;

public class GoToPotentialJobSite extends Behavior<Villager> {
    final float speedModifier;
    
    public GoToPotentialJobSite(final float float1) {
        super(ImmutableMap.<MemoryModuleType<GlobalPos>, MemoryStatus>of(MemoryModuleType.POTENTIAL_JOB_SITE, MemoryStatus.VALUE_PRESENT), 1200);
        this.speedModifier = float1;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final Villager bdp) {
        return bdp.getBrain().getActiveNonCoreActivity().<Boolean>map(bfl -> bfl == Activity.IDLE || bfl == Activity.WORK || bfl == Activity.PLAY).orElse(true);
    }
    
    @Override
    protected boolean canStillUse(final ServerLevel zd, final Villager bdp, final long long3) {
        return bdp.getBrain().hasMemoryValue(MemoryModuleType.POTENTIAL_JOB_SITE);
    }
    
    @Override
    protected void tick(final ServerLevel zd, final Villager bdp, final long long3) {
        BehaviorUtils.setWalkAndLookTargetMemories(bdp, bdp.getBrain().<GlobalPos>getMemory(MemoryModuleType.POTENTIAL_JOB_SITE).get().pos(), this.speedModifier, 1);
    }
    
    @Override
    protected void stop(final ServerLevel zd, final Villager bdp, final long long3) {
        final Optional<GlobalPos> optional6 = bdp.getBrain().<GlobalPos>getMemory(MemoryModuleType.POTENTIAL_JOB_SITE);
        final BlockPos fu3;
        final ServerLevel zd2;
        PoiManager axz5;
        optional6.ifPresent(gc -> {
            fu3 = gc.pos();
            zd2 = zd.getServer().getLevel(gc.dimension());
            if (zd2 == null) {
                return;
            }
            else {
                axz5 = zd2.getPoiManager();
                if (axz5.exists(fu3, ayc -> true)) {
                    axz5.release(fu3);
                }
                DebugPackets.sendPoiTicketCountPacket(zd, fu3);
                return;
            }
        });
        bdp.getBrain().<GlobalPos>eraseMemory(MemoryModuleType.POTENTIAL_JOB_SITE);
    }
}
