package dev.crucifixbeta.mod.gui.items.buttons;

import dev.crucifixbeta.api.utils.render.Render2DUtil;
import dev.crucifixbeta.mod.gui.clickgui.ClickGuiScreen;
import dev.crucifixbeta.mod.gui.items.Component;
import dev.crucifixbeta.mod.gui.items.Item;
import dev.crucifixbeta.mod.modules.impl.client.ClickGui;
import dev.crucifixbeta.mod.modules.impl.client.ClientSetting;
import dev.crucifixbeta.mod.modules.impl.client.ColorsModule;
import java.awt.Color;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;

@Environment(EnvType.CLIENT)
public class Button extends Item {
   private boolean state;
   private boolean wasHovering;
   public static int hoverColor = -2007673515;
   public static int defaultColor = 290805077;
   public static int defaultTextColor = -5592406;
   public static int enableTextColor = -1;

   public Button(String name) {
      super(name);
      this.setHeight(15);
   }

   @Override
   public void drawScreen(DrawContext context, int mouseX, int mouseY, float partialTicks) {
      boolean var5 = this.isHovering(mouseX, mouseY);
      this.updateHoverSound(var5);
      float var6 = (float)this.height - 0.5F;
      float var7 = ClientSetting.INSTANCE != null ? ClientSetting.INSTANCE.moduleCorner.getValueFloat() : 0.0F;
      if (this.getState()) {
         int var8 = var5 ? ClickGui.getInstance().hoverAlpha.getValueInt() : ClickGui.getInstance().alpha.getValueInt();
         double var9 = this.getColorDelay();
         if (ClickGui.getInstance().colorMode.getValue() == ClickGui.ColorMode.Spectrum) {
            Render2DUtil.drawLutRect(
               context.getMatrices(),
               this.x,
               this.y,
               (float)this.width,
               var6,
               ClickGui.getInstance().getSpectrumLutId(),
               ClickGui.getInstance().getSpectrumLutHeight(),
               var8
            );
         } else {
            ColorsModule var11 = ColorsModule.INSTANCE;
            Color var12 = var11 != null && var11.colorMode.getValue() == ColorsModule.ColorMode.Static
               ? var11.moduleBgOn.getValue()
               : ClickGui.getInstance().getActiveColor(var9);
            Render2DUtil.drawRoundedRect(context.getMatrices(), this.x, this.y, (float)this.width, var6, var7, var12);
         }
      } else {
         Render2DUtil.drawRoundedRect(context.getMatrices(), this.x, this.y, (float)this.width, var6, var7, new Color(!var5 ? defaultColor : hoverColor, true));
      }

      float var13 = this.getCenteredTextY(this.y, (float)this.height - 0.5F);
      this.drawString(this.getName(), (double)(this.x + 2.3F), (double)var13, this.getState() ? enableTextColor : defaultTextColor);
   }

   @Override
   public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
      if (mouseButton == 0 && this.isHovering(mouseX, mouseY)) {
         this.onMouseClick();
      }
   }

   public void onMouseClick() {
      this.state = !this.state;
      this.toggle();
      sound();
   }

   public void toggle() {
   }

   public boolean getState() {
      return this.state;
   }

   protected void updateHoverSound(boolean hovered) {
      if (hovered && !this.wasHovering) {
         hoverSound();
      }

      this.wasHovering = hovered;
   }

   @Override
   public int getHeight() {
      return this.height - 1;
   }

   public boolean isHovering(int mouseX, int mouseY) {
      for (Component var4 : ClickGuiScreen.getInstance().getComponents()) {
         if (var4.drag) {
            return false;
         }
      }

      return (float)mouseX >= this.getX()
         && (float)mouseX <= this.getX() + (float)this.getWidth()
         && (float)mouseY >= this.getY()
         && (float)mouseY <= this.getY() + (float)this.height - 1.0F;
   }
}
