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

import com.ibm.icu.impl.Pair;
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.BoundedNumberProperty;
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 BoundedNumberPropertyComponent extends PropertyPanel<BoundedNumberProperty> {
   private boolean draggingLow;
   private boolean draggingHigh;
   private Animation draggingLowAnimation;
   private Animation draggingHighAnimation;

   public BoundedNumberPropertyComponent(BoundedNumberProperty property) {
      super(property);
   }

   @Override
   public void render(DrawContext context, int mouseX, int mouseY, float delta) {
      this.setHeight(26.0F);
      super.render(context, mouseX, mouseY, delta);
      BoundedNumberProperty var5 = this.getProperty();
      NVGTextRenderer var6 = FontRepository.getFont("productsans-medium");
      Pair var7 = var5.getValue();
      Double var8 = (Double)var7.first;
      Double var9 = (Double)var7.second;
      float var10 = this.width - 12.0F;
      float var11 = 2.5F;
      float var12 = this.x + 6.0F;
      float var13 = this.y + 13.0F;
      float var14 = Math.min(1.0F, Math.max(0.0F, ((float)mouseX - var12) / var10));
      if (mouseX != -1) {
         double var15 = MathUtility.interpolate(var5.getMinValue(), var5.getMaxValue(), (double)var14);
         if (this.draggingLow && var15 <= var9) {
            var5.setValue(Pair.of(var15, var9));
         }

         if (this.draggingHigh && var15 >= var8) {
            var5.setValue(Pair.of(var8, var15));
         }
      }

      var7 = var5.getValue();
      var8 = (Double)var7.first;
      var9 = (Double)var7.second;
      String var17 = formatValue(var8, var5.getSuffix());
      String var18 = formatValue(var9, var5.getSuffix());
      var6.drawString(var5.getName() + ":" + var17 + "-" + var18, this.x + 5.0F, this.y + 8.5F, 7.0F, -1);
      double var19 = (var8 - var5.getMinValue()) / (var5.getMaxValue() - var5.getMinValue());
      double var21 = (var9 - var5.getMinValue()) / (var5.getMaxValue() - var5.getMinValue());
      double var23 = (double)var10 * var19;
      long var25 = ClientSetting.sliderFollowDuration(50L);
      if (this.draggingLowAnimation == null) {
         this.draggingLowAnimation = new Animation(Easing.LINEAR, var25);
         this.draggingLowAnimation.setValue((float)var23);
      } else {
         this.draggingLowAnimation.setDuration(var25);
         this.draggingLowAnimation.run((float)var23);
      }

      double var27 = (double)var10 * var21;
      if (this.draggingHighAnimation == null) {
         this.draggingHighAnimation = new Animation(Easing.LINEAR, var25);
         this.draggingHighAnimation.setValue((float)var27);
      } else {
         this.draggingHighAnimation.setDuration(var25);
         this.draggingHighAnimation.run((float)var27);
      }

      NVGRenderer.roundedRect(var12, var13, var10, 2.5F, 1.25F, ColorUtility.scaleOpacity(ColorUtility.getModuleBackgroundColor(), 0.82F));
      float var29 = this.draggingLowAnimation.getValue();
      float var30 = this.draggingHighAnimation.getValue();
      if (var30 > var29) {
         int var31 = (Integer)ColorUtility.getClientTheme().first;
         NVGRenderer.roundedRect(var12 + var29, var13, var30 - var29, 2.5F, 1.25F, var31);
      }

      NVGRenderer.roundedRect(var12 + var29 - 1.0F, var13 - 1.3F, 2.0F, 5.0F, 1.0F, -1);
      NVGRenderer.roundedRect(var12 + var30 - 1.0F, var13 - 1.3F, 2.0F, 5.0F, 1.0F, -1);
      var6.drawString(var17, var12 + var29 - var6.getStringWidth(var17, 5.5F) / 2.0F, this.y + 22.0F, 5.5F, ColorUtility.applyOpacity(-1, 0.8F));
      var6.drawString(var18, var12 + var30 - var6.getStringWidth(var18, 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) {
         float var6 = this.width - 12.0F;
         float var7 = this.x + 6.0F;
         BoundedNumberProperty var8 = this.getProperty();
         Pair var9 = var8.getValue();
         double var10 = ((Double)var9.first - var8.getMinValue()) / (var8.getMaxValue() - var8.getMinValue());
         double var12 = ((Double)var9.second - var8.getMinValue()) / (var8.getMaxValue() - var8.getMinValue());
         float var14 = var7 + (float)((double)var6 * var10);
         float var15 = var7 + (float)((double)var6 * var12);
         double var16 = Math.abs(mouseX - (double)var14);
         double var18 = Math.abs(mouseX - (double)var15);
         if (var16 == var18) {
            if (mouseX < (double)var14) {
               this.draggingLow = true;
            } else {
               this.draggingHigh = true;
            }
         } else if (var16 < var18) {
            this.draggingLow = true;
         } else {
            this.draggingHigh = true;
         }
      }
   }

   @Override
   public void mouseReleased(double mouseX, double mouseY, int button) {
      if (button == 0) {
         this.draggingHigh = this.draggingLow = 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;
   }
}
