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

import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.ai.memory.WalkTarget;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.schedule.Activity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.server.level.ServerLevel;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.npc.Villager;

public class VillagerPanicTrigger extends Behavior<Villager> {
    public VillagerPanicTrigger() {
        super(ImmutableMap.of());
    }
    
    @Override
    protected boolean canStillUse(final ServerLevel xd, final Villager axw, final long long3) {
        return isHurt(axw) || hasHostile(axw);
    }
    
    @Override
    protected void start(final ServerLevel xd, final Villager axw, final long long3) {
        if (isHurt(axw) || hasHostile(axw)) {
            final Brain<?> alp6 = axw.getBrain();
            if (!alp6.isActive(Activity.PANIC)) {
                alp6.<Path>eraseMemory(MemoryModuleType.PATH);
                alp6.<WalkTarget>eraseMemory(MemoryModuleType.WALK_TARGET);
                alp6.<PositionWrapper>eraseMemory(MemoryModuleType.LOOK_TARGET);
                alp6.<Villager>eraseMemory(MemoryModuleType.BREED_TARGET);
                alp6.<LivingEntity>eraseMemory(MemoryModuleType.INTERACTION_TARGET);
            }
            alp6.setActivity(Activity.PANIC);
        }
    }
    
    @Override
    protected void tick(final ServerLevel xd, final Villager axw, final long long3) {
        if (long3 % 100L == 0L) {
            axw.spawnGolemIfNeeded(long3, 3);
        }
    }
    
    public static boolean hasHostile(final LivingEntity akz) {
        return akz.getBrain().hasMemoryValue(MemoryModuleType.NEAREST_HOSTILE);
    }
    
    public static boolean isHurt(final LivingEntity akz) {
        return akz.getBrain().hasMemoryValue(MemoryModuleType.HURT_BY);
    }
}
