package dev.crucifixbeta.mod.modules.impl.client.hud;

import dev.crucifixbeta.api.utils.render.TextUtil;
import dev.crucifixbeta.core.impl.FontManager;
import dev.crucifixbeta.mod.modules.HudModule;
import dev.crucifixbeta.mod.modules.impl.client.ColorsModule;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.world.World;

@Environment(EnvType.CLIENT)
public class CoordsHudModule extends HudModule {
   public static CoordsHudModule INSTANCE;
   private final BooleanSetting lowerCase = this.add(new BooleanSetting("LowerCase", false));

   public CoordsHudModule() {
      super("Coords", "", "\u5750\u6807", 2, 2, HudModule.Corner.LeftBottom);
      INSTANCE = this;
   }

   @Override
   public void onRender2D(DrawContext context, float tickDelta) {
      if (mc.player != null && mc.world != null) {
         String var3 = this.getCoordsString();
         if (this.lowerCase.getValue()) {
            var3 = var3.toLowerCase();
         }

         int var4 = HudSetting.useFont() ? (int)Math.ceil((double)FontManager.ui.getWidth(var3)) : mc.textRenderer.getWidth(var3);
         int var5;
         if (HudSetting.useFont()) {
            var5 = (int)Math.ceil((double)FontManager.ui.getFontHeight());
         } else {
            var5 = 9;
         }

         int var6 = this.getHudRenderX(var4);
         int var7 = this.getHudRenderY(var5);
         int var8 = this.getHudColor(0.0);
         TextUtil.drawString(context, var3, (double)var6, (double)var7, var8, HudSetting.useFont(), false);
         this.setHudBounds(var6, var7, Math.max(1, var4), Math.max(1, var5));
      } else {
         this.clearHudBounds();
      }
   }

   private int getHudColor(double delay) {
      ColorsModule var3 = ColorsModule.INSTANCE;
      return var3 == null ? -1 : var3.getHudColorRGB(delay);
   }

   private String getCoordsString() {
      boolean var1 = mc.world.getRegistryKey().equals(World.NETHER);
      int var2 = mc.player.getBlockX();
      int var3 = mc.player.getBlockY();
      int var4 = mc.player.getBlockZ();
      float var5 = !var1 ? 0.125F : 8.0F;
      int var6 = (int)(mc.player.getX() * (double)var5);
      int var7 = (int)(mc.player.getZ() * (double)var5);
      return "XYZ §f"
         + (
            var1
               ? var2 + ", " + var3 + ", " + var4 + " §7[§f" + var6 + ", " + var7 + "§7]§f"
               : var2 + ", " + var3 + ", " + var4 + "§7 [§f" + var6 + ", " + var7 + "§7]"
         );
   }
}
