package dev.crucifixbeta.api.utils.combat.crystal;

import java.util.ArrayList;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.decoration.EndCrystalEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;

@Environment(EnvType.CLIENT)
public record CrystalEntitySnapshot(
   int id,
   String name,
   EntityType<?> type,
   Vec3d pos,
   Vec3d eyePos,
   Vec3d velocity,
   BlockPos blockPos,
   Box boundingBox,
   EntityDimensions dimensions,
   int age,
   boolean alive,
   boolean item,
   boolean endCrystal,
   boolean living,
   float totalHealth,
   int armor,
   float armorToughness,
   int resistanceLevel,
   List<ItemStack> armorItems,
   Item mainHand
) {
   public static CrystalEntitySnapshot of(Entity entity) {
      boolean var1 = entity instanceof LivingEntity;
      LivingEntity var2 = var1 ? (LivingEntity)entity : null;
      ArrayList var3 = new ArrayList();
      if (var2 != null) {
         for (ItemStack var5 : var2.getArmorItems()) {
            var3.add(var5.copy());
         }
      }

      return new CrystalEntitySnapshot(
         entity.getId(),
         entity.getName().getString(),
         entity.getType(),
         entity.getPos(),
         entity.getEyePos(),
         new Vec3d(entity.getX() - entity.prevX, entity.getY() - entity.prevY, entity.getZ() - entity.prevZ),
         entity.getBlockPos(),
         entity.getBoundingBox(),
         entity.getDimensions(entity.getPose()),
         entity.age,
         entity.isAlive(),
         entity instanceof ItemEntity,
         entity instanceof EndCrystalEntity,
         var1,
         var2 == null ? 0.0F : var2.getHealth() + var2.getAbsorptionAmount(),
         var2 == null ? 0 : var2.getArmor(),
         var2 == null ? 0.0F : (float)var2.getAttributeValue(EntityAttributes.GENERIC_ARMOR_TOUGHNESS),
         resistanceLevel(var2),
         var3,
         var2 == null ? Items.AIR : var2.getMainHandStack().getItem()
      );
   }

   private static int resistanceLevel(LivingEntity entity) {
      if (entity == null) {
         return 0;
      } else {
         StatusEffectInstance var1 = entity.getStatusEffect(StatusEffects.RESISTANCE);
         return var1 == null ? 0 : var1.getAmplifier() + 1;
      }
   }

   public boolean intersects(Box box) {
      return this.boundingBox.intersects(box);
   }
}
