package wtf.opal.client.renderer.world;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.util.math.MatrixStack.Entry;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import org.joml.Vector3f;
import wtf.opal.client.Constants;
import wtf.opal.utility.render.ColorUtility;
import wtf.opal.utility.render.Emitter;

@Environment(EnvType.CLIENT)
public record WorldRenderer(Camera camera, VertexConsumerProvider vcp) {
   public WorldRenderer(VertexConsumerProvider vcp) {
      this(Constants.mc.gameRenderer.getCamera(), vcp);
   }

   public void drawFilledQuad(MatrixStack stack, RenderLayer layer, Vec3d position, Vec2f dimensions, int color) {
      VertexConsumer var6 = this.vcp.getBuffer(layer);
      Entry var7 = stack.peek();
      Vector3f var8 = position.subtract(this.camera.getPos()).toVector3f();
      float var9 = var8.x;
      float var10 = var8.y;
      float var11 = var8.z;
      int[] var12 = ColorUtility.hexToRGBA(color);
      float var13 = (float)var12[0] / 255.0F;
      float var14 = (float)var12[1] / 255.0F;
      float var15 = (float)var12[2] / 255.0F;
      float var16 = (float)var12[3] / 255.0F;
      Emitter._emit_quad__4xposition_color(
         var7,
         var6,
         var9 + 0.0F,
         var10 + 0.0F,
         var11 + 0.0F,
         var13,
         var14,
         var15,
         var16,
         var9 + dimensions.x,
         var10 + 0.0F,
         var11 + 0.0F,
         var13,
         var14,
         var15,
         var16,
         var9 + dimensions.x,
         var10 + dimensions.y,
         var11 + 0.0F,
         var13,
         var14,
         var15,
         var16,
         var9 + 0.0F,
         var10 + dimensions.y,
         var11 + 0.0F,
         var13,
         var14,
         var15,
         var16
      );
   }

   public void drawFilledCube(MatrixStack stack, RenderLayer layer, Vec3d position, Vec3d dimensions, int color) {
      VertexConsumer var6 = this.vcp.getBuffer(layer);
      Entry var7 = stack.peek();
      Vector3f var8 = position.subtract(this.camera.getPos()).toVector3f();
      float var9 = var8.x;
      float var10 = var8.y;
      float var11 = var8.z;
      int[] var12 = ColorUtility.hexToRGBA(color);
      float var13 = (float)var12[0] / 255.0F;
      float var14 = (float)var12[1] / 255.0F;
      float var15 = (float)var12[2] / 255.0F;
      float var16 = (float)var12[3] / 255.0F;
      Emitter._emit_cube__8xposition_color(
         var7,
         var6,
         var9 + 0.0F,
         var10 + 0.0F,
         var11 + 0.0F,
         var13,
         var14,
         var15,
         var16,
         (float)((double)var9 + dimensions.getX()),
         var10 + 0.0F,
         var11 + 0.0F,
         var13,
         var14,
         var15,
         var16,
         (float)((double)var9 + dimensions.getX()),
         var10 + 0.0F,
         (float)((double)var11 + dimensions.getZ()),
         var13,
         var14,
         var15,
         var16,
         var9 + 0.0F,
         var10 + 0.0F,
         (float)((double)var11 + dimensions.getZ()),
         var13,
         var14,
         var15,
         var16,
         var9 + 0.0F,
         (float)((double)var10 + dimensions.getY()),
         var11 + 0.0F,
         var13,
         var14,
         var15,
         var16,
         (float)((double)var9 + dimensions.getX()),
         (float)((double)var10 + dimensions.getY()),
         var11 + 0.0F,
         var13,
         var14,
         var15,
         var16,
         (float)((double)var9 + dimensions.getX()),
         (float)((double)var10 + dimensions.getY()),
         (float)((double)var11 + dimensions.getZ()),
         var13,
         var14,
         var15,
         var16,
         var9 + 0.0F,
         (float)((double)var10 + dimensions.getY()),
         (float)((double)var11 + dimensions.getZ()),
         var13,
         var14,
         var15,
         var16
      );
   }

   public void drawLine(MatrixStack stack, RenderLayer layer, Vec3d start, Vec3d end, int color) {
      VertexConsumer var6 = this.vcp.getBuffer(layer);
      Entry var7 = stack.peek();
      Vector3f var8 = start.subtract(this.camera.getPos()).toVector3f();
      Vector3f var9 = end.subtract(this.camera.getPos()).toVector3f();
      Vector3f var10 = var9.sub(var8, new Vector3f()).normalize();
      float var11 = var8.x;
      float var12 = var8.y;
      float var13 = var8.z;
      float var14 = var9.x;
      float var15 = var9.y;
      float var16 = var9.z;
      int[] var17 = ColorUtility.hexToRGBA(color);
      float var18 = (float)var17[0] / 255.0F;
      float var19 = (float)var17[1] / 255.0F;
      float var20 = (float)var17[2] / 255.0F;
      float var21 = (float)var17[3] / 255.0F;
      Emitter._emit_line__2xposition_color_normal(
         var7,
         var6,
         var11,
         var12,
         var13,
         var18,
         var19,
         var20,
         var21,
         var10.x,
         var10.y,
         var10.z,
         var14,
         var15,
         var16,
         var18,
         var19,
         var20,
         var21,
         var10.x,
         var10.y,
         var10.z
      );
   }
}
