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

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.BedBlock;
import net.minecraft.tags.BlockTags;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.ai.Brain;
import net.minecraft.network.protocol.game.DebugPackets;
import net.minecraft.core.Position;
import java.util.Objects;
import net.minecraft.server.level.ServerLevel;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.village.poi.PoiType;
import java.util.function.Predicate;
import net.minecraft.core.GlobalPos;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.LivingEntity;

public class ValidateNearbyPoi extends Behavior<LivingEntity> {
    private final MemoryModuleType<GlobalPos> memoryType;
    private final Predicate<PoiType> poiPredicate;
    
    public ValidateNearbyPoi(final PoiType asu, final MemoryModuleType<GlobalPos> arl) {
        super(ImmutableMap.<MemoryModuleType<GlobalPos>, MemoryStatus>of(arl, MemoryStatus.VALUE_PRESENT));
        this.poiPredicate = asu.getPredicate();
        this.memoryType = arl;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel xd, final LivingEntity akz) {
        final GlobalPos fr4 = akz.getBrain().<GlobalPos>getMemory(this.memoryType).get();
        return Objects.equals(xd.getDimension().getType(), fr4.dimension()) && fr4.pos().closerThan(akz.position(), 5.0);
    }
    
    @Override
    protected void start(final ServerLevel xd, final LivingEntity akz, final long long3) {
        final Brain<?> alp6 = akz.getBrain();
        final GlobalPos fr7 = alp6.<GlobalPos>getMemory(this.memoryType).get();
        final BlockPos fk8 = fr7.pos();
        final ServerLevel xd2 = xd.getServer().getLevel(fr7.dimension());
        if (this.poiDoesntExist(xd2, fk8)) {
            alp6.<GlobalPos>eraseMemory(this.memoryType);
        }
        else if (this.bedIsOccupied(xd2, fk8, akz)) {
            alp6.<GlobalPos>eraseMemory(this.memoryType);
            xd.getPoiManager().release(fk8);
            DebugPackets.sendPoiTicketCountPacket(xd, fk8);
        }
    }
    
    private boolean bedIsOccupied(final ServerLevel xd, final BlockPos fk, final LivingEntity akz) {
        final BlockState byj5 = xd.getBlockState(fk);
        return byj5.getBlock().is(BlockTags.BEDS) && byj5.<Boolean>getValue((Property<Boolean>)BedBlock.OCCUPIED) && !akz.isSleeping();
    }
    
    private boolean poiDoesntExist(final ServerLevel xd, final BlockPos fk) {
        return !xd.getPoiManager().exists(fk, this.poiPredicate);
    }
}
