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

import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.UpdateEvent;
import dev.crucifixbeta.api.utils.render.Render3DUtil;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.settings.impl.ColorSetting;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;

@Environment(EnvType.CLIENT)
public class PhaseESP extends Module {
   public static PhaseESP INSTANCE;
   private final SliderSetting getDistance = this.add(new SliderSetting("Distance", 0.1, 0.0, 1.0, 0.1));
   private final SliderSetting bevelDistance = this.add(new SliderSetting("BevelDistance", 0.2, 0.0, 1.0, 0.1));
   private final ColorSetting safeFill = this.add(new ColorSetting("SafeFill", new Color(0, 255, 0, 50)).injectBoolean(true));
   private final ColorSetting safeBox = this.add(new ColorSetting("SafeBox", new Color(0, 255, 0, 100)).injectBoolean(true));
   private final ColorSetting semiSafeFill = this.add(new ColorSetting("SemiSafeFill", new Color(244, 255, 0, 50)).injectBoolean(true));
   private final ColorSetting semiSafeBox = this.add(new ColorSetting("SemiSafeBox", new Color(244, 255, 0, 100)).injectBoolean(true));
   private final ColorSetting unsafeFill = this.add(new ColorSetting("UnsafeFill", new Color(148, 0, 0, 50)).injectBoolean(true));
   private final ColorSetting unsafeBox = this.add(new ColorSetting("UnsafeBox", new Color(148, 0, 0, 100)).injectBoolean(true));
   List<BlockPos> safe = new ArrayList<>();
   List<BlockPos> semiSafe = new ArrayList<>();
   List<BlockPos> unsafe = new ArrayList<>();
   int[] offsets = new int[]{1, 0, -1};

   public PhaseESP() {
      super("PhaseESP", Module.Category.Render);
      this.setChinese("\u7a7f\u5899\u663e\u793a");
      INSTANCE = this;
   }

   @EventListener
   public void onUpdate(UpdateEvent event) {
      this.safe.clear();
      this.semiSafe.clear();
      this.unsafe.clear();

      for (int var5 : this.offsets) {
         for (int var9 : this.offsets) {
            BlockPos var10 = mc.player.getBlockPos().add(var5, 0, var9);
            double var11 = mc.player.getPos().distanceTo(var10.toBottomCenterPos());
            double var13 = var5 != 0 && var9 != 0 ? this.bevelDistance.getValue() + 1.0 : this.getDistance.getValue() + 0.8;
            if (var11 <= var13) {
               BlockState var15 = mc.world.getBlockState(var10);
               BlockPos var16 = var10.down();
               if (var15.getBlock() == Blocks.BEDROCK) {
                  Block var17 = mc.world.getBlockState(var16).getBlock();
                  if (var17 == Blocks.BEDROCK) {
                     this.safe.add(var10);
                  } else {
                     this.unsafe.add(var10);
                  }
               } else if (var15.isFullCube(mc.world, var10)) {
                  Block var18 = mc.world.getBlockState(var16).getBlock();
                  if (var18 == Blocks.BEDROCK) {
                     this.semiSafe.add(var10);
                  } else {
                     this.unsafe.add(var10);
                  }
               }
            }
         }
      }
   }

   @Override
   public void onRender3D(MatrixStack matrixStack) {
      this.draw(matrixStack, this.safe, this.safeFill, this.safeBox);
      this.draw(matrixStack, this.unsafe, this.unsafeFill, this.unsafeBox);
      this.draw(matrixStack, this.semiSafe, this.semiSafeFill, this.semiSafeBox);
   }

   private void draw(MatrixStack matrixStack, List<BlockPos> list, ColorSetting fill, ColorSetting box) {
      for (BlockPos var6 : list) {
         Box var7 = new Box(
            (double)var6.getX(), (double)var6.getY(), (double)var6.getZ(), (double)(var6.getX() + 1), (double)var6.getY(), (double)(var6.getZ() + 1)
         );
         if (fill.booleanValue) {
            Render3DUtil.drawFill(matrixStack, var7, fill.getValue());
         }

         if (box.booleanValue) {
            Render3DUtil.drawBox(matrixStack, var7, box.getValue());
         }
      }
   }
@Environment(EnvType.CLIENT)
   public static enum Type {
      None,
      Air,
      Normal,
      Bedrock;

}
}
