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

import net.minecraft.world.entity.LivingEntity;
import java.util.Optional;
import net.minecraft.world.entity.ai.Brain;
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 WorkAtPoi extends Behavior<Villager> {
    private long lastCheck;
    
    public WorkAtPoi() {
        super(ImmutableMap.<MemoryModuleType<GlobalPos>, MemoryStatus>of(MemoryModuleType.JOB_SITE, MemoryStatus.VALUE_PRESENT, (MemoryModuleType<GlobalPos>)MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED));
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final Villager bdp) {
        if (zd.getGameTime() - this.lastCheck < 300L) {
            return false;
        }
        if (zd.random.nextInt(2) != 0) {
            return false;
        }
        this.lastCheck = zd.getGameTime();
        final GlobalPos gc4 = bdp.getBrain().<GlobalPos>getMemory(MemoryModuleType.JOB_SITE).get();
        return gc4.dimension() == zd.dimension() && gc4.pos().closerThan(bdp.position(), 1.73);
    }
    
    @Override
    protected void start(final ServerLevel zd, final Villager bdp, final long long3) {
        final Brain<Villager> apr6 = bdp.getBrain();
        apr6.<Long>setMemory(MemoryModuleType.LAST_WORKED_AT_POI, long3);
        apr6.<GlobalPos>getMemory(MemoryModuleType.JOB_SITE).ifPresent(gc -> apr6.<BlockPosTracker>setMemory((MemoryModuleType<BlockPosTracker>)MemoryModuleType.LOOK_TARGET, new BlockPosTracker(gc.pos())));
        bdp.playWorkSound();
        this.useWorkstation(zd, bdp);
        if (bdp.shouldRestock()) {
            bdp.restock();
        }
    }
    
    protected void useWorkstation(final ServerLevel zd, final Villager bdp) {
    }
    
    @Override
    protected boolean canStillUse(final ServerLevel zd, final Villager bdp, final long long3) {
        final Optional<GlobalPos> optional6 = bdp.getBrain().<GlobalPos>getMemory(MemoryModuleType.JOB_SITE);
        if (!optional6.isPresent()) {
            return false;
        }
        final GlobalPos gc7 = optional6.get();
        return gc7.dimension() == zd.dimension() && gc7.pos().closerThan(bdp.position(), 1.73);
    }
}
