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

import java.awt.Color;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import wtf.opal.client.feature.module.property.impl.ColorProperty;
import wtf.opal.client.renderer.NVGRenderer;
import wtf.opal.client.renderer.repository.FontRepository;
import wtf.opal.client.screen.click.dropdown.ClickGuiAnimationConfig;
import wtf.opal.client.screen.click.dropdown.panel.property.PropertyPanel;
import wtf.opal.utility.misc.HoverUtility;
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 ColorPropertyComponent extends PropertyPanel<ColorProperty> {
   private boolean expanded;
   private ColorPropertyComponent.ColorDragType colorDragType;
   private final Animation expandAnimation = new Animation(Easing.DECELERATE, 125L);

   public ColorPropertyComponent(ColorProperty property) {
      super(property);
   }

   @Override
   public void render(DrawContext context, int mouseX, int mouseY, float delta) {
      super.render(context, mouseX, mouseY, delta);
      ClickGuiAnimationConfig.applyParameterPanel(this.expandAnimation, 125L, Easing.DECELERATE);
      this.expandAnimation.run(this.expanded ? 1.0F : 0.0F);
      FontRepository.getFont("productsans-medium").drawString(this.getProperty().getName(), this.x + 5.0F, this.y + 10.5F, 7.0F, -1);
      NVGRenderer.roundedRect(this.x + this.width - 22.0F, this.y + 3.5F, 18.0F, 10.0F, 3.0F, this.getProperty().getValue());
      float var5 = this.x + 5.0F;
      float var6 = this.y + 17.0F;
      float var7 = 65.0F;
      float var8 = 50.0F;
      NVGRenderer.scissor(
         this.x,
         this.y,
         this.width,
         this.height,
         () -> {
            if (this.expandAnimation.getValue() > 0.0F) {
               switch (this.colorDragType) {
                  case PICKER:
                     this.getProperty().setSaturation(Math.min(1.0F, Math.max(0.0F, ((float)mouseX - var5) / 65.0F)));
                     this.getProperty().setBrightness(Math.min(1.0F, Math.max(0.0F, 1.0F - ((float)mouseY - var6) / 50.0F)));
                     break;
                  case HUE:
                     this.getProperty().setHue(Math.min(1.0F, Math.max(0.0F, ((float)mouseY - var6) / 50.0F)));
               }

               float[] var5x = this.getProperty().getHSB();
               this.getProperty().updateValue();
               NVGRenderer.rect(var5, var6, 65.0F, 50.0F, Color.getHSBColor(var5x[0], 1.0F, 1.0F).getRGB());
               NVGRenderer.rectGradient(
                  var5,
                  var6,
                  65.0F,
                  50.0F,
                  Color.getHSBColor(var5x[0], 0.0F, 1.0F).getRGB(),
                  ColorUtility.applyOpacity(Color.getHSBColor(var5x[0], 0.0F, 1.0F).getRGB(), 0),
                  0.0F
               );
               NVGRenderer.rectGradient(
                  var5,
                  var6,
                  65.0F,
                  50.0F,
                  ColorUtility.applyOpacity(Color.getHSBColor(var5x[0], 1.0F, 0.0F).getRGB(), 0),
                  Color.getHSBColor(var5x[0], 1.0F, 0.0F).getRGB(),
                  90.0F
               );
               NVGRenderer.rainbowRect(var5 + 65.0F + 5.0F, var6, 8.0F, 50.0F);
            }

            this.setHeight(17.0F + 50.0F * this.expandAnimation.getValue());
         }
      );
   }

   @Override
   public void mouseClicked(double mouseX, double mouseY, int button) {
      if (HoverUtility.isHovering(this.x, this.y, this.width, 17.0F, mouseX, mouseY) && button == 1) {
         this.expanded = !this.expanded;
      } else {
         if (button == 0) {
            float var6 = this.x + 5.0F;
            float var7 = this.y + 17.0F;
            float var8 = 65.0F;
            float var9 = 50.0F;
            if (HoverUtility.isHovering(var6, var7, 65.0F, 50.0F, mouseX, mouseY)) {
               this.colorDragType = ColorPropertyComponent.ColorDragType.PICKER;
            } else if (HoverUtility.isHovering(var6 + 65.0F + 5.0F, var7, 8.0F, 50.0F, mouseX, mouseY)) {
               this.colorDragType = ColorPropertyComponent.ColorDragType.HUE;
            }
         }
      }
   }

   @Override
   public void mouseReleased(double mouseX, double mouseY, int button) {
      this.colorDragType = ColorPropertyComponent.ColorDragType.NONE;
   }
@Environment(EnvType.CLIENT)
   private static enum ColorDragType {
      PICKER,
      HUE,
      OPACITY,
      NONE;


   }
}
