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

import net.minecraft.world.entity.ai.memory.WalkTarget;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.network.protocol.game.DebugPackets;
import net.minecraft.world.entity.LivingEntity;
import java.util.Optional;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.ai.village.poi.PoiType;
import net.minecraft.world.entity.npc.VillagerProfession;
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 YieldJobSite extends Behavior<Villager> {
    private final float speedModifier;
    
    public YieldJobSite(final float float1) {
        super(ImmutableMap.<MemoryModuleType<GlobalPos>, MemoryStatus>of(MemoryModuleType.POTENTIAL_JOB_SITE, MemoryStatus.VALUE_PRESENT, MemoryModuleType.JOB_SITE, MemoryStatus.VALUE_ABSENT, (MemoryModuleType<GlobalPos>)MemoryModuleType.LIVING_ENTITIES, MemoryStatus.VALUE_PRESENT));
        this.speedModifier = float1;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final Villager bdp) {
        return !bdp.isBaby() && bdp.getVillagerData().getProfession() == VillagerProfession.NONE;
    }
    
    @Override
    protected void start(final ServerLevel zd, final Villager bdp, final long long3) {
        final BlockPos fu6 = bdp.getBrain().<GlobalPos>getMemory(MemoryModuleType.POTENTIAL_JOB_SITE).get().pos();
        final Optional<PoiType> optional7 = zd.getPoiManager().getType(fu6);
        if (!optional7.isPresent()) {
            return;
        }
        BehaviorUtils.getNearbyVillagersWithCondition(bdp, bdp -> this.nearbyWantsJobsite(optional7.get(), bdp, fu6)).findFirst().ifPresent(bdp4 -> this.yieldJobSite(zd, bdp, bdp4, fu6, bdp4.getBrain().<GlobalPos>getMemory(MemoryModuleType.JOB_SITE).isPresent()));
    }
    
    private boolean nearbyWantsJobsite(final PoiType ayc, final Villager bdp, final BlockPos fu) {
        final boolean boolean5 = bdp.getBrain().<GlobalPos>getMemory(MemoryModuleType.POTENTIAL_JOB_SITE).isPresent();
        if (boolean5) {
            return false;
        }
        final Optional<GlobalPos> optional6 = bdp.getBrain().<GlobalPos>getMemory(MemoryModuleType.JOB_SITE);
        final VillagerProfession bds7 = bdp.getVillagerData().getProfession();
        if (bdp.getVillagerData().getProfession() == VillagerProfession.NONE || !bds7.getJobPoiType().getPredicate().test(ayc)) {
            return false;
        }
        if (!optional6.isPresent()) {
            return this.canReachPos(bdp, fu, ayc);
        }
        return optional6.get().pos().equals(fu);
    }
    
    private void yieldJobSite(final ServerLevel zd, final Villager bdp2, final Villager bdp3, final BlockPos fu, final boolean boolean5) {
        this.eraseMemories(bdp2);
        if (!boolean5) {
            BehaviorUtils.setWalkAndLookTargetMemories(bdp3, fu, this.speedModifier, 1);
            bdp3.getBrain().<GlobalPos>setMemory(MemoryModuleType.POTENTIAL_JOB_SITE, GlobalPos.of(zd.dimension(), fu));
            DebugPackets.sendPoiTicketCountPacket(zd, fu);
        }
    }
    
    private boolean canReachPos(final Villager bdp, final BlockPos fu, final PoiType ayc) {
        final Path czf5 = bdp.getNavigation().createPath(fu, ayc.getValidRange());
        return czf5 != null && czf5.canReach();
    }
    
    private void eraseMemories(final Villager bdp) {
        bdp.getBrain().<WalkTarget>eraseMemory(MemoryModuleType.WALK_TARGET);
        bdp.getBrain().<PositionTracker>eraseMemory(MemoryModuleType.LOOK_TARGET);
        bdp.getBrain().<GlobalPos>eraseMemory(MemoryModuleType.POTENTIAL_JOB_SITE);
    }
}
