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 speedModifier;
    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> aoq, final int integer2, final Predicate<E> predicate3, final Predicate<T> predicate4, final MemoryModuleType<T> awp, final float float6, final int integer7) {
        super(ImmutableMap.<MemoryModuleType<PositionTracker>, MemoryStatus>of(MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED, (MemoryModuleType<PositionTracker>)MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT, (MemoryModuleType<PositionTracker>)MemoryModuleType.VISIBLE_LIVING_ENTITIES, MemoryStatus.VALUE_PRESENT));
        this.type = aoq;
        this.speedModifier = float6;
        this.interactionRangeSqr = integer2 * integer2;
        this.maxDist = integer7;
        this.targetFilter = predicate4;
        this.selfFilter = predicate3;
        this.memory = awp;
    }
    
    public static <T extends LivingEntity> InteractWith<LivingEntity, T> of(final EntityType<? extends T> aoq, final int integer2, final MemoryModuleType<T> awp, final float float4, final int integer5) {
        return new InteractWith<LivingEntity, T>(aoq, integer2, aoy -> true, aoy -> true, awp, float4, integer5);
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final E aoy) {
        return this.selfFilter.test(aoy) && this.seesAtLeastOneValidTarget(aoy);
    }
    
    private boolean seesAtLeastOneValidTarget(final E aoy) {
        final List<LivingEntity> list3 = aoy.getBrain().<List<LivingEntity>>getMemory(MemoryModuleType.VISIBLE_LIVING_ENTITIES).get();
        return list3.stream().anyMatch(this::isTargetValid);
    }
    
    private boolean isTargetValid(final LivingEntity aoy) {
        return this.type.equals(aoy.getType()) && this.targetFilter.test((T)aoy);
    }
    
    @Override
    protected void start(final ServerLevel zd, final E aoy, final long long3) {
        final Brain<?> apr6 = aoy.getBrain();
        final Brain brain;
        final MemoryModuleType walk_TARGET;
        final Object object;
        apr6.<List<LivingEntity>>getMemory(MemoryModuleType.VISIBLE_LIVING_ENTITIES).ifPresent(list -> list.stream().filter(aoy -> this.type.equals(aoy.getType())).map(aoy -> aoy).filter(aoy2 -> aoy2.distanceToSqr(aoy) <= this.interactionRangeSqr).filter(this.targetFilter).findFirst().ifPresent(aoy -> {
            brain.setMemory(this.memory, aoy);
            brain.setMemory(MemoryModuleType.LOOK_TARGET, new EntityTracker(aoy, true));
            walk_TARGET = MemoryModuleType.WALK_TARGET;
            new WalkTarget(new EntityTracker(aoy, false), this.speedModifier, this.maxDist);
            brain.setMemory(walk_TARGET, object);
        }));
    }
}
