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

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.mod.gui.clickgui.ClickGuiScreen;
import dev.crucifixbeta.mod.gui.items.Component;
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 dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
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;

@Environment(EnvType.CLIENT)
public class BooleanButton extends Button {
   private final BooleanSetting setting;
   private final Animation toggleAnimation = new Animation();

   public BooleanButton(BooleanSetting setting) {
      super(setting.getName());
      this.setting = setting;
      double var2 = setting.getValue() ? 1.0 : 0.0;
      this.toggleAnimation.from = var2;
      this.toggleAnimation.to = var2;
   }

   @Override
   public void drawScreen(DrawContext context, int mouseX, int mouseY, float partialTicks) {
      boolean var5 = this.isHovering(mouseX, mouseY);
      this.updateHoverSound(var5);
      double var6 = this.toggleAnimation.get(this.getState() ? 1.0 : 0.0, 160L, Easing.CubicInOut);
      double var8 = this.getColorDelay();
      float var10 = (float)this.width + 7.0F;
      float var11 = (float)this.height - 0.5F;
      float var12 = ClientSetting.INSTANCE != null ? ClientSetting.INSTANCE.moduleCorner.getValueFloat() : 0.0F;
      ColorsModule var13 = ColorsModule.INSTANCE;
      if (this.setting.hasParent()) {
         int var29 = var5 ? ClickGui.getInstance().hoverAlpha.getValueInt() : ClickGui.getInstance().alpha.getValueInt();
         Color var30 = new Color(var5 ? hoverColor : defaultColor, true);
         if (ClickGui.getInstance().colorMode.getValue() == ClickGui.ColorMode.Spectrum) {
            if (var6 >= 0.999) {
               Render2DUtil.drawLutRect(
                  context.getMatrices(),
                  this.x,
                  this.y,
                  var10,
                  var11,
                  ClickGui.getInstance().getSpectrumLutId(),
                  ClickGui.getInstance().getSpectrumLutHeight(),
                  var29
               );
            } else {
               Render2DUtil.drawRoundedRect(context.getMatrices(), this.x, this.y, var10, var11, var12, var30);
               int var31 = (int)Math.round((double)var29 * var6);
               if (var31 > 0) {
                  Render2DUtil.drawLutRect(
                     context.getMatrices(),
                     this.x,
                     this.y,
                     var10,
                     var11,
                     ClickGui.getInstance().getSpectrumLutId(),
                     ClickGui.getInstance().getSpectrumLutHeight(),
                     var31
                  );
               }
            }
         } else {
            Color var32 = var13 != null && var13.colorMode.getValue() == ColorsModule.ColorMode.Static
               ? var13.toggleOn.getValue()
               : ClickGui.getInstance().getActiveColor(var8);
            Color var34 = new Color(var32.getRed(), var32.getGreen(), var32.getBlue(), var32.getAlpha());
            Color var35 = ColorUtil.fadeColor(var30, var34, var6);
            Render2DUtil.drawRoundedRect(context.getMatrices(), this.x, this.y, var10, var11, var12, var35);
         }

         float var33 = this.getCenteredTextY(this.y, (float)this.height - 0.5F);
         if (var5 && InputUtil.isKeyPressed(mc.getWindow().getHandle(), 340)) {
            this.drawString("Reset Default", (double)(this.x + 2.3F), (double)var33, enableTextColor);
         } else {
            this.drawSettingString(this.setting.getDisplayName(), (double)(this.x + 2.3F), (double)var33, this.getState() ? enableTextColor : defaultTextColor);
         }

         this.drawString(
            this.setting.isOpen() ? "-" : "+",
            (double)(this.x + (float)this.width - 1.0F),
            (double)var33,
            ClickGui.getInstance().gear.getValue()
         );
      } else {
         Color var14 = new Color(var5 ? hoverColor : defaultColor, true);
         Render2DUtil.drawRoundedRect(context.getMatrices(), this.x, this.y, var10, var11, var12, var14);
         byte var15 = 28;
         byte var16 = 14;
         float var17 = this.x + (float)this.width - (float)var15 - 4.0F;
         float var18 = this.y + (var11 - (float)var16) / 2.0F;
         Color var19 = var13 != null ? var13.toggleOff.getValue() : new Color(225, 228, 232, 145);
         Color var20 = var13 != null && var13.colorMode.getValue() == ColorsModule.ColorMode.Static
            ? var13.accent.getValue()
            : ClickGui.getInstance().getActiveColor(var8);
         int var21 = Math.min(255, var19.getAlpha() + (var5 ? 24 : 0));
         Color var22 = ColorUtil.injectAlpha(var19, var21);
         Color var24 = ColorUtil.fadeColor(var22, var20, var6);
         Render2DUtil.drawRoundedRect(context.getMatrices(), var17, var18, (float)var15, (float)var16, (float)var16 / 2.0F, var24);
         float var25 = 5.5F;
         float var26 = var17 + 7.0F + (float)(14.0 * var6);
         float var27 = var18 + (float)var16 / 2.0F;
         Render2DUtil.drawCircle(context.getMatrices(), var26, var27, var25 + 0.85F, new Color(247, 249, 250, 48), 72);
         Render2DUtil.drawCircle(context.getMatrices(), var26, var27, var25 + 0.35F, new Color(247, 249, 250, 118), 72);
         Render2DUtil.drawCircle(context.getMatrices(), var26, var27, var25 - 0.35F, new Color(247, 249, 250, 248), 72);
         float var28 = this.getCenteredTextY(this.y, (float)this.height - 0.5F);
         if (var5 && InputUtil.isKeyPressed(mc.getWindow().getHandle(), 340)) {
            this.drawString("Reset Default", (double)(this.x + 2.3F), (double)var28, enableTextColor);
         } else {
            this.drawSettingString(this.setting.getDisplayName(), (double)(this.x + 2.3F), (double)var28, this.getState() ? enableTextColor : defaultTextColor);
         }

         if (this.setting.hasParent()) {
            this.drawString(
               this.setting.isOpen() ? "-" : "+",
               (double)(var17 - 10.0F),
               (double)var28,
               ClickGui.getInstance().gear.getValue()
            );
         }
      }
   }

   @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) && InputUtil.isKeyPressed(mc.getWindow().getHandle(), 340)) {
         if (this.setting.hasParent()) {
            boolean var4 = false;

            for (Component var6 : ClickGuiScreen.getInstance().getComponents()) {
               for (ModuleButton var8 : var6.getItems()) {
                  if (var8.getModule().getSettings().contains(this.setting)) {
                     var8.resetChildSettingsToDefault(this.setting);
                     var4 = true;
                     break;
                  }
               }

               if (var4) {
                  break;
               }
            }
         }

         this.setting.setValue(this.setting.getDefaultValue());
         sound();
      } else {
         super.mouseClicked(mouseX, mouseY, mouseButton);
         if (mouseButton == 1 && this.isHovering(mouseX, mouseY)) {
            sound();
            this.setting.setOpen(!this.setting.isOpen());
         }
      }
   }

   @Override
   public void toggle() {
      this.setting.setValue(!this.setting.getValue());
   }

   @Override
   public boolean getState() {
      return this.setting.getValue();
   }
}
