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

import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;

@Environment(EnvType.CLIENT)
public class LowHealthAlert extends Module {
   public static LowHealthAlert INSTANCE;
   private final SliderSetting threshold = this.add(new SliderSetting("Threshold", 10.0, 0.0, 36.0, 0.5));
   private final SliderSetting yOffset = this.add(new SliderSetting("YOffset", -60.0, 0.0, 200.0, 1.0));
   private long lastFlashTime;

   public LowHealthAlert() {
      super("LowHealthAlert", Module.Category.Player);
      this.setChinese("\u4f4e\u8840\u91cf\u8b66\u62a5");
      INSTANCE = this;
   }

   @Override
   public void onRender2D(DrawContext drawContext, float tickDelta) {
      if (!nullCheck()) {
         double var3 = this.threshold.getValue();
         int var5 = mc.getWindow().getScaledWidth();
         int var6 = mc.getWindow().getScaledHeight();
         int var7 = (int)this.yOffset.getValue();
         float var8 = mc.player.getHealth();
         if ((double)var8 < var3) {
            this.drawWarning(drawContext, "Your body Health is low!", var5, var6, var7);
         }
      }
   }

   private void drawWarning(DrawContext drawContext, String text, int screenWidth, int screenHeight, int yOffset) {
      int var6 = mc.textRenderer.getWidth(text);
      int var7 = (screenWidth - var6) / 2;
      int var8 = screenHeight / 2 - 100 + yOffset;
      long var9 = System.currentTimeMillis();
      boolean var11 = var9 - this.lastFlashTime < 250L;
      if (var9 - this.lastFlashTime >= 500L) {
         this.lastFlashTime = var9;
      }

      if (var11) {
         int var12 = var7 - 2;
         int var13 = var8 - 2;
         int var14 = var7 + var6 + 2;
         int var15 = var8 + 9 + 2;
         drawContext.fill(var12 + 1, var13, var14 - 1, var13 + 1, -2136342528);
         drawContext.fill(var12 + 1, var15 - 1, var14 - 1, var15, -2136342528);
         drawContext.fill(var12, var13 + 1, var12 + 1, var15 - 1, -2136342528);
         drawContext.fill(var14 - 1, var13 + 1, var14, var15 - 1, -2136342528);
         drawContext.fill(var12 + 1, var13 + 1, var14 - 1, var15 - 1, -2136342528);
      }

      drawContext.drawText(mc.textRenderer, Text.literal(text), var7, var8, -65536, false);
   }
}
