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

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.utils.player.InventoryUtil;
import dev.crucifixbeta.core.impl.FontManager;
import dev.crucifixbeta.mod.gui.clickgui.ClickGuiScreen;
import dev.crucifixbeta.mod.gui.windows.impl.ItemSelectWindow;
import dev.crucifixbeta.mod.modules.HudModule;
import dev.crucifixbeta.mod.modules.impl.client.ClickGui;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import java.util.ArrayList;
import java.util.function.IntSupplier;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;

@Environment(EnvType.CLIENT)
public class ItemCounterHudModule extends HudModule {
   private final ItemStack stack;
   private final IntSupplier countSupplier;
   private final boolean useManager;
   private final BooleanSetting edit;
   private final SliderSetting perRow;
   private final BooleanSetting showZero;

   public ItemCounterHudModule(String name, String chinese, Item item, int defaultX, int defaultY) {
      this(name, chinese, item, defaultX, defaultY, () -> InventoryUtil.getItemCount(item));
   }

   public ItemCounterHudModule(String name, String chinese, Item item, int defaultX, int defaultY, IntSupplier countSupplier) {
      super(name, chinese, defaultX, defaultY);
      this.stack = new ItemStack(item);
      this.countSupplier = countSupplier;
      this.useManager = false;
      this.edit = null;
      this.perRow = null;
      this.showZero = null;
   }

   public ItemCounterHudModule(String name, String chinese, int defaultX, int defaultY) {
      super(name, chinese, defaultX, defaultY);
      this.stack = new ItemStack(Items.AIR);
      this.countSupplier = null;
      this.useManager = true;
      this.edit = this.add(new BooleanSetting("Edit", false).injectTask(this::openGui));
      this.perRow = this.add(new SliderSetting("PerRow", 8, 1, 20));
      this.showZero = this.add(new BooleanSetting("ShowZero", false));
   }

   private void openGui() {
      this.edit.setValueWithoutTask(false);
      if (!nullCheck()) {
         ClickGuiScreen var1 = ClickGuiScreen.getInstance();
         var1.openHudWindow(new ItemSelectWindow(CrucifixBeta.HUD_ITEM));
         ClickGui var2 = ClickGui.getInstance();
         if (var2 != null && !var2.isOn()) {
            var2.enable();
         } else if (mc.currentScreen != var1) {
            mc.setScreen(var1);
         }
      }
   }

   private Item resolveItem(String key) {
      String var2 = key.replace("block.minecraft.", "").replace("item.minecraft.", "");

      Identifier var3;
      try {
         var3 = var2.contains(":") ? Identifier.of(var2) : Identifier.of("minecraft", var2);
      } catch (Exception var5) {
         return null;
      }

      Item var4 = Registries.ITEM.get(var3);
      return var4 == Items.AIR ? null : var4;
   }

   @Override
   public void onRender2D(DrawContext drawContext, float tickDelta) {
      if (!this.useManager) {
         int var22 = this.countSupplier.getAsInt();
         if (var22 <= 0) {
            this.clearHudBounds();
         } else {
            this.stack.setCount(var22);
            int var23 = this.getHudRenderX(16);
            int var25 = this.getHudRenderY(16);
            this.setHudBounds(var23, var25, 16, 16);
            drawContext.drawItem(this.stack, var23, var25);
            if (HudSetting.useFont()) {
               String var27 = String.valueOf(var22);
               int var29 = var23 + 16 - (int)Math.ceil((double)FontManager.ui.getWidth(var27));
               int var31 = var25 + 16 - (int)Math.ceil((double)FontManager.ui.getFontHeight());
               drawContext.getMatrices().push();
               drawContext.getMatrices().translate(0.0F, 0.0F, 200.0F);
               FontManager.ui.drawString(drawContext.getMatrices(), var27, (double)(var29 + 1), (double)(var31 + 1), -1, false);
               drawContext.getMatrices().pop();
            } else {
               drawContext.drawItemInSlot(mc.textRenderer, this.stack, var23, var25);
            }
         }
      } else {
         ArrayList var3 = new ArrayList();
         ArrayList var4 = new ArrayList();

         for (String var6 : CrucifixBeta.HUD_ITEM.getList()) {
            Item var7 = this.resolveItem(var6);
            if (var7 != null) {
               int var8 = InventoryUtil.getItemCount(var7);
               if (var8 > 0 || this.showZero.getValue()) {
                  ItemStack var9 = new ItemStack(var7);
                  var9.setCount(Math.max(1, var8));
                  var3.add(var9);
                  var4.add(var8);
               }
            }
         }

         if (var3.isEmpty()) {
            this.clearHudBounds();
         } else {
            int var24 = Math.max(1, this.perRow.getValueInt());
            int var26 = (var3.size() + var24 - 1) / var24;
            int var28 = Math.min(var24, var3.size());
            int var30 = var28 * 18;
            int var32 = var26 * 18;
            int var10 = this.getHudRenderX(var30);
            int var11 = this.getHudRenderY(var32);
            this.setHudBounds(var10, var11, var30, var32);

            for (int var12 = 0; var12 < var3.size(); var12++) {
               int var13 = var12 / var24;
               int var14 = var12 % var24;
               int var15 = var10 + var14 * 18;
               int var16 = var11 + var13 * 18;
               ItemStack var17 = (ItemStack)var3.get(var12);
               int var18 = (Integer)var4.get(var12);
               if (HudSetting.useFont()) {
                  drawContext.drawItem(var17, var15, var16);
                  String var19 = String.valueOf(var18);
                  int var20 = var15 + 16 - (int)Math.ceil((double)FontManager.ui.getWidth(var19));
                  int var21 = var16 + 16 - (int)Math.ceil((double)FontManager.ui.getFontHeight());
                  drawContext.getMatrices().push();
                  drawContext.getMatrices().translate(0.0F, 0.0F, 200.0F);
                  FontManager.ui.drawString(drawContext.getMatrices(), var19, (double)(var20 + 1), (double)(var21 + 1), -1, false);
                  drawContext.getMatrices().pop();
               } else {
                  drawContext.drawItemInSlot(mc.textRenderer, var17, var15, var16);
               }
            }
         }
      }
   }
}
