package wtf.opal.client.screen.click.dropdown.panel.property.impl;

import dev.crucifixbeta.mod.modules.impl.client.ClientSetting;
import java.util.Locale;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import wtf.opal.client.feature.module.property.impl.number.NumberProperty;
import wtf.opal.client.renderer.NVGRenderer;
import wtf.opal.client.renderer.repository.FontRepository;
import wtf.opal.client.renderer.text.NVGTextRenderer;
import wtf.opal.client.screen.click.dropdown.panel.property.PropertyPanel;
import wtf.opal.utility.misc.HoverUtility;
import wtf.opal.utility.misc.math.MathUtility;
import wtf.opal.utility.render.ColorUtility;
import wtf.opal.utility.render.animation.Animation;
import wtf.opal.utility.render.animation.Easing;

@Environment(EnvType.CLIENT)
public final class NumberPropertyComponent extends PropertyPanel<NumberProperty> {
   private boolean dragging;
   private Animation dragAnimation;

   public NumberPropertyComponent(NumberProperty property) {
      super(property);
   }

   @Override
   public void render(DrawContext context, int mouseX, int mouseY, float delta) {
      this.setHeight(15.0F);
      super.render(context, mouseX, mouseY, delta);
      NumberProperty var5 = this.getProperty();
      NVGTextRenderer var6 = FontRepository.getFont("productsans-medium");
      float var7 = this.width - 12.0F;
      float var8 = 2.5F;
      float var9 = this.x + 6.0F;
      float var10 = this.y + 13.0F;
      if (this.dragging && mouseX != -1) {
         float var11 = Math.min(1.0F, Math.max(0.0F, ((float)mouseX - var9) / var7));
         var5.setValue(MathUtility.interpolate(var5.getMinValue(), var5.getMaxValue(), (double)var11));
      }

      String var20 = formatValue(var5.getValue(), var5.getSuffix());
      var6.drawString(var5.getName() + ":" + var20, this.x + 5.0F, this.y + 8.5F, 7.0F, -1);
      double var12 = (var5.getValue() - var5.getMinValue()) / (var5.getMaxValue() - var5.getMinValue());
      double var14 = (double)var7 * var12;
      long var16 = ClientSetting.sliderFollowDuration(50L);
      if (this.dragAnimation == null) {
         this.dragAnimation = new Animation(Easing.LINEAR, var16);
         this.dragAnimation.setValue((float)var14);
      } else {
         this.dragAnimation.setDuration(var16);
         this.dragAnimation.run((float)var14);
      }

      NVGRenderer.roundedRect(var9, var10, var7, 2.5F, 1.25F, ColorUtility.scaleOpacity(ColorUtility.getModuleBackgroundColor(), 0.82F));
      float var18 = this.dragAnimation.getValue();
      if (var18 > 1.0F) {
         int var19 = (Integer)ColorUtility.getClientTheme().first;
         NVGRenderer.roundedRect(var9, var10, var18, 2.5F, 1.25F, var19);
      }

      NVGRenderer.roundedRect(var9 + var18 - 1.0F, var10 - 1.3F, 2.0F, 5.0F, 1.0F, -1);
      var6.drawString(var20, var9 + var18 - var6.getStringWidth(var20, 5.5F) / 2.0F, this.y + 22.0F, 5.5F, ColorUtility.applyOpacity(-1, 0.8F));
   }

   @Override
   public void mouseClicked(double mouseX, double mouseY, int button) {
      if (HoverUtility.isHovering(this.x, this.y, this.width, this.height, mouseX, mouseY) && button == 0) {
         this.dragging = true;
      }
   }

   @Override
   public void mouseReleased(double mouseX, double mouseY, int button) {
      if (this.dragging && button == 0) {
         this.dragging = false;
      }
   }

   private static String formatValue(Number value, String suffix) {
      String var2;
      if (value.doubleValue() == (double)value.intValue()) {
         var2 = String.valueOf(value.intValue());
      } else {
         var2 = String.format(Locale.ROOT, "%.3f", value.doubleValue())
            .replaceAll("0+$", "")
            .replaceAll("\\.$", "");
      }

      return suffix == null ? var2 : var2 + suffix;
   }
}
