package wtf.opal.utility.misc.math;

import java.util.Objects;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.render.Camera;
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import wtf.opal.client.Constants;

@Environment(EnvType.CLIENT)
public final class MathUtility {
   private MathUtility() {
   }

   public static Number roundAndClamp(Number value, Number minValue, Number maxValue, Number increment) {
      Objects.requireNonNull(value);
      switch (value) {
         case Double var6:
            Double var10 = (double)Math.round(var6 / increment.doubleValue()) * increment.doubleValue();
            return MathHelper.clamp(var10, minValue.doubleValue(), maxValue.doubleValue());
         case Float var7:
            Float var11 = (float)Math.round(var7 / increment.floatValue()) * increment.floatValue();
            return MathHelper.clamp(var11, minValue.floatValue(), maxValue.floatValue());
         case Long var8:
            Long var12 = (long)Math.round((float)var8.longValue() / (float)increment.longValue()) * increment.longValue();
            return MathHelper.clamp(var12, minValue.longValue(), maxValue.longValue());
         default:
            int var9 = value.intValue();
            var9 = Math.round((float)var9 / (float)increment.intValue()) * increment.intValue();
            var9 = MathHelper.clamp(var9, minValue.intValue(), maxValue.intValue());
            return var9;
      }
   }

   public static Vec3d interpolate(LivingEntity entity, float tickDelta) {
      Camera var2 = Constants.mc.gameRenderer.getCamera();
      return entity.getPos()
         .add(
            MathHelper.lerp((double)tickDelta, entity.lastRenderX, entity.getX()) - entity.getX(),
            MathHelper.lerp((double)tickDelta, entity.lastRenderY, entity.getY()) - entity.getY(),
            MathHelper.lerp((double)tickDelta, entity.lastRenderZ, entity.getZ()) - entity.getZ()
         )
         .subtract(var2.getPos());
   }

   public static double interpolate(double a, double b, double v) {
      return a + (b - a) * v;
   }

   public static float interpolate(float a, float b, float v) {
      return a + (b - a) * v;
   }

   public static long interpolate(long a, long b, long v) {
      return a + (b - a) * v;
   }

   public static int interpolate(int a, int b, int v) {
      return a + (b - a) * v;
   }
}
