package net.vulkanmod.config.gui.widget;

import net.minecraft.class_1144;
import net.minecraft.class_11908;
import net.minecraft.class_2561;
import net.minecraft.class_310;
import net.minecraft.class_327;
import net.minecraft.class_3532;
import net.vulkanmod.config.gui.render.GuiRenderer;
import net.vulkanmod.config.option.RangeOption;
import net.vulkanmod.vulkan.util.ColorUtil;

public class RangeOptionWidget extends OptionWidget<RangeOption> {
   protected double value;
   private boolean focused;

   public RangeOptionWidget(RangeOption option, class_2561 name) {
      super(option, name);
      this.setValue((double)option.getScaledValue());
   }

   protected int getYImage(boolean hovered) {
      return 0;
   }

   protected void renderControls(double mouseX, double mouseY) {
      int valueX = this.controlX + (int)(this.value * (double)this.controlWidth);
      if (this.controlHovered && this.active) {
         int halfWidth = 2;
         int halfHeight = 4;
         int y0 = (int)((float)this.y + (float)this.height * 0.5F - 1.0F);
         int y1 = (int)((float)y0 + 2.0F);
         GuiRenderer.fill(this.controlX, y0, this.controlX + this.controlWidth, y1, ColorUtil.ARGB.pack(1.0F, 1.0F, 1.0F, 0.1F));
         GuiRenderer.fill(this.controlX, y0, valueX - halfWidth, y1, ColorUtil.ARGB.pack(1.0F, 1.0F, 1.0F, 0.3F));
         int color = ColorUtil.ARGB.pack(1.0F, 1.0F, 1.0F, 0.3F);
         GuiRenderer.renderBorder(valueX - halfWidth, y0 - halfHeight, valueX + halfWidth, y1 + halfHeight, 1, color);
      } else {
         int y0 = (int)((float)(this.y + this.height) - 5.0F);
         int y1 = (int)((float)y0 + 1.5F);
         GuiRenderer.fill(this.controlX, y0, this.controlX + this.controlWidth, y1, ColorUtil.ARGB.pack(1.0F, 1.0F, 1.0F, 0.3F));
         float alpha = this.active ? 0.8F : 0.3F;
         GuiRenderer.fill(this.controlX, y0, valueX, y1, ColorUtil.ARGB.pack(1.0F, 1.0F, 1.0F, alpha));
      }

      int color = this.active ? -1 : -6250336;
      class_327 font = class_310.method_1551().field_1772;
      class_2561 text = this.getDisplayedValue();
      int width = font.method_27525(text);
      int x = this.controlX + this.controlWidth / 2 - width / 2;
      int y = this.y + (this.height - 9) / 2;
      GuiRenderer.drawString(font, text.method_30937(), x, y, color);
   }

   public void onClick(double mouseX, double mouseY) {
      this.setValueFromMouse(mouseX);
   }

   public boolean method_25404(class_11908 event) {
      boolean isLeft = event.comp_4795() == 263;
      boolean isRight = event.comp_4795() == 262;
      if (isLeft || isRight) {
         float direction = isLeft ? -1.0F : 1.0F;
         this.setValue(this.value + (double)(direction / (float)(this.width - 8)));
      }

      return false;
   }

   public void method_25365(boolean bl) {
      this.focused = bl;
   }

   public boolean method_25370() {
      return this.focused;
   }

   private void setValueFromMouse(double mouseX) {
      this.setValue((mouseX - (double)(this.controlX + 4)) / (double)(this.controlWidth - 8));
   }

   private void setValue(double value) {
      double d = this.value;
      this.value = class_3532.method_15350(value, (double)0.0F, (double)1.0F);
      if (d != this.value) {
         this.applyValue();
      }

      this.updateDisplayedValue();
   }

   protected void onDrag(double mouseX, double mouseY, double deltaX, double deltaY) {
      this.setValueFromMouse(mouseX);
   }

   private void applyValue() {
      ((RangeOption)this.option).setValue((float)this.value);
      this.value = (double)((RangeOption)this.option).getScaledValue();
   }

   public void playDownSound(class_1144 soundManager) {
   }

   public void onRelease(double mouseX, double mouseY) {
      if (this.controlHovered) {
         super.playDownSound(class_310.method_1551().method_1483());
      }

   }
}
