package dev.crucifixbeta.api.utils.entity;

import asm.accessors.ILivingEntity;
import com.mojang.authlib.GameProfile;
import dev.crucifixbeta.api.utils.Wrapper;
import dev.crucifixbeta.api.utils.math.PredictUtil;
import java.util.ArrayList;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.player.PlayerEntity;

@Environment(EnvType.CLIENT)
public class CopyPlayerEntity extends PlayerEntity {
   private final boolean onGround;

   public CopyPlayerEntity(PlayerEntity player) {
      this(player, false, 0.0, 0, 0, false, false, false, false);
   }

   public CopyPlayerEntity(PlayerEntity player, boolean effect) {
      super(Wrapper.mc.world, player.getBlockPos(), player.getYaw(), new GameProfile(player.getGameProfile().getId(), player.getGameProfile().getName()));
      this.copyPositionAndRotation(player);
      this.prevX = player.prevX;
      this.prevZ = player.prevZ;
      this.prevY = player.prevY;
      this.bodyYaw = player.bodyYaw;
      this.headYaw = player.headYaw;
      this.handSwingProgress = player.handSwingProgress;
      this.handSwingTicks = player.handSwingTicks;
      this.limbAnimator.setSpeed(player.limbAnimator.getSpeed());
      this.limbAnimator.pos = player.limbAnimator.getPos();
      ((ILivingEntity)this).setLeaningPitch(((ILivingEntity)player).getLeaningPitch());
      ((ILivingEntity)this).setLastLeaningPitch(((ILivingEntity)player).getLeaningPitch());
      this.touchingWater = player.isTouchingWater();
      this.setSneaking(player.isSneaking());
      this.setPose(player.getPose());
      this.setFlag(7, player.isFallFlying());
      this.onGround = player.isOnGround();
      this.setOnGround(this.onGround);
      this.setVelocity(player.getVelocity());
      this.getInventory().clone(player.getInventory());
      if (effect) {
         for (StatusEffectInstance var4 : new ArrayList<>(player.getStatusEffects())) {
            this.addStatusEffect(var4);
         }
      }

      this.setAbsorptionAmountUnclamped(player.getAbsorptionAmount());
      this.setHealth(player.getHealth());
      this.setBoundingBox(player.getBoundingBox());
   }

   public CopyPlayerEntity(
      PlayerEntity player, boolean effect, double maxMotionY, int ticks, int simulation, boolean step, boolean doubleStep, boolean jump, boolean inBlockPause
   ) {
      this(player, effect);
      if (ticks > 0) {
         this.setPosition(PredictUtil.getPos(player, maxMotionY, ticks, simulation, step, doubleStep, jump, inBlockPause));
      }
   }

   @Override
   public boolean isOnGround() {
      return this.onGround;
   }

   @Override
   public boolean isSpectator() {
      return false;
   }

   @Override
   public boolean isCreative() {
      return false;
   }
}
