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

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.utils.Wrapper;
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.modules.HudModule;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.impl.client.ClickGui;
import dev.crucifixbeta.mod.modules.impl.client.hud.HudSetting;
import java.util.ArrayList;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.InputUtil;

@Environment(EnvType.CLIENT)
public final class ClickGuiModulePage {
   private final ClickGuiScreen host;

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

   public void load() {
      ArrayList<Component> var1 = this.host.getComponents();
      var1.clear();
      int var2 = ClickGui.getInstance() != null ? ClickGui.getInstance().categoryWidth.getValueInt() : 101;
      int var3 = ClickGui.getInstance() != null ? ClickGui.getInstance().moduleButtonWidth.getValueInt() : 93;
      int var4 = Math.max(var2, var3);
      int var5 = var4 + 1;
      int var6 = 0;

      for (Module.Category var10 : Module.Category.values()) {
         if (this.shouldShowCategory(var10)) {
            var6++;
         }
      }

      int var16 = 10;
      int var17 = 4;
      if (Wrapper.mc != null && Wrapper.mc.getWindow() != null) {
         int var18 = Wrapper.mc.getWindow().getScaledWidth();
         int var20 = Wrapper.mc.getWindow().getScaledHeight();
         int var11 = var6 * var4 + (var6 - 1);
         var16 = Math.round(((float)var18 - (float)var11) / 2.0F);
         var17 = Math.round((float)var20 / 6.0F);
      }

      int var19 = Math.round(((float)var4 - (float)var3) / 2.0F);
      int var21 = var16 - var5;

      for (final Module.Category var14 : Module.Category.values()) {
         if (this.shouldShowCategory(var14)) {
            var21 += var5;
            Component var15 = new Component(this.getCategoryTitle(var14), var14, var21 + var19, var17, true) {
               @Override
               public void setupItems() {
                  for (Module var2 : CrucifixBeta.MODULE.getModules()) {
                     if (var2.getCategory().equals(var14) && !ClickGuiModulePage.this.isHudComponentModule(var2)) {
                        this.addButton(new ModuleButton(var2));
                     }
                  }
               }
            };
            var15.setOpenAnimationIndex(var1.size());
            var1.add(var15);
         }
      }

      var1.forEach(c -> c.getItems().sort(ClickGuiScreen.MODULE_NAME_COMPARATOR));
   }

   public void render(DrawContext context, int mouseX, int mouseY, float delta) {
      this.host.getComponents().forEach(c -> c.drawScreen(context, mouseX, mouseY, delta));
   }

   public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
      this.host.getComponents().forEach(c -> c.mouseClicked(mouseX, mouseY, mouseButton));
   }

   public void mouseReleased(int mouseX, int mouseY, int releaseButton) {
      this.host.getComponents().forEach(c -> c.mouseReleased(mouseX, mouseY, releaseButton));
   }

   public void keyPressed(int keyCode) {
      this.host.getComponents().forEach(c -> c.onKeyPressed(keyCode));
   }

   public void charTyped(char chr, int modifiers) {
      this.host.getComponents().forEach(c -> c.onKeyTyped(chr, modifiers));
   }

   public void mouseScrolled(double mouseX, double mouseY, double verticalAmount) {
      if (Wrapper.mc != null && Wrapper.mc.getWindow() != null) {
         if (InputUtil.isKeyPressed(Wrapper.mc.getWindow().getHandle(), 340)) {
            if (verticalAmount < 0.0) {
               this.host.getComponents().forEach(component -> component.setX(component.getTargetX() - 15));
            } else if (verticalAmount > 0.0) {
               this.host.getComponents().forEach(component -> component.setX(component.getTargetX() + 15));
            }
         } else {
            boolean var7 = false;

            for (Component var9 : this.host.getComponents()) {
               if (var9.isListMouseOver((int)mouseX, (int)mouseY)) {
                  var9.scrollList(verticalAmount);
                  var7 = true;
               }
            }

            if (!var7) {
               this.host.getComponents().forEach(component -> component.scrollList(verticalAmount));
            }
         }
      }
   }

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

   private boolean shouldShowCategory(Module.Category category) {
      return category == Module.Category.HUD ? false : category != Module.Category.Debug;
   }

   private String getCategoryTitle(Module.Category category) {
      return category == Module.Category.Debug ? "debug" : category.toString();
   }
}
