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

import dev.crucifixbeta.api.utils.player.EntityUtil;
import dev.crucifixbeta.api.utils.render.ColorUtil;
import dev.crucifixbeta.api.utils.render.TextUtil;
import dev.crucifixbeta.core.impl.FontManager;
import dev.crucifixbeta.mod.modules.HudModule;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import java.awt.Color;
import java.util.Objects;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.ItemStack;

@Environment(EnvType.CLIENT)
public class ArmorHudModule extends HudModule {
   public static ArmorHudModule INSTANCE;
   private static final int SLOT_SPACING;
   private static final int ICON_SIZE;
   private static final int DURABILITY_PAD;
   private final BooleanSetting durability = this.add(new BooleanSetting("Durability", true));
   private final Color minColor = new Color(196, 0, 0);
   private final Color maxColor = new Color(0, 227, 0);

   public ArmorHudModule() {
      super("Armor", "\u62a4\u7532", 0, 70);
      INSTANCE = this;
   }

   @Override
   public void onRender2D(DrawContext context, float tickDelta) {
      if (mc.player == null) {
         this.clearHudBounds();
      } else {
         int var3 = 0;
         boolean var4 = false;

         for (ItemStack var6 : mc.player.getInventory().armor) {
            var3++;
            if (!var6.isEmpty()) {
               var4 = true;
            }
         }

         if (!var4) {
            this.clearHudBounds();
         } else {
            int var19 = Math.max(1, 20 * var3);
            int var20 = this.durability.getValue() ? 6 : 0;
            int var7 = 16 + var20;
            int var8 = this.getHudRenderX(var19);
            int var9 = this.getHudRenderY(var7);
            int var10 = var8;
            int var11 = var9 + var20;
            int var12 = 0;

            for (ItemStack var14 : mc.player.getInventory().armor) {
               int var15 = var10 + var12 * 20;
               if (var14.isEmpty()) {
                  var12++;
               } else {
                  context.getMatrices().push();
                  int var16 = EntityUtil.getDamagePercent(var14);
                  context.drawItem(var14, var15, var11);
                  context.drawItemInSlot(mc.textRenderer, var14, var15, var11);
                  if (this.durability.getValue()) {
                     int var17 = ColorUtil.fadeColor(this.minColor, this.maxColor, (double)((float)var16 / 100.0F)).getRGB();
                     if (HudSetting.useFont()) {
                        FontManager.small
                           .drawString(
                              context.getMatrices(),
                              var16 + "%",
                              (double)(var15 + 1),
                              (double)((float)var11 - FontManager.small.getFontHeight() / 2.0F),
                              var17,
                              false
                           );
                     } else {
                        String var18 = var16 + "%";
                        Objects.requireNonNull(mc.textRenderer);
                        TextUtil.drawStringScale(context, var18, (float)var15 + 2.0F, (float)var11 - 2.25F, var17, 0.5F, false);
                     }
                  }

                  context.getMatrices().pop();
                  var12++;
               }
            }

            this.setHudBounds(var8, var9, var19, var7);
         }
      }
   }

   static {

      DURABILITY_PAD = 6;
      ICON_SIZE = 16;
      SLOT_SPACING = 20;
   }
}
