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

import java.util.function.ToIntFunction;
import java.util.Comparator;
import net.minecraft.core.SectionPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.phys.Vec3;
import net.minecraft.core.Vec3i;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.ai.memory.WalkTarget;
import net.minecraft.world.entity.monster.SharedMonsterAttributes;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import java.util.List;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.LivingEntity;

public class BehaviorUtils {
    public static void lockGazeAndWalkToEachOther(final LivingEntity akz1, final LivingEntity akz2) {
        lookAtEachOther(akz1, akz2);
        walkToEachOther(akz1, akz2);
    }
    
    public static boolean entityIsVisible(final Brain<?> alp, final LivingEntity akz) {
        return alp.<List<LivingEntity>>getMemory(MemoryModuleType.VISIBLE_LIVING_ENTITIES).filter(list -> list.contains(akz)).isPresent();
    }
    
    public static boolean targetIsValid(final Brain<?> alp, final MemoryModuleType<? extends LivingEntity> arl, final EntityType<?> aku) {
        return alp.getMemory(arl).filter(akz -> akz.getType() == aku).filter(LivingEntity::isAlive).filter(akz -> entityIsVisible(alp, akz)).isPresent();
    }
    
    public static void lookAtEachOther(final LivingEntity akz1, final LivingEntity akz2) {
        lookAtEntity(akz1, akz2);
        lookAtEntity(akz2, akz1);
    }
    
    public static void lookAtEntity(final LivingEntity akz1, final LivingEntity akz2) {
        akz1.getBrain().<EntityPosWrapper>setMemory((MemoryModuleType<EntityPosWrapper>)MemoryModuleType.LOOK_TARGET, new EntityPosWrapper(akz2));
    }
    
    public static void walkToEachOther(final LivingEntity akz1, final LivingEntity akz2) {
        final int integer3 = 2;
        walkToEntity(akz1, akz2, 2);
        walkToEntity(akz2, akz1, 2);
    }
    
    public static void walkToEntity(final LivingEntity akz1, final LivingEntity akz2, final int integer) {
        final float float4 = (float)akz1.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).getValue();
        final EntityPosWrapper amg5 = new EntityPosWrapper(akz2);
        final WalkTarget arn6 = new WalkTarget(amg5, float4, integer);
        akz1.getBrain().<PositionWrapper>setMemory(MemoryModuleType.LOOK_TARGET, amg5);
        akz1.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, arn6);
    }
    
    public static void throwItem(final LivingEntity akz1, final ItemStack ben, final LivingEntity akz3) {
        final double double4 = akz1.getEyeY() - 0.30000001192092896;
        final ItemEntity awb6 = new ItemEntity(akz1.level, akz1.getX(), double4, akz1.getZ(), ben);
        final BlockPos fk7 = new BlockPos(akz3);
        final BlockPos fk8 = new BlockPos(akz1);
        final float float9 = 0.3f;
        Vec3 cvl10 = new Vec3(fk7.subtract(fk8));
        cvl10 = cvl10.normalize().scale(0.30000001192092896);
        awb6.setDeltaMovement(cvl10);
        awb6.setDefaultPickUpDelay();
        akz1.level.addFreshEntity(awb6);
    }
    
    public static SectionPos findSectionClosestToVillage(final ServerLevel xd, final SectionPos gd, final int integer) {
        final int integer2 = xd.sectionsToVillage(gd);
        return SectionPos.cube(gd, integer).filter(gd -> xd.sectionsToVillage(gd) < integer2).min(Comparator.comparingInt(xd::sectionsToVillage)).orElse(gd);
    }
}
