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

import dev.crucifixbeta.api.utils.math.Timer;
import dev.crucifixbeta.api.utils.render.Render2DUtil;
import dev.crucifixbeta.mod.modules.impl.client.ClickGui;
import dev.crucifixbeta.mod.modules.settings.impl.StringSetting;
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.client.util.SelectionManager;
import net.minecraft.util.Formatting;

@Environment(EnvType.CLIENT)
public class StringButton extends Button {
   private static final Timer idleTimer;
   private static boolean idle;
   private final StringSetting setting;
   public boolean isListening;
   private String currentString = "";

   public StringButton(StringSetting setting) {
      super(setting.getName());
      this.setting = setting;
   }

   public static String removeLastChar(String str) {
      String var1 = "";
      if (str != null && !str.isEmpty()) {
         var1 = str.substring(0, str.length() - 1);
      }

      return var1;
   }

   public static String getIdleSign() {
      if (idleTimer.passed(500L)) {
         idle = !idle;
         idleTimer.reset();
      }

      return idle ? "_" : "";
   }

   @Override
   public void drawScreen(DrawContext context, int mouseX, int mouseY, float partialTicks) {
      String var10000 = "\u0000\u0000\u0001\u0000\u0017ü¼\u007f\u0014oîæp\u008b\u009bMI?på\u0013{fÝ±\\`\u009a\u009c\u0098\u0011!?ýß*0ÅÜ_p±rFâW\u0095%÷±0\râ{\u001bt_îg\u0091äë&$¨y\u0004þ¶¶ö\u000b²5\u009f¥ãÌ¾F~½³' @y\u0081ð¾Ä<\u0098Ô{>\nOsÝk\u0013îxíT±{\u008ex>÷¼zé³tZ|\u008a\u001c¤í\táº\u0015vÊ\u0081}2\u008e0xÓ\u0010Ká >°\u0080}æûÏ\u0015øÐ¡ìjØýüôÉ§\u007f´\u0019\u001a\u0003´)E\u0081{${©ïôÜö\u0013Æ§\u001dºÕ!\u0015Ñ(åY ¸»>\u0002qmç\u0090+À\u0088XÞ\u008cGFÝ6(ÍZ¯ñ=ÛA²\u0097\\\u001e\bIÃK'ìÊ\u0082ºå \u009c\u0090Ï\u0087\u0003\u0000\u0002^k\b¹©Ä\u009a#ë¹\u008cº\u008däzÆG\u001dÀ]k8\u0081\u0000\u0000\u0001\u0000\u000b&»6)(Gg>\u000fñï|\bÚ\füCh\u000e\\gÑ\u00054KöW\u0095\u009a;1ç\u0097\"\u001cßP\u0003ª\u008bÅ\u0090Çä\"\u001fF\u008aºà\u0006\f\u001c\u0093D\u0091iQÖ\u0092\\lt\u0092¡PV\u0088<\u0014×\u0099´E\u0017®\u008fªÙu\u009321°N\"\u0085Nà\tèÁS\u0003NO\u0014\u0094Ê\u0091«Ä\u0000Ô\u001b_\u001d\u00195HÂ=\u008c\u0090&\u0000Ú\u0080å\u0099Î\u0018ú\u0092¥½ó\u0094Q`²+\u008aS¡\te\u0018{°Ù\u0090Ë\u001c\u0086ÄÁpB#ß³ªß\u0094y\u0094N\u0013UZ{ÄºO°$\u009d\fñ\u000bË\u008aö\u009cÆ \u008cñÄ)f{¢\u0005×\u0018\u0086¤\rc\u0089\u0006\u0095·çY\u0084\nÊX&Þ6T\u00015æ\u0089 Ú6î\u000b{P\u0080&ü|ÚÉ\u008d\u009dÿAã\u0001\u001ca\u0086cÉÖÝ\u0007Kÿhä\u0006´\u0084\u0096A(\u008bA>IñúSz2\u0000\u0000\u00003\u008aÛ;çã<\u001fÜK4g¹\"Û»¾Qm,÷h!²CN{ôvÐ!ðÁIÃÇ\u000eê²WçRê\f\u00adú\u0003 \u0094\u0013²\"";
      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 (ClickGui.getInstance().colorMode.getValue() == ClickGui.ColorMode.Spectrum && this.getState()) {
         int var11 = !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(), var11
         );
      } 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() ? var10.getRGB() : (!var5 ? defaultColor : hoverColor)
         );
      }

      float var12 = this.getCenteredTextY(this.y, (float)this.height - 0.5F);
      if (this.isListening) {
         this.drawString(this.currentString + getIdleSign(), (double)(this.x + 2.3F), (double)var12, this.getState() ? enableTextColor : defaultTextColor);
      } else if (this.isHovering(mouseX, mouseY) && InputUtil.isKeyPressed(mc.getWindow().getHandle(), 340)) {
         this.drawString("Reset Default", (double)(this.x + 2.3F), (double)var12, enableTextColor);
      } else {
         this.drawSettingString(
            this.setting.getDisplayName() + ": " + Formatting.GRAY + this.setting.getValue(),
            (double)(this.x + 2.3F),
            (double)var12,
            this.getState() ? enableTextColor : defaultTextColor
         );
      }
   }

   @Override
   public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
      if (mouseButton == 0 && this.isHovering(mouseX, mouseY) && InputUtil.isKeyPressed(mc.getWindow().getHandle(), 340)) {
         this.isListening = false;
         this.currentString = "";
         this.setting.setValue(this.setting.getDefaultValue());
         sound();
      } else {
         super.mouseClicked(mouseX, mouseY, mouseButton);
      }
   }

   @Override
   public void onKeyTyped(char typedChar, int keyCode) {
      if (this.isListening) {
         this.setString(this.currentString + typedChar);
      }
   }

   @Override
   public void onKeyPressed(int key) {
      if (this.isListening) {
         switch (key) {
            case 86:
               if (InputUtil.isKeyPressed(mc.getWindow().getHandle(), 341)) {
                  this.setString(this.currentString + SelectionManager.getClipboard(mc));
               }
               break;
            case 256:
               this.isListening = false;
               break;
            case 257:
            case 335:
               this.enterString();
               break;
            case 259:
               this.setString(removeLastChar(this.currentString));
         }
      }
   }

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

   private void enterString() {
      if (this.currentString.isEmpty()) {
         this.setting.setValue(this.setting.getDefaultValue());
      } else {
         this.setting.setValue(this.currentString);
      }

      this.onMouseClick();
   }

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

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

   public void setString(String newString) {
      this.currentString = newString;
   }

   static {

      idleTimer = new Timer();
   }
}
