package dev.crucifixbeta.mod.modules.impl.render;

import com.mojang.blaze3d.systems.RenderSystem;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import dev.crucifixbeta.mod.modules.settings.impl.ColorSetting;
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.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.hit.HitResult.Type;
import net.minecraft.util.math.Box;
import net.minecraft.util.shape.VoxelShape;
import org.joml.Matrix4f;

@Environment(EnvType.CLIENT)
public class HighLight extends Module {
   public static HighLight INSTANCE;
   private final BooleanSetting depth = this.add(new BooleanSetting("Depth", true));
   private final ColorSetting fill = this.add(new ColorSetting("Fill", new Color(255, 0, 0, 50)).injectBoolean(true));
   private final ColorSetting boxColor = this.add(new ColorSetting("Box", new Color(255, 0, 0, 100)).injectBoolean(true));

   public HighLight() {
      super("HighLight", Module.Category.Render);
      INSTANCE = this;
      this.setChinese("\u65b9\u5757\u9ad8\u4eae");
   }

   @Override
   public void onRender3D(MatrixStack matrixStack) {
      if (mc.crosshairTarget.getType() == Type.BLOCK) {
         HitResult var2 = mc.crosshairTarget;
         if (mc.crosshairTarget instanceof BlockHitResult) {
            BlockHitResult var3 = (BlockHitResult)var2;
            if (this.fill.booleanValue || this.boxColor.booleanValue) {
               VoxelShape var4 = mc.world.getBlockState(var3.getBlockPos()).getOutlineShape(mc.world, var3.getBlockPos());
               if (var4 == null) {
                  return;
               }

               if (var4.isEmpty()) {
                  return;
               }

               Box var5 = var4.getBoundingBox().offset(var3.getBlockPos()).expand(0.001);
               var5 = var5.offset(mc.gameRenderer.getCamera().getPos().negate());
               RenderSystem.enableBlend();
               if (!this.depth.getValue()) {
                  RenderSystem.disableDepthTest();
               } else {
                  RenderSystem.enableDepthTest();
               }

               Matrix4f var6 = matrixStack.peek().getPositionMatrix();
               if (this.fill.booleanValue) {
                  Color var7 = this.fill.getValue();
                  float var8 = (float)var7.getAlpha() / 255.0F;
                  float var9 = (float)var7.getRed() / 255.0F;
                  float var10 = (float)var7.getGreen() / 255.0F;
                  float var11 = (float)var7.getBlue() / 255.0F;
                  RenderSystem.setShader(GameRenderer::getPositionColorProgram);
                  BufferBuilder var12 = Tessellator.getInstance().begin(DrawMode.QUADS, VertexFormats.POSITION_COLOR);
                  var12.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.minZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.minZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.minZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.minZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.minZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.minZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.minZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.minZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.minZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.minZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.minZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.maxZ).color(var9, var10, var11, var8);
                  var12.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.minZ).color(var9, var10, var11, var8);
                  BufferRenderer.drawWithGlobalProgram(var12.end());
               }

               if (this.depth.getValue()) {
                  RenderSystem.disableDepthTest();
               }

               if (this.boxColor.booleanValue) {
                  Color var14 = this.boxColor.getValue();
                  float var15 = (float)var14.getAlpha() / 255.0F;
                  float var16 = (float)var14.getRed() / 255.0F;
                  float var17 = (float)var14.getGreen() / 255.0F;
                  float var18 = (float)var14.getBlue() / 255.0F;
                  RenderSystem.setShader(GameRenderer::getPositionColorProgram);
                  BufferBuilder var19 = Tessellator.getInstance().begin(DrawMode.DEBUG_LINES, VertexFormats.POSITION_COLOR);
                  var19.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.minZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.minZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.minZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.minZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.minZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.minZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.minZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.minZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.minY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.minX, (float)var5.minY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.minZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.minZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.minZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.maxX, (float)var5.maxY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.maxZ).color(var16, var17, var18, var15);
                  var19.vertex(var6, (float)var5.minX, (float)var5.maxY, (float)var5.minZ).color(var16, var17, var18, var15);
                  BufferRenderer.drawWithGlobalProgram(var19.end());
               }

               RenderSystem.enableDepthTest();
               RenderSystem.disableBlend();
            }
         }
      }
   }
}
