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

import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import java.util.Objects;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;

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

   public LowArmorAlert() {
      super("LowArmorAlert", Module.Category.Player);
      this.setChinese("\u4f4e\u76d4\u7532\u8010\u4e45\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();
         boolean var8 = false;
         ItemStack var9 = mc.player.getInventory().armor.get(3);
         if (!var9.isEmpty() && var9.getItem() instanceof ArmorItem) {
            float var10 = this.getDurabilityPercent(var9);
            if ((double)var10 < var3) {
               var8 = true;
            }
         }

         ItemStack var14 = mc.player.getInventory().armor.get(2);
         if (!var14.isEmpty() && var14.getItem() instanceof ArmorItem) {
            float var11 = this.getDurabilityPercent(var14);
            if ((double)var11 < var3) {
               var8 = true;
            }
         }

         ItemStack var15 = mc.player.getInventory().armor.get(1);
         if (!var15.isEmpty() && var15.getItem() instanceof ArmorItem) {
            float var12 = this.getDurabilityPercent(var15);
            if ((double)var12 < var3) {
               var8 = true;
            }
         }

         ItemStack var16 = mc.player.getInventory().armor.get(0);
         if (!var16.isEmpty() && var16.getItem() instanceof ArmorItem) {
            float var13 = this.getDurabilityPercent(var16);
            if ((double)var13 < var3) {
               var8 = true;
            }
         }

         if (var8) {
            this.drawWarning(drawContext, "Your armor durability is low!", var5, var6, var7);
         }
      }
   }

   private float getDurabilityPercent(ItemStack stack) {
      if (stack.isEmpty()) {
         return 0.0F;
      } else {
         float var2 = (float)stack.getMaxDamage();
         float var3 = (float)stack.getDamage();
         float var4 = var2 - var3;
         return var4 / var2 * 100.0F;
      }
   }

   private void drawWarning(DrawContext drawContext, String text, int screenWidth, int screenHeight, int yOffset) {
      int var6 = mc.textRenderer.getWidth(text);
      Objects.requireNonNull(mc.textRenderer);
      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);
   }
}
