package dev.crucifixbeta.mod.gui.clickgui.pages;

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.utils.Wrapper;
import dev.crucifixbeta.api.utils.math.Animation;
import dev.crucifixbeta.api.utils.math.Easing;
import dev.crucifixbeta.api.utils.render.ColorUtil;
import dev.crucifixbeta.api.utils.render.Render2DUtil;
import dev.crucifixbeta.api.utils.render.TextUtil;
import dev.crucifixbeta.core.impl.FontManager;
import dev.crucifixbeta.mod.gui.clickgui.ClickGuiFrame;
import dev.crucifixbeta.mod.gui.clickgui.ClickGuiScreen;
import dev.crucifixbeta.mod.gui.items.Component;
import dev.crucifixbeta.mod.gui.items.buttons.ModuleButton;
import dev.crucifixbeta.mod.gui.windows.WindowBase;
import dev.crucifixbeta.mod.gui.windows.WindowsScreen;
import dev.crucifixbeta.mod.modules.HudModule;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.impl.client.ClickGui;
import dev.crucifixbeta.mod.modules.impl.client.ClientSetting;
import dev.crucifixbeta.mod.modules.impl.client.hud.HudSetting;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Comparator;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;

@Environment(EnvType.CLIENT)
public final class ClickGuiHudPage {
   private final ClickGuiScreen host;
   private Component hudComponent;
   private boolean hudPosInit;
   private float hudLocalX;
   private float hudLocalY;
   private HudModule elementDragging;
   private int elementDragDx;
   private int elementDragDy;
   private boolean selecting;
   private int selectStartX;
   private int selectStartY;
   private int selectEndX;
   private int selectEndY;
   private final ArrayList<HudModule> selectedHud = new ArrayList<>();
   private boolean groupDragging;
   private int groupDragStartMouseX;
   private int groupDragStartMouseY;
   private final ArrayList<ClickGuiHudPage.GroupDragEntry> groupDragEntries = new ArrayList<>();
   private boolean layoutMenuOpen;
   private int layoutMenuX;
   private int layoutMenuY;
   private final Animation layoutButtonAnim = new Animation();
   private final Animation layoutMenuAnim = new Animation();
   private WindowBase editWindow;
   private boolean editWindowHover;
   private boolean editWindowPosInit;
   private float editWindowLocalX;
   private float editWindowLocalY;

   public ClickGuiHudPage(ClickGuiScreen host) {
      this.host = host;
   }

   public void init() {
      this.initHudButtons();
   }

   public void openHudWindow(WindowBase window) {
      this.editWindow = window;
      if (this.editWindow != null) {
         this.editWindow.setVisible(true);
         this.editWindowPosInit = false;
      }
   }

   public void resetHudLayout() {
      this.hudPosInit = false;
   }

   public void mouseScrolled(double verticalAmount) {
      if (Wrapper.mc != null && Wrapper.mc.getWindow() != null) {
         if (this.editWindow != null && this.editWindow.isVisible()) {
            this.editWindow.mouseScrolled((int)verticalAmount);
            if (this.editWindowHover) {
               return;
            }
         }

         if (this.hudComponent != null) {
            if (this.hudPosInit) {
               if (InputUtil.isKeyPressed(Wrapper.mc.getWindow().getHandle(), 340)) {
                  if (verticalAmount < 0.0) {
                     this.hudLocalX -= 15.0F;
                  } else if (verticalAmount > 0.0) {
                     this.hudLocalX += 15.0F;
                  }
               } else if (verticalAmount < 0.0) {
                  this.hudLocalY -= 15.0F;
               } else if (verticalAmount > 0.0) {
                  this.hudLocalY += 15.0F;
               }
            }
         }
      }
   }

   public void mouseReleased(int mouseX, int mouseY, int releaseButton) {
      if (this.editWindow != null && this.editWindow.isVisible()) {
         this.editWindow.mouseReleased((double)mouseX, (double)mouseY, releaseButton);
      }

      if (releaseButton == 0) {
         this.elementDragging = null;
         this.groupDragging = false;
         this.groupDragEntries.clear();
      }

      if (this.hudComponent != null) {
         this.hudComponent.mouseReleased(mouseX, mouseY, releaseButton);
      }
   }

   public boolean keyPressed(int keyCode) {
      if (this.editWindow != null && this.editWindow.isVisible()) {
         if (keyCode == 256) {
            this.editWindow.setVisible(false);
            this.editWindow = null;
            this.editWindowPosInit = false;
            return true;
         } else {
            this.editWindow.keyPressed(keyCode, 0, 0);
            return false;
         }
      } else if (keyCode == 256) {
         if (this.selecting || this.elementDragging != null || this.groupDragging) {
            this.selecting = false;
            this.elementDragging = null;
            this.groupDragging = false;
            this.groupDragEntries.clear();
            return true;
         } else if (this.layoutMenuOpen) {
            this.closeLayoutMenu();
            return true;
         } else if (!this.selectedHud.isEmpty()) {
            this.clearSelection();
            return true;
         } else {
            return false;
         }
      } else {
         if (!this.selectedHud.isEmpty()) {
            if (keyCode == 71) {
               this.applyGridLayout();
               return true;
            }

            if (keyCode == 72) {
               this.applyHorizontalLayout();
               return true;
            }

            if (keyCode == 86) {
               this.applyVerticalLayout();
               return true;
            }

            if (keyCode == 76) {
               this.applyAlignLeft();
               return true;
            }

            if (keyCode == 84) {
               this.applyAlignTop();
               return true;
            }
         }

         if (this.hudComponent != null) {
            this.hudComponent.onKeyPressed(keyCode);
         }

         return false;
      }
   }

   public void charTyped(char chr, int modifiers) {
      if (this.editWindow != null && this.editWindow.isVisible()) {
         this.editWindow.charTyped(chr, modifiers);
      } else {
         if (this.hudComponent != null) {
            this.hudComponent.onKeyTyped(chr, modifiers);
         }
      }
   }

   public void render(DrawContext context, int mouseX, int mouseY, float delta, ClickGuiFrame frame) {
      ClickGui var6 = ClickGui.getInstance();
      if (var6 != null) {
         if (this.editWindow != null && !this.editWindow.isVisible()) {
            this.editWindow = null;
            this.editWindowPosInit = false;
         }

         this.dragHudElements(mouseX, mouseY);
         this.updateSelection(mouseX, mouseY);
         float var7 = frame.baseX(ClickGuiScreen.Page.Hud);
         float var8 = frame.scale == 0.0F ? (float)frame.screenW : (float)frame.screenW / frame.scale;
         float var9 = Math.max(8.0F, (var8 - (float)this.hudComponent.getWidth()) / 2.0F);
         float var10 = Math.max(24.0F, (float)frame.panelY + 8.0F);
         if (this.hudComponent != null) {
            this.hudComponent.setMouseMoveOffset(frame.totalOffsetX, frame.totalOffsetY);
            this.hudComponent.setPageOffsetX(var7);
            if (!this.hudPosInit) {
               this.hudLocalX = var9;
               this.hudLocalY = var10;
               this.hudComponent.setX((int)this.hudLocalX);
               this.hudComponent.setY((int)this.hudLocalY);
               this.hudPosInit = true;
            }

            if (this.hudComponent.drag) {
               this.hudLocalX = (float)this.hudComponent.getX() - var7 - frame.totalOffsetX;
               this.hudLocalY = (float)this.hudComponent.getY() - frame.totalOffsetY;
            } else {
               this.hudComponent.setX((int)this.hudLocalX);
               this.hudComponent.setY((int)this.hudLocalY);
            }

            this.hudComponent.drawScreen(context, mouseX, mouseY, delta);
            context.getMatrices().push();
            context.getMatrices().translate(0.0F, 0.0F, 900.0F);
            this.renderSelectionOverlay(context, frame, mouseX, mouseY);
            this.renderLayoutMenu(context, frame, mouseX, mouseY);
            context.getMatrices().pop();
            if (this.editWindow != null && this.editWindow.isVisible()) {
               int var11 = (int)frame.unitMouseX(mouseX);
               int var12 = (int)frame.unitMouseY(mouseY);
               context.getMatrices().push();
               context.getMatrices().translate(0.0F, 0.0F, 1100.0F);
               this.syncEditWindowPosition(frame);
               this.editWindowHover = this.isWindowHover(this.editWindow, var11, var12);
               this.editWindow.render(context, var11, var12);
               this.updateEditWindowLocal(frame);
               context.getMatrices().pop();
            }
         }
      }
   }

   public boolean mouseClicked(int mouseX, int mouseY, int mouseButton, ClickGuiFrame frame) {
      if (Wrapper.mc != null && Wrapper.mc.getWindow() != null) {
         ClickGui var5 = ClickGui.getInstance();
         if (var5 == null) {
            return false;
         } else {
            if (this.editWindow != null && this.editWindow.isVisible()) {
               this.syncEditWindowPosition(frame);
               int var6 = (int)frame.unitMouseX(mouseX);
               int var7 = (int)frame.unitMouseY(mouseY);
               this.editWindow.mouseClicked((double)var6, (double)var7, mouseButton);
               if (this.isWindowHover(this.editWindow, var6, var7) || WindowsScreen.draggingWindow == this.editWindow) {
                  return true;
               }
            }

            float var8 = frame.unitMouseX(mouseX);
            float var9 = frame.unitMouseY(mouseY);
            if (this.isLayoutMenuVisible() && this.handleLayoutMenuClick(mouseX, mouseY, mouseButton, frame)) {
               return true;
            } else if (!this.selectedHud.isEmpty() && mouseButton == 0 && this.isHoveringLayoutButton(var8, var9, frame)) {
               this.openLayoutMenu(frame);
               return true;
            } else if (this.hudComponent != null && this.hudComponent.isMouseOver(mouseX, mouseY)) {
               this.hudComponent.mouseClicked(mouseX, mouseY, mouseButton);
               return true;
            } else if (mouseButton == 0) {
               if (this.tryBeginDragHudElement(mouseX, mouseY)) {
                  return true;
               } else {
                  this.beginSelection(mouseX, mouseY);
                  return true;
               }
            } else if (mouseButton == 1 && !this.selectedHud.isEmpty()) {
               this.openLayoutMenu(frame);
               return true;
            } else {
               return false;
            }
         }
      } else {
         return false;
      }
   }

   private boolean isWindowHover(WindowBase window, int mouseX, int mouseY) {
      return Render2DUtil.isHovered(
         (double)mouseX, (double)mouseY, (double)window.getX(), (double)window.getY(), (double)window.getWidth(), (double)window.getHeight()
      );
   }

   private void syncEditWindowPosition(ClickGuiFrame frame) {
      if (this.editWindow != null) {
         float var2 = frame.baseX(ClickGuiScreen.Page.Hud);
         if (!this.editWindowPosInit) {
            this.editWindowLocalX = this.editWindow.getX() - var2 - frame.totalOffsetX;
            this.editWindowLocalY = this.editWindow.getY() - frame.totalOffsetY;
            this.editWindowPosInit = true;
         }

         if (WindowsScreen.draggingWindow != this.editWindow) {
            float var3 = var2 + this.editWindowLocalX + frame.totalOffsetX;
            float var4 = this.editWindowLocalY + frame.totalOffsetY;
            this.editWindow.setPosition(var3, var4);
         }
      }
   }

   private void updateEditWindowLocal(ClickGuiFrame frame) {
      if (this.editWindow != null) {
         if (WindowsScreen.draggingWindow == this.editWindow) {
            float var2 = frame.baseX(ClickGuiScreen.Page.Hud);
            this.editWindowLocalX = this.editWindow.getX() - var2 - frame.totalOffsetX;
            this.editWindowLocalY = this.editWindow.getY() - frame.totalOffsetY;
         }
      }
   }

   private void initHudButtons() {
      ArrayList<Module> var1 = new ArrayList<>();

      for (Module var3 : CrucifixBeta.MODULE.getModules()) {
         if (this.isHudComponentModule(var3)) {
            var1.add(var3);
         }
      }

      var1.sort(ClickGuiScreen.MODULE_NAME_COMPARATOR);
      this.hudComponent = new Component("HUD", Module.Category.Client, 100, 100, true);

      for (Module var5 : var1) {
         this.hudComponent.addButton(new ModuleButton(var5));
      }
   }

   private boolean isHudComponentModule(Module module) {
      return module instanceof HudModule || module instanceof HudSetting;
   }

   private boolean tryBeginDragHudElement(int mouseX, int mouseY) {
      if (this.hudComponent == null) {
         return false;
      } else {
         for (ModuleButton var4 : this.hudComponent.getItems()) {
            if (var4.getModule() instanceof HudModule var6 && var6.isOn() && var6.isHudHit(mouseX, mouseY)) {
               if (!this.selectedHud.isEmpty() && this.selectedHud.contains(var6)) {
                  this.beginGroupDrag(mouseX, mouseY);
                  return true;
               }

               this.groupDragging = false;
               this.groupDragEntries.clear();
               this.elementDragging = var6;
               this.elementDragDx = mouseX - var6.getHudBoundX();
               this.elementDragDy = mouseY - var6.getHudBoundY();
               return true;
            }
         }

         return false;
      }
   }

   private void dragHudElements(int mouseX, int mouseY) {
      if (this.groupDragging || this.elementDragging != null) {
         if (Wrapper.mc != null && Wrapper.mc.getWindow() != null) {
            long var3 = Wrapper.mc.getWindow().getHandle();
            if (GLFW.glfwGetMouseButton(var3, 0) != 1) {
               this.elementDragging = null;
               this.groupDragging = false;
               this.groupDragEntries.clear();
            } else if (!this.groupDragging) {
               int var9 = mouseX - this.elementDragDx;
               int var10 = mouseY - this.elementDragDy;
               this.elementDragging.setHudPosFromBounds(var9, var10);
            } else {
               int var5 = mouseX - this.groupDragStartMouseX;
               int var6 = mouseY - this.groupDragStartMouseY;

               for (ClickGuiHudPage.GroupDragEntry var8 : this.groupDragEntries) {
                  var8.module.setHudPosFromBounds(var8.startX + var5, var8.startY + var6);
               }
            }
         } else {
            this.elementDragging = null;
            this.groupDragging = false;
            this.groupDragEntries.clear();
         }
      }
   }

   private void beginSelection(int mouseX, int mouseY) {
      this.closeLayoutMenu();
      this.selecting = true;
      this.selectStartX = mouseX;
      this.selectStartY = mouseY;
      this.selectEndX = mouseX;
      this.selectEndY = mouseY;
      this.selectedHud.clear();
      this.groupDragging = false;
      this.groupDragEntries.clear();
      this.elementDragging = null;
      if (this.hudComponent != null) {
         this.hudComponent.drag = false;
      }
   }

   private void updateSelection(int mouseX, int mouseY) {
      if (this.selecting) {
         this.selectEndX = mouseX;
         this.selectEndY = mouseY;
         if (Wrapper.mc != null && Wrapper.mc.getWindow() != null) {
            long var3 = Wrapper.mc.getWindow().getHandle();
            if (GLFW.glfwGetMouseButton(var3, 0) != 1) {
               this.finishSelection();
            }
         } else {
            this.finishSelection();
         }
      }
   }

   private void beginGroupDrag(int mouseX, int mouseY) {
      this.closeLayoutMenu();
      this.groupDragging = true;
      this.groupDragStartMouseX = mouseX;
      this.groupDragStartMouseY = mouseY;
      this.groupDragEntries.clear();

      for (HudModule var4 : this.selectedHud) {
         if (var4.isOn()) {
            this.groupDragEntries.add(new ClickGuiHudPage.GroupDragEntry(var4, var4.getHudBoundX(), var4.getHudBoundY()));
         }
      }

      this.elementDragging = null;
      if (this.hudComponent != null) {
         this.hudComponent.drag = false;
      }
   }

   private void finishSelection() {
      if (this.selecting) {
         this.selecting = false;
         int var1 = this.selectStartX;
         int var2 = this.selectStartY;
         int var3 = this.selectEndX;
         int var4 = this.selectEndY;
         this.selectedHud.clear();

         for (Module var6 : CrucifixBeta.MODULE.getModules()) {
            if (var6 instanceof HudModule) {
               HudModule var7 = (HudModule)var6;
               if (var7.isOn() && var7.isHudOverlapping(var1, var2, var3, var4)) {
                  this.selectedHud.add(var7);
               }
            }
         }
      }
   }

   private void clearSelection() {
      this.selecting = false;
      this.selectedHud.clear();
      this.closeLayoutMenu();
   }

   private void renderSelectionOverlay(DrawContext context, ClickGuiFrame frame, int mouseX, int mouseY) {
      ClickGui var5 = ClickGui.getInstance();
      if (var5 != null) {
         boolean var6 = FontManager.isCustomFontEnabled();
         boolean var7 = FontManager.isShadowEnabled();
         boolean var8 = ClientSetting.INSTANCE != null && ClientSetting.INSTANCE.chinese.getValue();
         if (this.selecting) {
            int var9 = this.selectStartX;
            int var10 = this.selectStartY;
            int var11 = this.selectEndX;
            int var12 = this.selectEndY;
            float var13 = Math.min(frame.unitMouseX(var9), frame.unitMouseX(var11));
            float var14 = Math.min(frame.unitMouseY(var10), frame.unitMouseY(var12));
            float var15 = Math.abs(frame.unitMouseX(var11) - frame.unitMouseX(var9));
            float var16 = Math.abs(frame.unitMouseY(var12) - frame.unitMouseY(var10));
            Color var17 = ColorUtil.injectAlpha(var5.hoverColor.getValue(), 36);
            Color var18 = ColorUtil.injectAlpha(var5.hoverColor.getValue(), 170);
            Render2DUtil.drawRectWithOutline(context.getMatrices(), var13, var14, var15, var16, var17, var18);
         }

         double var19 = this.layoutButtonAnim.get(this.selectedHud.isEmpty() ? 0.0 : 1.0, 160L, Easing.CubicInOut);
         float var35 = (float)var19;
         if (!(var35 < 0.01F) || !this.selectedHud.isEmpty()) {
            Color var36 = ColorUtil.injectAlpha(var5.hoverColor.getValue(), (int)Math.round(210.0 * (double)var35));
            int var37 = Integer.MAX_VALUE;
            int var38 = Integer.MAX_VALUE;

            for (HudModule var41 : this.selectedHud) {
               int var43 = var41.getHudBoundW();
               int var45 = var41.getHudBoundH();
               if (var43 > 0 && var45 > 0) {
                  int var21 = var41.getHudBoundX();
                  int var22 = var41.getHudBoundY();
                  var37 = Math.min(var37, var21);
                  var38 = Math.min(var38, var22);
                  float var23 = frame.unitMouseX(var21);
                  float var24 = frame.unitMouseY(var22);
                  float var25 = frame.unitMouseX(var21 + var43) - frame.unitMouseX(var21);
                  float var26 = frame.unitMouseY(var22 + var45) - frame.unitMouseY(var22);
                  Render2DUtil.drawRectWithOutline(context.getMatrices(), var23, var24, var25, var26, new Color(0, 0, 0, 0), var36);
               }
            }

            if (var37 != Integer.MAX_VALUE) {
               String var40 = var8 ? "\u5e03\u5c40" : "Layout";
               float var42 = var6 ? FontManager.ui.getWidth(var40) : TextUtil.getWidth(var40);
               float var44 = var6 ? FontManager.ui.getFontHeight() : TextUtil.getHeight();
               float var46 = 4.0F;
               float var47 = var42 + var46 * 2.0F;
               float var48 = var44 + 4.0F;
               float var49 = frame.unitMouseX(var37);
               float var50 = frame.unitMouseY(var38) - var48 - 2.0F;
               float var51 = frame.scale == 0.0F ? (float)frame.screenH : ((float)frame.screenH - frame.slideY) / frame.scale;
               if (var50 < 1.0F) {
                  var50 = 1.0F;
               }

               if (var50 + var48 > var51 - 1.0F) {
                  var50 = var51 - var48 - 1.0F;
               }

               float var52 = 0.85F + 0.15F * var35;
               float var27 = var49 + var47 / 2.0F;
               float var28 = var50 + var48 / 2.0F;
               context.getMatrices().push();
               context.getMatrices().translate(var27, var28, 0.0F);
               context.getMatrices().scale(var52, var52, 1.0F);
               context.getMatrices().translate(-var27, -var28, 0.0F);
               int var29 = Math.max(0, Math.min(255, (int)Math.round(240.0 * (double)var35)));
               int var30 = Math.max(0, Math.min(255, (int)Math.round(200.0 * (double)var35)));
               Color var31 = ColorUtil.injectAlpha(var5.defaultColor.getValue(), var29);
               Color var32 = ColorUtil.injectAlpha(var5.hoverColor.getValue(), var30);
               Render2DUtil.drawRectWithOutline(context.getMatrices(), var49, var50, var47, var48, var31, var32);
               int var33 = Math.max(0, Math.min(255, (int)Math.round((double)var5.enableTextColor.getValue().getAlpha() * (double)var35)));
               int var34 = ColorUtil.injectAlpha(var5.enableTextColor.getValue(), var33).getRGB();
               TextUtil.drawString(context, var40, (double)(var49 + var46), (double)(var50 + 2.0F), var34, var6, var7);
               context.getMatrices().pop();
            }
         }
      }
   }

   private void openLayoutMenu(ClickGuiFrame frame) {
      this.layoutMenuOpen = true;
      if (this.selectedHud.isEmpty()) {
         this.layoutMenuX = 0;
         this.layoutMenuY = 0;
      } else {
         boolean var2 = FontManager.isCustomFontEnabled();
         boolean var3 = ClientSetting.INSTANCE != null && ClientSetting.INSTANCE.chinese.getValue();
         int var4 = Integer.MAX_VALUE;
         int var5 = Integer.MAX_VALUE;

         for (HudModule var7 : this.selectedHud) {
            int var8 = var7.getHudBoundW();
            int var9 = var7.getHudBoundH();
            if (var8 > 0 && var9 > 0) {
               var4 = Math.min(var4, var7.getHudBoundX());
               var5 = Math.min(var5, var7.getHudBoundY());
            }
         }

         if (var4 == Integer.MAX_VALUE) {
            this.layoutMenuX = 0;
            this.layoutMenuY = 0;
         } else {
            String var17 = var3 ? "\u5e03\u5c40" : "Layout";
            float var18 = var2 ? FontManager.ui.getWidth(var17) : TextUtil.getWidth(var17);
            float var19 = var2 ? FontManager.ui.getFontHeight() : TextUtil.getHeight();
            float var20 = 4.0F;
            float var10 = var18 + var20 * 2.0F;
            float var11 = var19 + 4.0F;
            float var12 = frame.unitMouseX(var4);
            float var13 = frame.unitMouseY(var5) - var11 - 2.0F;
            float var14 = frame.scale == 0.0F ? (float)frame.screenH : ((float)frame.screenH - frame.slideY) / frame.scale;
            if (var13 < 1.0F) {
               var13 = 1.0F;
            }

            if (var13 + var11 > var14 - 1.0F) {
               var13 = var14 - var11 - 1.0F;
            }

            float var15 = var12 + var10;
            this.layoutMenuX = Math.round(frame.scale == 0.0F ? var15 : var15 * frame.scale);
            this.layoutMenuY = Math.round(frame.scale == 0.0F ? var13 : var13 * frame.scale + frame.slideY);
         }
      }
   }

   private void closeLayoutMenu() {
      this.layoutMenuOpen = false;
   }

   private boolean isHoveringLayoutButton(float mx, float my, ClickGuiFrame frame) {
      if (this.selectedHud.isEmpty()) {
         return false;
      } else {
         boolean var4 = FontManager.isCustomFontEnabled();
         boolean var5 = ClientSetting.INSTANCE != null && ClientSetting.INSTANCE.chinese.getValue();
         int var6 = Integer.MAX_VALUE;
         int var7 = Integer.MAX_VALUE;

         for (HudModule var9 : this.selectedHud) {
            int var10 = var9.getHudBoundW();
            int var11 = var9.getHudBoundH();
            if (var10 > 0 && var11 > 0) {
               var6 = Math.min(var6, var9.getHudBoundX());
               var7 = Math.min(var7, var9.getHudBoundY());
            }
         }

         if (var6 == Integer.MAX_VALUE) {
            return false;
         } else {
            String var17 = var5 ? "\u5e03\u5c40" : "Layout";
            float var18 = var4 ? FontManager.ui.getWidth(var17) : TextUtil.getWidth(var17);
            float var19 = var4 ? FontManager.ui.getFontHeight() : TextUtil.getHeight();
            float var20 = 4.0F;
            float var12 = var18 + var20 * 2.0F;
            float var13 = var19 + 4.0F;
            float var14 = frame.unitMouseX(var6);
            float var15 = frame.unitMouseY(var7) - var13 - 2.0F;
            float var16 = frame.scale == 0.0F ? (float)frame.screenH : ((float)frame.screenH - frame.slideY) / frame.scale;
            if (var15 < 1.0F) {
               var15 = 1.0F;
            }

            if (var15 + var13 > var16 - 1.0F) {
               var15 = var16 - var13 - 1.0F;
            }

            return mx >= var14 && mx <= var14 + var12 && my >= var15 && my <= var15 + var13;
         }
      }
   }

   private String[] getLayoutMenuLabels(boolean chinese) {
      return chinese
         ? new String[]{"\u7f51\u683c (G)", "\u6c34\u5e73 (H)", "\u5782\u76f4 (V)"}
         : new String[]{"Grid (G)", "Horizontal (H)", "Vertical (V)"};
   }

   private boolean isLayoutMenuVisible() {
      if (!this.layoutMenuOpen && this.selectedHud.isEmpty()) {
         return false;
      } else {
         double var1 = this.layoutMenuAnim.get(this.layoutMenuOpen ? 1.0 : 0.0, 160L, Easing.CubicInOut);
         return var1 > 0.01;
      }
   }

   private boolean handleLayoutMenuClick(int mouseX, int mouseY, int mouseButton, ClickGuiFrame frame) {
      double var5 = this.layoutMenuAnim.get(this.layoutMenuOpen ? 1.0 : 0.0, 160L, Easing.CubicInOut);
      if (!(var5 <= 0.01) && !this.selectedHud.isEmpty()) {
         ClickGui var7 = ClickGui.getInstance();
         if (var7 == null) {
            this.closeLayoutMenu();
            return false;
         } else {
            boolean var8 = FontManager.isCustomFontEnabled();
            boolean var9 = FontManager.isShadowEnabled();
            boolean var10 = ClientSetting.INSTANCE != null && ClientSetting.INSTANCE.chinese.getValue();
            String[] var11 = this.getLayoutMenuLabels(var10);
            float var12 = frame.unitMouseX(mouseX);
            float var13 = frame.unitMouseY(mouseY);
            float var14 = frame.unitMouseX(this.layoutMenuX);
            float var15 = frame.unitMouseY(this.layoutMenuY);
            float var16 = var8 ? FontManager.ui.getFontHeight() : TextUtil.getHeight();
            float var17 = var16 + 6.0F;
            float var18 = 6.0F;
            float var19 = 0.0F;

            for (String var23 : var11) {
               float var24 = var8 ? FontManager.ui.getWidth(var23) : TextUtil.getWidth(var23);
               if (var24 > var19) {
                  var19 = var24;
               }
            }

            float var30 = var19 + var18 * 2.0F;
            float var31 = var17 * (float)var11.length + var18 * 2.0F;
            float var32 = (float)var5;
            float var33 = 0.85F + 0.15F * var32;
            float var34 = var30 * var33;
            float var25 = var31 * var33;
            boolean var26 = var12 >= var14 && var12 <= var14 + var34 && var13 >= var15 && var13 <= var15 + var25;
            if (!var26) {
               if (mouseButton == 0) {
                  this.closeLayoutMenu();
                  return false;
               } else {
                  this.closeLayoutMenu();
                  return true;
               }
            } else if (mouseButton != 0) {
               return true;
            } else {
               float var27 = (var12 - var14) / Math.max(1.0E-4F, var33);
               float var28 = (var13 - var15) / Math.max(1.0E-4F, var33);
               int var29 = (int)Math.floor((double)((var28 - var18) / var17));
               if (var29 >= 0 && var29 < var11.length) {
                  if (var29 == 0) {
                     this.applyGridLayout();
                  } else if (var29 == 1) {
                     this.applyHorizontalLayout();
                  } else if (var29 == 2) {
                     this.applyVerticalLayout();
                  }

                  this.closeLayoutMenu();
                  return true;
               } else {
                  return true;
               }
            }
         }
      } else {
         return false;
      }
   }

   private void renderLayoutMenu(DrawContext context, ClickGuiFrame frame, int mouseX, int mouseY) {
      double var5 = this.layoutMenuAnim.get(this.layoutMenuOpen ? 1.0 : 0.0, 160L, Easing.CubicInOut);
      float var7 = (float)var5;
      if (!(var7 <= 0.01F) && !this.selectedHud.isEmpty()) {
         ClickGui var8 = ClickGui.getInstance();
         if (var8 == null) {
            this.closeLayoutMenu();
         } else {
            boolean var9 = FontManager.isCustomFontEnabled();
            boolean var10 = FontManager.isShadowEnabled();
            boolean var11 = ClientSetting.INSTANCE != null && ClientSetting.INSTANCE.chinese.getValue();
            String[] var12 = this.getLayoutMenuLabels(var11);
            float var13 = frame.unitMouseX(this.layoutMenuX);
            float var14 = frame.unitMouseY(this.layoutMenuY);
            float var15 = var9 ? FontManager.ui.getFontHeight() : TextUtil.getHeight();
            float var16 = var15 + 6.0F;
            float var17 = 6.0F;
            float var18 = 0.0F;

            for (String var22 : var12) {
               float var23 = var9 ? FontManager.ui.getWidth(var22) : TextUtil.getWidth(var22);
               if (var23 > var18) {
                  var18 = var23;
               }
            }

            float var39 = var18 + var17 * 2.0F;
            float var40 = var16 * (float)var12.length + var17 * 2.0F;
            float var41 = frame.scale == 0.0F ? (float)frame.screenW : (float)frame.screenW / frame.scale;
            float var42 = frame.scale == 0.0F ? (float)frame.screenH : ((float)frame.screenH - frame.slideY) / frame.scale;
            if (var13 + var39 > var41 - 2.0F) {
               var13 = var41 - var39 - 2.0F;
            }

            if (var14 + var40 > var42 - 2.0F) {
               var14 = var42 - var40 - 2.0F;
            }

            if (var13 < 2.0F) {
               var13 = 2.0F;
            }

            if (var14 < 2.0F) {
               var14 = 2.0F;
            }

            float var43 = var13 + var39 / 2.0F;
            float var24 = var14 + var40 / 2.0F;
            float var25 = 0.85F + 0.15F * var7;
            context.getMatrices().push();
            context.getMatrices().translate(var43, var24, 0.0F);
            context.getMatrices().scale(var25, var25, 1.0F);
            context.getMatrices().translate(-var43, -var24, 0.0F);
            int var26 = Math.max(0, Math.min(255, (int)Math.round(245.0 * (double)var7)));
            int var27 = Math.max(0, Math.min(255, (int)Math.round(210.0 * (double)var7)));
            Color var28 = ColorUtil.injectAlpha(var8.defaultColor.getValue(), var26);
            Color var29 = ColorUtil.injectAlpha(var8.hoverColor.getValue(), var27);
            Render2DUtil.drawRectWithOutline(context.getMatrices(), var13, var14, var39, var40, var28, var29);
            float var30 = frame.unitMouseX(mouseX);
            float var31 = frame.unitMouseY(mouseY);
            float var32 = var13 + (var30 - var13) / Math.max(1.0E-4F, var25);
            float var33 = var14 + (var31 - var14) / Math.max(1.0E-4F, var25);
            int var34 = Math.max(0, Math.min(255, (int)Math.round((double)var8.enableTextColor.getValue().getAlpha() * (double)var7)));
            int var35 = ColorUtil.injectAlpha(var8.enableTextColor.getValue(), var34).getRGB();

            for (int var36 = 0; var36 < var12.length; var36++) {
               float var37 = var14 + var17 + (float)var36 * var16;
               boolean var38 = var32 >= var13 && var32 <= var13 + var39 && var33 >= var37 && var33 <= var37 + var16;
               if (var38) {
                  Render2DUtil.drawRect(
                     context.getMatrices(),
                     var13 + 1.0F,
                     var37,
                     var39 - 2.0F,
                     var16,
                     ColorUtil.injectAlpha(var8.hoverColor.getValue(), Math.max(0, Math.min(255, (int)Math.round(160.0 * (double)var7))))
                  );
               }

               TextUtil.drawString(context, var12[var36], (double)(var13 + var17), (double)(var37 + 2.0F), var35, var9, var10);
            }

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

   private void applyGridLayout() {
      if (!this.selectedHud.isEmpty()) {
         this.selectedHud.sort(Comparator.comparingInt(HudModule::getHudBoundY).thenComparingInt(HudModule::getHudBoundX));
         int var1 = Integer.MAX_VALUE;
         int var2 = Integer.MAX_VALUE;
         int var3 = 0;
         int var4 = 0;

         for (HudModule var6 : this.selectedHud) {
            var1 = Math.min(var1, var6.getHudBoundX());
            var2 = Math.min(var2, var6.getHudBoundY());
            var3 = Math.max(var3, Math.max(1, var6.getHudBoundW()));
            var4 = Math.max(var4, Math.max(1, var6.getHudBoundH()));
         }

         byte var15 = 2;
         int var16 = var3 + var15;
         int var7 = var4 + var15;
         int var8 = this.selectedHud.size();
         int var9 = (int)Math.ceil(Math.sqrt((double)var8));
         var9 = Math.max(1, var9);

         for (int var10 = 0; var10 < var8; var10++) {
            int var11 = var10 % var9;
            int var12 = var10 / var9;
            int var13 = var1 + var11 * var16;
            int var14 = var2 + var12 * var7;
            this.selectedHud.get(var10).setHudPosFromBounds(var13, var14);
         }
      }
   }

   private void applyHorizontalLayout() {
      if (!this.selectedHud.isEmpty()) {
         this.selectedHud.sort(Comparator.comparingInt(HudModule::getHudBoundX));
         int var1 = Integer.MAX_VALUE;

         for (HudModule var3 : this.selectedHud) {
            var1 = Math.min(var1, var3.getHudBoundY());
         }

         byte var7 = 2;
         int var8 = this.selectedHud.get(0).getHudBoundX();

         for (HudModule var5 : this.selectedHud) {
            var5.setHudPosFromBounds(var8, var1);
            int var6 = Math.max(1, var5.getHudBoundW());
            var8 += var6 + var7;
         }
      }
   }

   private void applyVerticalLayout() {
      if (!this.selectedHud.isEmpty()) {
         this.selectedHud.sort(Comparator.comparingInt(HudModule::getHudBoundY));
         int var1 = Integer.MAX_VALUE;

         for (HudModule var3 : this.selectedHud) {
            var1 = Math.min(var1, var3.getHudBoundX());
         }

         byte var7 = 2;
         int var8 = this.selectedHud.get(0).getHudBoundY();

         for (HudModule var5 : this.selectedHud) {
            var5.setHudPosFromBounds(var1, var8);
            int var6 = Math.max(1, var5.getHudBoundH());
            var8 += var6 + var7;
         }
      }
   }

   private void applyAlignLeft() {
      if (!this.selectedHud.isEmpty()) {
         int var1 = Integer.MAX_VALUE;

         for (HudModule var3 : this.selectedHud) {
            var1 = Math.min(var1, var3.getHudBoundX());
         }

         for (HudModule var5 : this.selectedHud) {
            var5.setHudPosFromBounds(var1, var5.getHudBoundY());
         }
      }
   }

   private void applyAlignTop() {
      if (!this.selectedHud.isEmpty()) {
         int var1 = Integer.MAX_VALUE;

         for (HudModule var3 : this.selectedHud) {
            var1 = Math.min(var1, var3.getHudBoundY());
         }

         for (HudModule var5 : this.selectedHud) {
            var5.setHudPosFromBounds(var5.getHudBoundX(), var1);
         }
      }
   }
@Environment(EnvType.CLIENT)
   private static final class GroupDragEntry {
      private final HudModule module;
      private final int startX;
      private final int startY;

      private GroupDragEntry(HudModule module, int startX, int startY) {
         this.module = module;
         this.startX = startX;
         this.startY = startY;
      }
   }
}
