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

import net.minecraft.world.entity.ai.village.poi.PoiType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.core.Registry;
import java.util.Optional;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Position;
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 AssignProfessionFromJobSite extends Behavior<Villager> {
    public AssignProfessionFromJobSite() {
        super(ImmutableMap.<MemoryModuleType<GlobalPos>, MemoryStatus>of(MemoryModuleType.POTENTIAL_JOB_SITE, MemoryStatus.VALUE_PRESENT));
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final Villager bdp) {
        final BlockPos fu4 = bdp.getBrain().<GlobalPos>getMemory(MemoryModuleType.POTENTIAL_JOB_SITE).get().pos();
        return fu4.closerThan(bdp.position(), 2.0) || bdp.assignProfessionWhenSpawned();
    }
    
    @Override
    protected void start(final ServerLevel zd, final Villager bdp, final long long3) {
        final GlobalPos gc6 = bdp.getBrain().<GlobalPos>getMemory(MemoryModuleType.POTENTIAL_JOB_SITE).get();
        bdp.getBrain().<GlobalPos>eraseMemory(MemoryModuleType.POTENTIAL_JOB_SITE);
        bdp.getBrain().<GlobalPos>setMemory(MemoryModuleType.JOB_SITE, gc6);
        if (bdp.getVillagerData().getProfession() != VillagerProfession.NONE) {
            return;
        }
        final MinecraftServer minecraftServer7 = zd.getServer();
        Optional.<ServerLevel>ofNullable(minecraftServer7.getLevel(gc6.dimension())).flatMap(zd -> zd.getPoiManager().getType(gc6.pos())).flatMap(ayc -> Registry.VILLAGER_PROFESSION.stream().filter(bds -> bds.getJobPoiType() == ayc).findFirst()).ifPresent(bds -> {
            bdp.setVillagerData(bdp.getVillagerData().setProfession(bds));
            bdp.refreshBrain(zd);
        });
    }
}
