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

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.utils.render.Render3DUtil;
import dev.crucifixbeta.api.utils.world.BlockUtil;
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 net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.ChestBlockEntity;
import net.minecraft.block.entity.EnderChestBlockEntity;
import net.minecraft.block.entity.ShulkerBoxBlockEntity;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.Vec3d;

@Environment(EnvType.CLIENT)
public class Tracers extends Module {
   private final ColorSetting item = this.add(new ColorSetting("Item", new Color(255, 255, 255, 100)).injectBoolean(true));
   private final ColorSetting player = this.add(new ColorSetting("Player", new Color(255, 255, 255, 100)).injectBoolean(true));
   private final ColorSetting chest = this.add(new ColorSetting("Chest", new Color(255, 255, 255, 100)).injectBoolean(false));
   private final ColorSetting enderChest = this.add(new ColorSetting("EnderChest", new Color(255, 100, 255, 100)).injectBoolean(false));
   private final ColorSetting shulkerBox = this.add(new ColorSetting("ShulkerBox", new Color(15, 255, 255, 100)).injectBoolean(false));
   private final SliderSetting distance = this.add(new SliderSetting("Distance", 30.0, 0.0, 300.0, 0.1));

   public Tracers() {
      super("Tracers", Module.Category.Render);
      this.setChinese("\u8ffd\u8e2a\u8005");
   }

   @Override
   public void onRender3D(MatrixStack matrixStack) {
      mc.options.getBobView().setValue(false);
      double var2 = this.distance.getValue();
      if (this.item.booleanValue || this.player.booleanValue) {
         for (Entity var5 : CrucifixBeta.THREAD.getEntities()) {
            if (var5 instanceof ItemEntity && this.item.booleanValue) {
               if (!(mc.player.getPos().distanceTo(var5.getPos()) > var2)) {
                  this.drawLine(var5.getPos(), this.item.getValue());
               }
            } else if (var5 instanceof PlayerEntity && this.player.booleanValue && var5 != mc.player && !(mc.player.getPos().distanceTo(var5.getPos()) > var2)) {
               this.drawLine(var5.getPos(), this.player.getValue());
            }
         }
      }

      for (BlockEntity var6 : BlockUtil.getTileEntities()) {
         if (var6 instanceof ChestBlockEntity && this.chest.booleanValue) {
            if (!(mc.player.getPos().distanceTo(var6.getPos().toCenterPos()) > var2)) {
               this.drawLine(var6.getPos().toCenterPos(), this.chest.getValue());
            }
         } else if (var6 instanceof EnderChestBlockEntity && this.enderChest.booleanValue) {
            if (!(mc.player.getPos().distanceTo(var6.getPos().toCenterPos()) > var2)) {
               this.drawLine(var6.getPos().toCenterPos(), this.enderChest.getValue());
            }
         } else if (var6 instanceof ShulkerBoxBlockEntity
            && this.shulkerBox.booleanValue
            && !(mc.player.getPos().distanceTo(var6.getPos().toCenterPos()) > var2)) {
            this.drawLine(var6.getPos().toCenterPos(), this.shulkerBox.getValue());
         }
      }
   }

   private void drawLine(Vec3d pos, Color color) {
      Render3DUtil.drawLine(
         pos,
         mc.gameRenderer
            .getCamera()
            .getPos()
            .add(
               Vec3d.fromPolar(mc.player.getPitch(mc.getRenderTickCounter().getTickDelta(true)), mc.player.getYaw(mc.getRenderTickCounter().getTickDelta(true)))
                  .multiply(0.2)
            ),
         color
      );
   }
}
