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

import dev.crucifixbeta.api.utils.render.Render2DUtil;
import dev.crucifixbeta.mod.modules.impl.client.ClickGui;
import dev.crucifixbeta.mod.modules.settings.impl.BindSetting;
import java.awt.Color;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.InputUtil;
import net.minecraft.util.Formatting;

@Environment(EnvType.CLIENT)
public class BindButton extends Button {
   private final BindSetting setting;
   public boolean isListening;

   public BindButton(BindSetting setting) {
      super(setting.getName());
      this.setting = setting;
   }

   @Override
   public void drawScreen(DrawContext context, int mouseX, int mouseY, float partialTicks) {
      boolean var5 = this.isHovering(mouseX, mouseY);
      this.updateHoverSound(var5);
      double var6 = this.getColorDelay();
      float var8 = (float)this.width + 7.0F;
      float var9 = (float)this.height - 0.5F;
      if (!this.getState() && ClickGui.getInstance().colorMode.getValue() == ClickGui.ColorMode.Spectrum) {
         int var13 = !var5 ? ClickGui.getInstance().alpha.getValueInt() : ClickGui.getInstance().hoverAlpha.getValueInt();
         Render2DUtil.drawLutRect(
            context.getMatrices(), this.x, this.y, var8, var9, ClickGui.getInstance().getSpectrumLutId(), ClickGui.getInstance().getSpectrumLutHeight(), var13
         );
      } else {
         Color var10 = ClickGui.getInstance().getColor(var6);
         Render2DUtil.rect(
            context.getMatrices(),
            this.x,
            this.y,
            this.x + var8,
            this.y + (float)this.height - 0.5F,
            this.getState() ? (!var5 ? defaultColor : hoverColor) : var10.getRGB()
         );
      }

      float var14 = this.getCenteredTextY(this.y, (float)this.height - 0.5F);
      boolean var11 = this.shouldShowHoldMode(mouseX, mouseY);
      if (this.isListening) {
         this.drawString("Press keyCodec Key...", (double)(this.x + 2.3F), (double)var14, enableTextColor);
      } else {
         String var12 = this.setting.getKeyString();
         if (var11) {
            if (this.setting.isHoldEnable()) {
               this.drawSettingString(this.setting.getDisplayName() + " §7切换/§f按住", (double)(this.x + 2.3F), (double)var14, enableTextColor);
            } else {
               this.drawSettingString(this.setting.getDisplayName() + " §f切换§7/按住", (double)(this.x + 2.3F), (double)var14, enableTextColor);
            }
         } else {
            this.drawSettingString(
               this.setting.getDisplayName() + " " + Formatting.GRAY + var12,
               (double)(this.x + 2.3F),
               (double)var14,
               this.getState() ? enableTextColor : defaultTextColor
            );
         }
      }
   }

   @Override
   public void update() {
      this.setHidden(!this.setting.isVisible());
   }

   @Override
   public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
      if (mouseButton == 0 && this.isHovering(mouseX, mouseY)) {
         if (this.shouldShowHoldMode(mouseX, mouseY)) {
            this.setting.setHoldEnable(!this.setting.isHoldEnable());
            this.setting.setPressed(false);
            this.setting.holding = false;
            sound();
         } else {
            this.onMouseClick();
         }
      } else if (mouseButton == 1 && this.isHovering(mouseX, mouseY) && !this.isListening) {
         this.setting.setValue(-1);
         this.setting.setPressed(false);
         this.setting.holding = false;
         sound();
      } else if (this.isListening) {
         this.setting.setValue(-mouseButton - 2);
         this.onMouseClick();
      }
   }

   @Override
   public void onKeyPressed(int key) {
      if (this.isListening) {
         if (key != 256 && key != 259 && key != 261) {
            this.setting.setValue(key);
         } else {
            this.setting.setValue(-1);
         }

         this.onMouseClick();
      }
   }

   @Override
   public void toggle() {
      this.isListening = !this.isListening;
   }

   @Override
   public boolean getState() {
      return !this.isListening;
   }

   private boolean isShiftDown() {
      long var1 = mc.getWindow().getHandle();
      return InputUtil.isKeyPressed(var1, 340) || InputUtil.isKeyPressed(var1, 344);
   }

   private boolean shouldShowHoldMode(int mouseX, int mouseY) {
      return this.isHovering(mouseX, mouseY) && this.isShiftDown() && this.setting.getName().equalsIgnoreCase("Key");
   }
}
