package wtf.opal.client.feature.module.property.impl.number;

import com.google.gson.internal.LinkedTreeMap;
import com.ibm.icu.impl.Pair;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import wtf.opal.client.feature.module.property.Property;
import wtf.opal.client.feature.module.property.impl.mode.ModuleMode;
import wtf.opal.client.screen.click.dropdown.panel.property.PropertyPanel;
import wtf.opal.client.screen.click.dropdown.panel.property.impl.BoundedNumberPropertyComponent;
import wtf.opal.utility.misc.math.MathUtility;
import wtf.opal.utility.misc.math.RandomUtility;

@Environment(EnvType.CLIENT)
public final class BoundedNumberProperty extends Property<Pair<Double, Double>> {
   private final double minValue;
   private final double maxValue;
   private final double increment;
   private String suffix;

   public BoundedNumberProperty(String name, double defaultValue, double defaultValue2, double minValue, double maxValue, double increment) {
      super(name);
      this.minValue = minValue;
      this.maxValue = maxValue;
      this.increment = increment;
      this.setValue(Pair.of(defaultValue, defaultValue2));
   }

   public BoundedNumberProperty(
      String name, ModuleMode<?> parent, double defaultValue, double defaultValue2, double minValue, double maxValue, double increment
   ) {
      super(name, parent);
      this.minValue = minValue;
      this.maxValue = maxValue;
      this.increment = increment;
      this.setValue(Pair.of(defaultValue, defaultValue2));
   }

   public BoundedNumberProperty(String name, String suffix, double defaultValue, double defaultValue2, double minValue, double maxValue, double increment) {
      this(name, defaultValue, defaultValue2, minValue, maxValue, increment);
      this.suffix = suffix;
   }

   public BoundedNumberProperty(
      String name, ModuleMode<?> parent, String suffix, double defaultValue, double defaultValue2, double minValue, double maxValue, double increment
   ) {
      this(name, parent, defaultValue, defaultValue2, minValue, maxValue, increment);
      this.suffix = suffix;
   }

   public double getMaxValue() {
      return this.maxValue;
   }

   public double getMinValue() {
      return this.minValue;
   }

   public String getSuffix() {
      return this.suffix;
   }

   public Double getMidpoint() {
      return ((Double)this.getValue().first + (Double)this.getValue().second) / 2.0;
   }

   public Double getRandomValue() {
      return RandomUtility.getRandomDouble((Double)this.getValue().first, (Double)this.getValue().second);
   }

   public void setValue(Pair<Double, Double> value) {
      super.setValue(
         Pair.of(
            MathUtility.roundAndClamp((Number)value.first, this.minValue, this.maxValue, this.increment).doubleValue(),
            MathUtility.roundAndClamp((Number)value.second, this.minValue, this.maxValue, this.increment).doubleValue()
         )
      );
   }

   @Override
   public void applyValue(Object propertyValue) {
      if (propertyValue instanceof LinkedTreeMap var2) {
         if (var2.isEmpty()) {
            return;
         }

         Object var3 = var2.get("x");
         Object var4 = var2.get("y");
         if (var3 instanceof Double var5 && var4 instanceof Double var6) {
            double var7 = MathUtility.roundAndClamp(var5, this.minValue, this.maxValue, this.increment).doubleValue();
            double var9 = MathUtility.roundAndClamp(var6, this.minValue, this.maxValue, this.increment).doubleValue();
            this.setValue(Pair.of(var7, var9));
         }
      }
   }

   @Override
   public PropertyPanel<?> createClickGUIComponent() {
      return new BoundedNumberPropertyComponent(this);
   }
}
