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 net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.util.math.MathHelper;

@Environment(EnvType.CLIENT)
public class AntiAlias extends Module {
   public static AntiAlias INSTANCE;
   public final BooleanSetting smoothLines = this.add(new BooleanSetting("SmoothLines", true));
   public final BooleanSetting smoothPoly = this.add(new BooleanSetting("SmoothPoly", false));
   public final BooleanSetting shapeAA = this.add(new BooleanSetting("ShapeAA", true));
   public final BooleanSetting fontAA = this.add(new BooleanSetting("FontAA", true));
   public final BooleanSetting vanillaTextAA = this.add(new BooleanSetting("VanillaTextAA", false));
   public final BooleanSetting clickGuiAA = this.add(new BooleanSetting("ClickGuiAA", true));
   public final BooleanSetting inventoryAA = this.add(new BooleanSetting("InventoryAA", true));
   private static int clickGuiDepth;
   private static int inventoryDepth;
   private static int textDepth;

   public AntiAlias() {
      super("AntiAlias", Module.Category.Render);
      this.setChinese("\u6297\u952f\u9f7f");
      INSTANCE = this;
   }

   public static boolean isFontAAEnabled() {
      return INSTANCE != null && INSTANCE.isOn() && INSTANCE.fontAA.getValue();
   }

   public static boolean isVanillaTextAAEnabled() {
      return isFontAAEnabled() && INSTANCE.vanillaTextAA.getValue();
   }

   public static boolean isClickGuiAAEnabled() {
      return INSTANCE != null && INSTANCE.isOn() && INSTANCE.clickGuiAA.getValue();
   }

   public static boolean isInventoryAAEnabled() {
      return INSTANCE != null && INSTANCE.isOn() && INSTANCE.inventoryAA.getValue();
   }

   public static boolean isShapeAAEnabled() {
      return INSTANCE != null && INSTANCE.isOn() && INSTANCE.shapeAA.getValue();
   }

   public static void beginClickGuiAA() {
      if (isClickGuiAAEnabled()) {
         clickGuiDepth++;
      }
   }

   public static void endClickGuiAA() {
      if (clickGuiDepth > 0) {
         clickGuiDepth--;
      }
   }

   public static void withClickGuiAA(Runnable runnable) {
      beginClickGuiAA();

      try {
         runnable.run();
      } finally {
         endClickGuiAA();
      }
   }

   public static void beginTextRender() {
      textDepth++;
   }

   public static void endTextRender() {
      if (textDepth > 0) {
         textDepth--;
      }
   }

   public static void withTextRender(Runnable runnable) {
      beginTextRender();

      try {
         runnable.run();
      } finally {
         endTextRender();
      }
   }

   public static void beginInventoryAA() {
      if (isInventoryAAEnabled()) {
         inventoryDepth++;
      }
   }

   public static void endInventoryAA() {
      if (inventoryDepth > 0) {
         inventoryDepth--;
      }
   }

   public static void drawText(DrawContext context, TextRenderer renderer, String text, int x, int y, int color, boolean shadow) {
      if (text != null && !text.isEmpty()) {
         if (!isFontAAEnabled()) {
            context.drawText(renderer, text, x, y, color, false);
         } else {
            RenderSystem.enableBlend();
            int var7 = withMultipliedAlpha(color, 0.28F);
            context.getMatrices().push();
            context.getMatrices().translate(0.25F, 0.0F, 0.0F);
            context.drawText(renderer, text, x, y, var7, false);
            context.getMatrices().translate(-0.5F, 0.0F, 0.0F);
            context.drawText(renderer, text, x, y, var7, false);
            context.getMatrices().pop();
            context.drawText(renderer, text, x, y, color, false);
         }
      }
   }

   @Override
   public void onDisable() {
      clickGuiDepth = 0;
      inventoryDepth = 0;
      textDepth = 0;
   }

   private static int withMultipliedAlpha(int color, float multiplier) {
      int var2 = color >>> 24 & 0xFF;
      return withAlpha(color, (int)((float)var2 * MathHelper.clamp(multiplier, 0.0F, 1.0F)));
   }

   private static int withAlpha(int color, int alpha) {
      return MathHelper.clamp(alpha, 0, 255) << 24 | color & 16777215;
   }
}
