package dev.crucifixbeta.api.utils.render;

import com.mojang.blaze3d.systems.RenderSystem;
import dev.crucifixbeta.api.utils.Wrapper;
import java.awt.Color;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BufferRenderer;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.render.VertexFormat.DrawMode;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;

@Environment(EnvType.CLIENT)
public class JelloUtil implements Wrapper {
   private static float prevCircleStep;
   private static float circleStep;

   public static void drawJello(MatrixStack matrix, Entity target, Color color) {
      double var3 = (double)(prevCircleStep + (circleStep - prevCircleStep) * mc.getRenderTickCounter().getTickDelta(true));
      double var5 = absSinAnimation(var3 - 0.45F);
      double var7 = absSinAnimation(var3);
      double var9 = target.prevX
         + (target.getX() - target.prevX) * (double)mc.getRenderTickCounter().getTickDelta(true)
         - mc.getEntityRenderDispatcher().camera.getPos().getX();
      double var11 = target.prevY
         + (target.getY() - target.prevY) * (double)mc.getRenderTickCounter().getTickDelta(true)
         - mc.getEntityRenderDispatcher().camera.getPos().getY()
         + var5 * (double)target.getHeight();
      double var13 = target.prevZ
         + (target.getZ() - target.prevZ) * (double)mc.getRenderTickCounter().getTickDelta(true)
         - mc.getEntityRenderDispatcher().camera.getPos().getZ();
      double var15 = target.prevY
         + (target.getY() - target.prevY) * (double)mc.getRenderTickCounter().getTickDelta(true)
         - mc.getEntityRenderDispatcher().camera.getPos().getY()
         + var7 * (double)target.getHeight();
      matrix.push();
      RenderSystem.enableBlend();
      RenderSystem.disableCull();
      RenderSystem.disableDepthTest();
      Tessellator var17 = Tessellator.getInstance();
      BufferBuilder var18 = var17.begin(DrawMode.TRIANGLE_STRIP, VertexFormats.POSITION_COLOR);
      RenderSystem.setShader(GameRenderer::getPositionColorProgram);

      for (int var19 = 0; var19 <= 30; var19++) {
         float var20 = (float)(
            var9
               + Math.cos((double)var19 * 6.28 / 30.0)
                  * (target.getBoundingBox().maxX - target.getBoundingBox().minX + (target.getBoundingBox().maxZ - target.getBoundingBox().minZ))
                  * 0.5
         );
         float var21 = (float)(
            var13
               + Math.sin((double)var19 * 6.28 / 30.0)
                  * (target.getBoundingBox().maxX - target.getBoundingBox().minX + (target.getBoundingBox().maxZ - target.getBoundingBox().minZ))
                  * 0.5
         );
         var18.vertex(matrix.peek().getPositionMatrix(), var20, (float)var15, var21).color(color.getRGB());
         var18.vertex(matrix.peek().getPositionMatrix(), var20, (float)var11, var21).color(ColorUtil.injectAlpha(color, 0).getRGB());
      }

      BufferRenderer.drawWithGlobalProgram(var18.end());
      RenderSystem.enableCull();
      RenderSystem.disableBlend();
      RenderSystem.enableDepthTest();
      matrix.pop();
   }

   public static void updateJello() {
      prevCircleStep = circleStep;
      circleStep += 0.15F;
   }

   private static double absSinAnimation(double input) {
      return Math.abs(1.0 + Math.sin(input)) / 2.0;
   }
}
