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

import com.mojang.blaze3d.systems.RenderSystem;
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.core.impl.CommandManager;
import dev.crucifixbeta.mod.modules.impl.client.ClickGui;
import dev.crucifixbeta.mod.modules.settings.impl.ColorSetting;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.Objects;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BufferRenderer;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.render.VertexFormat.DrawMode;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.util.math.MatrixStack;
import org.lwjgl.glfw.GLFW;

@Environment(EnvType.CLIENT)
public class PickerButton extends Button {
   static MatrixStack matrixStack;
   private final ColorSetting setting;
   boolean pickingColor;
   boolean pickingHue;
   boolean pickingAlpha;
   boolean open;
   private final Animation openAnimation = new Animation();
   private final Animation pickerCursorXAnimation = new Animation();
   private final Animation pickerCursorYAnimation = new Animation();
   private final Animation hueCursorAnimation = new Animation();
   private final Animation alphaCursorAnimation = new Animation();
   private final Animation toggleAnimation = new Animation();
   private boolean indicatorsInitialized;
   float[] hsb = new float[]{1.0F, 1.0F, 1.0F, 1.0F};

   public PickerButton(ColorSetting setting) {
      super(setting.getName());
      this.setting = setting;
      double var2 = this.getState() ? 1.0 : 0.0;
      this.toggleAnimation.from = var2;
      this.toggleAnimation.to = var2;
   }

   public static boolean mouseOver(int minX, int minY, int maxX, int maxY, int mX, int mY) {
      return mX >= minX && mY >= minY && mX <= maxX && mY <= maxY;
   }

   public static Color getColor(Color color, float alpha) {
      float var2 = (float)color.getRed() / 255.0F;
      float var3 = (float)color.getGreen() / 255.0F;
      float var4 = (float)color.getBlue() / 255.0F;
      return new Color(var2, var3, var4, alpha);
   }

   public static void drawPickerBase(int pickerX, int pickerY, int pickerWidth, int pickerHeight, float red, float green, float blue, float alpha) {
      RenderSystem.enableBlend();
      RenderSystem.setShader(GameRenderer::getPositionColorProgram);
      BufferBuilder var8 = Tessellator.getInstance().begin(DrawMode.QUADS, VertexFormats.POSITION_COLOR);
      var8.vertex(matrixStack.peek().getPositionMatrix(), (float)pickerX, (float)pickerY, 0.0F).color(1.0F, 1.0F, 1.0F, 1.0F);
      var8.vertex(matrixStack.peek().getPositionMatrix(), (float)pickerX, (float)(pickerY + pickerHeight), 0.0F).color(1.0F, 1.0F, 1.0F, 1.0F);
      var8.vertex(matrixStack.peek().getPositionMatrix(), (float)(pickerX + pickerWidth), (float)(pickerY + pickerHeight), 0.0F).color(red, green, blue, 1.0F);
      var8.vertex(matrixStack.peek().getPositionMatrix(), (float)(pickerX + pickerWidth), (float)pickerY, 0.0F).color(red, green, blue, 1.0F);
      BufferRenderer.drawWithGlobalProgram(var8.end());
      RenderSystem.disableBlend();
      RenderSystem.enableBlend();
      RenderSystem.setShader(GameRenderer::getPositionColorProgram);
      var8 = Tessellator.getInstance().begin(DrawMode.QUADS, VertexFormats.POSITION_COLOR);
      var8.vertex(matrixStack.peek().getPositionMatrix(), (float)pickerX, (float)pickerY, 0.0F).color(0.0F, 0.0F, 0.0F, 0.0F);
      var8.vertex(matrixStack.peek().getPositionMatrix(), (float)pickerX, (float)(pickerY + pickerHeight), 0.0F).color(0.0F, 0.0F, 0.0F, 1.0F);
      var8.vertex(matrixStack.peek().getPositionMatrix(), (float)(pickerX + pickerWidth), (float)(pickerY + pickerHeight), 0.0F).color(0.0F, 0.0F, 0.0F, 1.0F);
      var8.vertex(matrixStack.peek().getPositionMatrix(), (float)(pickerX + pickerWidth), (float)pickerY, 0.0F).color(0.0F, 0.0F, 0.0F, 0.0F);
      BufferRenderer.drawWithGlobalProgram(var8.end());
      RenderSystem.disableBlend();
   }

   public static void drawLeftGradientRect(int left, int top, int right, int bottom, int startColor, int endColor) {
      float var6 = (float)(startColor >> 24 & 0xFF) / 255.0F;
      float var7 = (float)(startColor >> 16 & 0xFF) / 255.0F;
      float var8 = (float)(startColor >> 8 & 0xFF) / 255.0F;
      float var9 = (float)(startColor & 0xFF) / 255.0F;
      float var10 = (float)(endColor >> 24 & 0xFF) / 255.0F;
      float var11 = (float)(endColor >> 16 & 0xFF) / 255.0F;
      float var12 = (float)(endColor >> 8 & 0xFF) / 255.0F;
      float var13 = (float)(endColor & 0xFF) / 255.0F;
      RenderSystem.enableBlend();
      RenderSystem.setShader(GameRenderer::getPositionColorProgram);
      BufferBuilder var14 = Tessellator.getInstance().begin(DrawMode.QUADS, VertexFormats.POSITION_COLOR);
      var14.vertex(matrixStack.peek().getPositionMatrix(), (float)right, (float)top, 0.0F).color(var11, var12, var13, var10);
      var14.vertex(matrixStack.peek().getPositionMatrix(), (float)left, (float)top, 0.0F).color(var7, var8, var9, var6);
      var14.vertex(matrixStack.peek().getPositionMatrix(), (float)left, (float)bottom, 0.0F).color(var7, var8, var9, var6);
      var14.vertex(matrixStack.peek().getPositionMatrix(), (float)right, (float)bottom, 0.0F).color(var11, var12, var13, var10);
      BufferRenderer.drawWithGlobalProgram(var14.end());
      RenderSystem.disableBlend();
   }

   public static void gradient(int minX, int minY, int maxX, int maxY, int startColor, int endColor, boolean left) {
      float var7 = (float)(startColor >> 24 & 0xFF) / 255.0F;
      float var8 = (float)(startColor >> 16 & 0xFF) / 255.0F;
      float var9 = (float)(startColor >> 8 & 0xFF) / 255.0F;
      float var10 = (float)(startColor & 0xFF) / 255.0F;
      float var11 = (float)(endColor >> 24 & 0xFF) / 255.0F;
      float var12 = (float)(endColor >> 16 & 0xFF) / 255.0F;
      float var13 = (float)(endColor >> 8 & 0xFF) / 255.0F;
      float var14 = (float)(endColor & 0xFF) / 255.0F;
      RenderSystem.enableBlend();
      RenderSystem.setShader(GameRenderer::getPositionColorProgram);
      BufferBuilder var15 = Tessellator.getInstance().begin(DrawMode.QUADS, VertexFormats.POSITION_COLOR);
      var15.vertex(matrixStack.peek().getPositionMatrix(), (float)minX, (float)minY, 0.0F).color(var8, var9, var10, var7);
      var15.vertex(matrixStack.peek().getPositionMatrix(), (float)minX, (float)maxY, 0.0F).color(var8, var9, var10, var7);
      var15.vertex(matrixStack.peek().getPositionMatrix(), (float)maxX, (float)maxY, 0.0F).color(var12, var13, var14, var11);
      var15.vertex(matrixStack.peek().getPositionMatrix(), (float)maxX, (float)minY, 0.0F).color(var12, var13, var14, var11);
      BufferRenderer.drawWithGlobalProgram(var15.end());
      RenderSystem.disableBlend();
   }

   public static int gradientColor(int color, int percentage) {
      int var2 = ((color & 0xFF0000) >> 16) * (100 + percentage) / 100;
      int var3 = ((color & 0xFF00) >> 8) * (100 + percentage) / 100;
      int var4 = (color & 0xFF) * (100 + percentage) / 100;
      return new Color(var2, var3, var4).hashCode();
   }

   public static void drawGradientRect(float left, float top, float right, float bottom, int startColor, int endColor, boolean hovered) {
      if (hovered) {
         startColor = gradientColor(startColor, -20);
         endColor = gradientColor(endColor, -20);
      }

      float var7 = (float)(startColor >> 24 & 0xFF) / 255.0F;
      float var8 = (float)(startColor >> 16 & 0xFF) / 255.0F;
      float var9 = (float)(startColor >> 8 & 0xFF) / 255.0F;
      float var10 = (float)(startColor & 0xFF) / 255.0F;
      float var11 = (float)(endColor >> 24 & 0xFF) / 255.0F;
      float var12 = (float)(endColor >> 16 & 0xFF) / 255.0F;
      float var13 = (float)(endColor >> 8 & 0xFF) / 255.0F;
      float var14 = (float)(endColor & 0xFF) / 255.0F;
      RenderSystem.enableBlend();
      RenderSystem.setShader(GameRenderer::getPositionColorProgram);
      BufferBuilder var15 = Tessellator.getInstance().begin(DrawMode.QUADS, VertexFormats.POSITION_COLOR);
      var15.vertex(matrixStack.peek().getPositionMatrix(), right, top, 0.0F).color(var8, var9, var10, var7);
      var15.vertex(matrixStack.peek().getPositionMatrix(), left, top, 0.0F).color(var8, var9, var10, var7);
      var15.vertex(matrixStack.peek().getPositionMatrix(), left, bottom, 0.0F).color(var12, var13, var14, var11);
      var15.vertex(matrixStack.peek().getPositionMatrix(), right, bottom, 0.0F).color(var12, var13, var14, var11);
      BufferRenderer.drawWithGlobalProgram(var15.end());
      RenderSystem.disableBlend();
   }

   public static String readClipboard() {
      try {
         return (String)Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
      } catch (IOException | UnsupportedFlavorException var1) {
         return null;
      }
   }

   public static void drawOutlineRect(double left, double top, double right, double bottom, Color color, float lineWidth) {
      if (left < right) {
         double var10 = left;
         left = right;
         right = var10;
      }

      if (top < bottom) {
         double var17 = top;
         top = bottom;
         bottom = var17;
      }

      float var12 = (float)(color.getRGB() >> 24 & 0xFF) / 255.0F;
      float var13 = (float)(color.getRGB() >> 16 & 0xFF) / 255.0F;
      float var14 = (float)(color.getRGB() >> 8 & 0xFF) / 255.0F;
      float var15 = (float)(color.getRGB() & 0xFF) / 255.0F;
      RenderSystem.enableBlend();
      RenderSystem.setShader(GameRenderer::getPositionColorProgram);
      BufferBuilder var16 = Tessellator.getInstance().begin(DrawMode.DEBUG_LINES, VertexFormats.POSITION_COLOR);
      var16.vertex(matrixStack.peek().getPositionMatrix(), (float)left, (float)bottom, 0.0F).color(var13, var14, var15, var12);
      var16.vertex(matrixStack.peek().getPositionMatrix(), (float)right, (float)bottom, 0.0F).color(var13, var14, var15, var12);
      var16.vertex(matrixStack.peek().getPositionMatrix(), (float)right, (float)top, 0.0F).color(var13, var14, var15, var12);
      var16.vertex(matrixStack.peek().getPositionMatrix(), (float)left, (float)top, 0.0F).color(var13, var14, var15, var12);
      BufferRenderer.drawWithGlobalProgram(var16.end());
      RenderSystem.disableBlend();
   }

   @Override
   public void drawScreen(DrawContext context, int mouseX, int mouseY, float partialTicks) {
      matrixStack = context.getMatrices();
      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();
      Color var10 = ClickGui.getInstance().getColor(var8);
      int var11 = var5 ? ClickGui.getInstance().hoverAlpha.getValueInt() : ClickGui.getInstance().alpha.getValueInt();
      Color var12 = new Color(var5 ? hoverColor : defaultColor, true);
      float var13 = (float)this.width + 7.0F;
      float var14 = (float)this.height - 0.5F;
      if (ClickGui.getInstance().colorMode.getValue() == ClickGui.ColorMode.Spectrum) {
         if (var6 >= 0.999) {
            Render2DUtil.drawLutRect(
               context.getMatrices(),
               this.x,
               this.y,
               var13,
               var14,
               ClickGui.getInstance().getSpectrumLutId(),
               ClickGui.getInstance().getSpectrumLutHeight(),
               var11
            );
         } else {
            Render2DUtil.rect(context.getMatrices(), this.x, this.y, this.x + var13, this.y + var14, var12.getRGB());
            int var15 = (int)Math.round((double)var11 * var6);
            if (var15 > 0) {
               Render2DUtil.drawLutRect(
                  context.getMatrices(),
                  this.x,
                  this.y,
                  var13,
                  var14,
                  ClickGui.getInstance().getSpectrumLutId(),
                  ClickGui.getInstance().getSpectrumLutHeight(),
                  var15
               );
            }
         }
      } else {
         Color var33 = new Color(var10.getRed(), var10.getGreen(), var10.getBlue(), var10.getAlpha());
         Color var16 = ColorUtil.fadeColor(var12, var33, var6);
         Render2DUtil.rect(context.getMatrices(), this.x, this.y, this.x + var13, this.y + (float)this.height - 0.5F, var16.getRGB());
      }

      Render2DUtil.rect(
         matrixStack,
         this.x - 1.5F + (float)this.width + 0.6F - 0.5F,
         this.y + 4.0F,
         this.x + (float)this.width + 7.0F - 2.5F,
         this.y + (float)this.height - 5.0F,
         ColorUtil.injectAlpha(this.setting.getValue(), 255).getRGB()
      );
      float var34 = this.getCenteredTextY(this.y, (float)this.height - 0.5F);
      if (this.isHovering(mouseX, mouseY) && InputUtil.isKeyPressed(mc.getWindow().getHandle(), 340)) {
         this.drawString("Reset Default", (double)(this.x + 2.3F), (double)var34, enableTextColor);
      } else {
         this.drawSettingString(this.setting.getDisplayName(), (double)(this.x + 2.3F), (double)var34, enableTextColor);
      }

      double var17 = this.openAnimation.get(this.open ? 1.0 : 0.0, 200L, Easing.CubicInOut);
      if (var17 > 0.01) {
         int var19 = (int)this.x - 1;
         int var20 = (int)this.y - 1;
         int var21 = (int)(this.x + (float)this.width + 7.0F) + 1;
         int var22 = (int)Math.round((double)(this.y + (float)this.height) + 119.0 * var17) + 1;
         context.enableScissor(var19, var20, var21, var22);
         int var23 = (int)this.x;
         int var24 = (int)(this.y + (float)this.height);
         int var25 = (int)((float)this.width + 7.4F);
         byte var26 = 78;
         int var27 = var24 + var26 + 4;
         int var28 = var27 + 8;
         this.drawPicker(this.setting, var23, var24, var23, var28, var23, var27, mouseX, mouseY);
         float var29 = (float)var24 + 96.0F;
         float var30 = this.getCenteredTextY(var29, (float)this.getFontHeight());
         this.drawString(
            "copy", (double)(this.x + 2.3F), (double)var30, this.isInsideCopy(mouseX, mouseY) ? enableTextColor : defaultTextColor
         );
         this.drawString(
            "paste",
            (double)(this.x + (float)this.width - 2.3F - (float)this.getWidth("paste") + 11.7F - 4.6F),
            (double)var30,
            this.isInsidePaste(mouseX, mouseY) ? enableTextColor : defaultTextColor
         );
         float var31 = var29 + (float)this.getFontHeight();
         float var32 = this.getCenteredTextY(var31, (float)this.getFontHeight());
         this.drawString(
            "rainbow",
            (double)(this.x + 2.3F),
            (double)var32,
            this.setting.rainbow ? ColorUtil.injectAlpha(var10, 255).getRGB() : (this.isInsideRainbow(mouseX, mouseY) ? enableTextColor : defaultTextColor)
         );
         context.disableScissor();
      }
   }

   @Override
   public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
      if (mouseButton == 0 && this.isHovering(mouseX, mouseY) && InputUtil.isKeyPressed(mc.getWindow().getHandle(), 340)) {
         this.pickingColor = false;
         this.pickingHue = false;
         this.pickingAlpha = false;
         this.setting.setValue(this.setting.getDefaultValue());
         this.setting.rainbow = this.setting.getDefaultRainbow();
         if (this.setting.injectBoolean) {
            this.setting.booleanValue = this.setting.getDefaultBooleanValue();
         }

         sound();
      } else {
         if (mouseButton == 0) {
            int var4 = (int)this.x;
            int var5 = (int)(this.y + (float)this.height);
            int var6 = (int)((float)this.width + 7.4F);
            byte var7 = 78;
            int var8 = var6 + 3;
            byte var9 = 7;
            byte var10 = 7;
            int var11 = var5 + var7 + 4;
            int var12 = var11 + var10 + 1;
            if (mouseOver(var4, var5, var4 + var6, var5 + var7, mouseX, mouseY)) {
               this.pickingColor = true;
            }

            if (mouseOver(var4, var12, var4 + var8, var12 + var9, mouseX, mouseY)) {
               this.pickingHue = true;
            }

            if (mouseOver(var4, var11, var4 + var6, var11 + var10, mouseX, mouseY)) {
               this.pickingAlpha = true;
            }
         }

         if (this.isHovering(mouseX, mouseY)) {
            if (mouseButton == 1) {
               sound();
               this.open = !this.open;
            } else if (mouseButton == 0 && this.setting.injectBoolean) {
               sound();
               boolean var14 = this.setting.booleanValue = !this.setting.booleanValue;
            }
         }

         if (mouseButton == 0 && this.isInsideRainbow(mouseX, mouseY) && this.open) {
            boolean var15 = this.setting.rainbow = !this.setting.rainbow;
         }

         if (mouseButton == 0 && this.isInsideCopy(mouseX, mouseY) && this.open) {
            sound();
            String var16 = String.format(
               "#%02x%02x%02x%02x",
               this.setting.getValue().getAlpha(),
               this.setting.getValue().getRed(),
               this.setting.getValue().getGreen(),
               this.setting.getValue().getBlue()
            );
            StringSelection var19 = new StringSelection(var16);
            Clipboard var21 = Toolkit.getDefaultToolkit().getSystemClipboard();
            var21.setContents(var19, var19);
            CommandManager.sendMessage("Copied the color to your clipboard.");
         }

         if (mouseButton == 0 && this.isInsidePaste(mouseX, mouseY) && this.open) {
            try {
               if (readClipboard() != null) {
                  if (Objects.requireNonNull(readClipboard()).startsWith("#")) {
                     String var17 = Objects.requireNonNull(readClipboard());
                     int var20 = Integer.valueOf(var17.substring(1, 3), 16);
                     int var22 = Integer.valueOf(var17.substring(3, 5), 16);
                     int var23 = Integer.valueOf(var17.substring(5, 7), 16);
                     int var24 = Integer.valueOf(var17.substring(7, 9), 16);
                     this.setting.setValue(new Color(var22, var23, var24, var20));
                  } else {
                     String[] var18 = readClipboard().split(",");
                     this.setting.setValue(new Color(Integer.parseInt(var18[0]), Integer.parseInt(var18[1]), Integer.parseInt(var18[2])));
                  }
               }
            } catch (Exception var13) {
               var13.printStackTrace();
               CommandManager.sendMessage("\u00a74Bad color format! Use Hex (#FFFFFFFF)");
               this.setting.setValue(-1);
            }
         }
      }
   }

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

   @Override
   public void update() {
      this.setHidden(!this.setting.isVisible());
   }

   @Override
   public void mouseReleased(int mouseX, int mouseY, int releaseButton) {
      this.pickingAlpha = false;
      this.pickingHue = false;
      this.pickingColor = false;
   }

   public boolean isInsideCopy(int mouseX, int mouseY) {
      float var3 = this.y + (float)this.height + 96.0F;
      int var4 = (int)this.getCenteredTextY(var3, (float)this.getFontHeight());
      return mouseOver(
         (int)((float)((int)this.x) + 2.3F),
         var4,
         (int)((float)((int)this.x) + 2.3F) + this.getWidth("copy"),
         var4 + this.getFontHeight(),
         mouseX,
         mouseY
      );
   }

   public boolean isInsideRainbow(int mouseX, int mouseY) {
      float var3 = this.y + (float)this.height + 96.0F;
      float var4 = var3 + (float)this.getFontHeight();
      int var5 = (int)this.getCenteredTextY(var4, (float)this.getFontHeight());
      return mouseOver(
         (int)((float)((int)this.x) + 2.3F),
         var5,
         (int)((float)((int)this.x) + 2.3F) + this.getWidth("rainbow"),
         var5 + this.getFontHeight(),
         mouseX,
         mouseY
      );
   }

   public boolean isInsidePaste(int mouseX, int mouseY) {
      float var3 = this.y + (float)this.height + 96.0F;
      int var4 = (int)this.getCenteredTextY(var3, (float)this.getFontHeight());
      int var5 = (int)(this.x + (float)this.width - 2.3F - (float)this.getWidth("paste") + 11.7F - 4.6F);
      return mouseOver(var5, var4, var5 + this.getWidth("paste"), var4 + this.getFontHeight(), mouseX, mouseY);
   }

   @Override
   public int getHeight() {
      return this.open ? super.getHeight() + 119 : super.getHeight();
   }

   public void drawPicker(
      ColorSetting setting, int pickerX, int pickerY, int hueSliderX, int hueSliderY, int alphaSliderX, int alphaSliderY, int mouseX, int mouseY
   ) {
      int var10 = (int)((float)this.width + 7.4F);
      byte var11 = 78;
      byte var12 = 7;
      byte var13 = 7;
      if (this.pickingColor
         && (GLFW.glfwGetMouseButton(mc.getWindow().getHandle(), 0) != 1 || !mouseOver(pickerX, pickerY, pickerX + var10, pickerY + var11, mouseX, mouseY))) {
         this.pickingColor = false;
      }

      if (!this.pickingColor) {
         this.hsb = Color.RGBtoHSB(setting.getValue().getRed(), setting.getValue().getGreen(), setting.getValue().getBlue(), null);
      }

      float[] var14 = new float[]{this.hsb[0], this.hsb[1], this.hsb[2], (float)setting.getValue().getAlpha() / 255.0F};
      if (this.pickingHue
         && (
            GLFW.glfwGetMouseButton(mc.getWindow().getHandle(), 0) != 1
               || !mouseOver(hueSliderX, hueSliderY, hueSliderX + var10, hueSliderY + var12, mouseX, mouseY)
         )) {
         this.pickingHue = false;
      }

      if (this.pickingAlpha
         && (
            GLFW.glfwGetMouseButton(mc.getWindow().getHandle(), 0) != 1
               || !mouseOver(alphaSliderX, alphaSliderY, alphaSliderX + var10, alphaSliderY + var13, mouseX, mouseY)
         )) {
         this.pickingAlpha = false;
      }

      if (this.pickingHue) {
         float var15 = (float)Math.min(Math.max(hueSliderX, mouseX), hueSliderX + var10);
         var14[0] = (var15 - (float)hueSliderX) / (float)var10;
      }

      if (this.pickingAlpha) {
         float var28 = (float)Math.min(Math.max(alphaSliderX, mouseX), alphaSliderX + var10);
         var14[3] = (var28 - (float)alphaSliderX) / (float)var10;
      }

      if (this.pickingColor) {
         float var29 = (float)Math.min(Math.max(pickerX, mouseX), pickerX + var10);
         float var16 = (float)Math.min(Math.max(pickerY, mouseY), pickerY + var11);
         var14[1] = (var29 - (float)pickerX) / (float)var10;
         var14[2] = 1.0F - (var16 - (float)pickerY) / (float)var11;
      }

      int var30 = Color.HSBtoRGB(var14[0], 1.0F, 1.0F);
      float var17 = (float)(var30 >> 16 & 0xFF) / 255.0F;
      float var18 = (float)(var30 >> 8 & 0xFF) / 255.0F;
      float var19 = (float)(var30 & 0xFF) / 255.0F;
      drawPickerBase(pickerX, pickerY, var10, var11, var17, var18, var19, var14[3]);
      int var20 = (int)((float)pickerX + var14[1] * (float)var10);
      int var21 = (int)((float)(pickerY + var11) - var14[2] * (float)var11);
      int var22 = (int)((float)hueSliderX + (float)var10 * var14[0]);
      int var23 = (int)((float)alphaSliderX + (float)var10 * var14[3]);
      if (!this.indicatorsInitialized) {
         this.pickerCursorXAnimation.from = (double)var20;
         this.pickerCursorXAnimation.to = (double)var20;
         this.pickerCursorYAnimation.from = (double)var21;
         this.pickerCursorYAnimation.to = (double)var21;
         this.hueCursorAnimation.from = (double)var22;
         this.hueCursorAnimation.to = (double)var22;
         this.alphaCursorAnimation.from = (double)var23;
         this.alphaCursorAnimation.to = (double)var23;
         this.indicatorsInitialized = true;
      }

      long var24 = this.pickingColor ? 40L : 120L;
      int var26 = (int)Math.round(this.pickerCursorXAnimation.get((double)var20, var24, Easing.SineOut));
      int var27 = (int)Math.round(this.pickerCursorYAnimation.get((double)var21, var24, Easing.SineOut));
      this.drawHueSlider(hueSliderX, hueSliderY, var10, var12, var14[0], this.pickingHue);
      setting.setValue(getColor(new Color(Color.HSBtoRGB(var14[0], var14[1], var14[2])), var14[3]));
      Render2DUtil.arrow(matrixStack, (float)var26, (float)var27, setting.getValue());
      this.drawAlphaSlider(alphaSliderX, alphaSliderY, var10 - 1, var13, var17, var18, var19, var14[3], this.pickingAlpha);
   }

   public void drawHueSlider(int x, int y, int width, int height, float hue, boolean snap) {
      int var7 = 0;
      if (height > width) {
         Render2DUtil.rect(matrixStack, (float)x, (float)y, (float)(x + width), (float)(y + 4), -65536);
         y += 4;

         for (int var8 = 0; var8 < 6; var8++) {
            int var9 = Color.HSBtoRGB((float)var7 / 6.0F, 1.0F, 1.0F);
            int var10 = Color.HSBtoRGB((float)(var7 + 1) / 6.0F, 1.0F, 1.0F);
            drawGradientRect(
               (float)x,
               (float)y + (float)var7 * ((float)height / 6.0F),
               (float)(x + width),
               (float)y + (float)(var7 + 1) * ((float)height / 6.0F),
               var9,
               var10,
               false
            );
            var7++;
         }

         int var15 = (int)((float)y + (float)height * hue) - 4;
         long var11 = snap ? 40L : 120L;
         int var13 = (int)Math.round(this.hueCursorAnimation.get((double)var15, var11, Easing.SineOut));
         Render2DUtil.rect(matrixStack, (float)x, (float)(var13 - 1), (float)(x + width), (float)(var13 + 1), -1);
         drawOutlineRect((double)x, (double)(var13 - 1), (double)(x + width), (double)(var13 + 1), Color.BLACK, 1.0F);
      } else {
         for (int var16 = 0; var16 < 6; var16++) {
            int var18 = Color.HSBtoRGB((float)var7 / 6.0F, 1.0F, 1.0F);
            int var19 = Color.HSBtoRGB((float)(var7 + 1) / 6.0F, 1.0F, 1.0F);
            gradient(x + var7 * (width / 6), y, x + (var7 + 1) * (width / 6) + 3, y + height, var18, var19, true);
            var7++;
         }

         int var17 = (int)((float)x + (float)width * hue);
         long var20 = snap ? 40L : 120L;
         int var21 = (int)Math.round(this.hueCursorAnimation.get((double)var17, var20, Easing.SineOut));
         Render2DUtil.rect(matrixStack, (float)(var21 - 1), (float)y - 1.2F, (float)(var21 + 1), (float)(y + height) + 1.2F, -1);
         drawOutlineRect((double)var21 - 1.2, (double)y - 1.2, (double)var21 + 1.2, (double)(y + height) + 1.2, Color.BLACK, 0.1F);
      }
   }

   public void drawAlphaSlider(int x, int y, int width, int height, float red, float green, float blue, float alpha, boolean snap) {
      boolean var10 = true;
      int var11 = height / 2;

      for (int var12 = -var11; var12 < width; var12 += var11) {
         if (!var10) {
            Render2DUtil.rect(matrixStack, (float)(x + var12), (float)y, (float)(x + var12 + var11), (float)(y + height), -1);
            Render2DUtil.rect(matrixStack, (float)(x + var12), (float)(y + var11), (float)(x + var12 + var11), (float)(y + height), -7303024);
            if (var12 < width - var11) {
               int var13 = x + var12 + var11;
               int var14 = Math.min(x + width, x + var12 + var11 * 2);
               Render2DUtil.rect(matrixStack, (float)var13, (float)y, (float)var14, (float)(y + height), -7303024);
               Render2DUtil.rect(matrixStack, (float)var13, (float)(y + var11), (float)var14, (float)(y + height), -1);
            }
         }

         var10 = !var10;
      }

      drawLeftGradientRect(x, y, x + width, y + height, 0, new Color(red, green, blue, 1.0F).getRGB());
      int var18 = (int)((float)x + (float)width * alpha);
      long var15 = snap ? 40L : 120L;
      int var17 = (int)Math.round(this.alphaCursorAnimation.get((double)var18, var15, Easing.SineOut));
      Render2DUtil.rect(matrixStack, (float)(var17 - 1), (float)y - 1.2F, (float)(var17 + 1), (float)(y + height) + 1.2F, -1);
      drawOutlineRect((double)var17 - 1.2, (double)y - 1.2, (double)var17 + 1.2, (double)(y + height) + 1.2, Color.BLACK, 0.1F);
   }
}
