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.entity.Entity;
import java.util.List;
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.memory.MemoryModuleType;
import java.util.function.Predicate;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;

public class InteractWith<E extends LivingEntity, T extends LivingEntity> extends Behavior<E> {
    private final int maxDist;
    private final float speed;
    private final EntityType<? extends T> type;
    private final int interactionRangeSqr;
    private final Predicate<T> targetFilter;
    private final Predicate<E> selfFilter;
    private final MemoryModuleType<T> memory;
    
    public InteractWith(final EntityType<? extends T> aku, final int integer2, final Predicate<E> predicate3, final Predicate<T> predicate4, final MemoryModuleType<T> arl, final float float6, final int integer7) {
        super(ImmutableMap.<MemoryModuleType<PositionWrapper>, MemoryStatus>of(MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED, (MemoryModuleType<PositionWrapper>)MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT, (MemoryModuleType<PositionWrapper>)arl, MemoryStatus.VALUE_ABSENT, (MemoryModuleType<PositionWrapper>)MemoryModuleType.VISIBLE_LIVING_ENTITIES, MemoryStatus.VALUE_PRESENT));
        this.type = aku;
        this.speed = float6;
        this.interactionRangeSqr = integer2 * integer2;
        this.maxDist = integer7;
        this.targetFilter = predicate4;
        this.selfFilter = predicate3;
        this.memory = arl;
    }
    
    public static <T extends LivingEntity> InteractWith<LivingEntity, T> of(final EntityType<? extends T> aku, final int integer2, final MemoryModuleType<T> arl, final float float4, final int integer5) {
        return new InteractWith<LivingEntity, T>(aku, integer2, akz -> true, akz -> true, arl, float4, integer5);
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel xd, final E akz) {
        return this.selfFilter.test(akz) && akz.getBrain().<List<LivingEntity>>getMemory(MemoryModuleType.VISIBLE_LIVING_ENTITIES).get().stream().anyMatch(akz -> this.type.equals(akz.getType()) && this.targetFilter.test((T)akz));
    }
    
    @Override
    protected void start(final ServerLevel xd, final E akz, final long long3) {
        final Brain<?> alp6 = akz.getBrain();
        final Brain brain;
        final MemoryModuleType walk_TARGET;
        final Object object;
        alp6.<List<LivingEntity>>getMemory(MemoryModuleType.VISIBLE_LIVING_ENTITIES).ifPresent(list -> list.stream().filter(akz -> this.type.equals(akz.getType())).map(akz -> akz).filter(akz2 -> akz2.distanceToSqr(akz) <= this.interactionRangeSqr).filter(this.targetFilter).findFirst().ifPresent(akz -> {
            brain.setMemory(this.memory, akz);
            brain.setMemory(MemoryModuleType.LOOK_TARGET, new EntityPosWrapper(akz));
            walk_TARGET = MemoryModuleType.WALK_TARGET;
            new WalkTarget(new EntityPosWrapper(akz), this.speed, this.maxDist);
            brain.setMemory(walk_TARGET, object);
        }));
    }
}
