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

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.input.KeyInput;
import wtf.opal.client.feature.module.property.impl.GroupProperty;
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.ClickGuiAnimationConfig;
import wtf.opal.client.screen.click.dropdown.panel.property.PropertyPanel;
import wtf.opal.client.screen.click.dropdown.panel.property.PropertyProvider;
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 GroupPropertyComponent extends PropertyPanel<GroupProperty> {
   private final PropertyProvider propertyProvider;
   private final Animation expandAnimation = new Animation(Easing.DECELERATE, 125L);
   private boolean expanded;

   public GroupPropertyComponent(GroupProperty property) {
      super(property);
      this.propertyProvider = new PropertyProvider(property, this::isExpandedAnimation, null);
   }

   private boolean isExpandedAnimation() {
      return this.expanded || this.expandAnimation.getValue() > 0.0F;
   }

   @Override
   public void close() {
      this.propertyProvider.close();
   }

   @Override
   public void init() {
      this.propertyProvider.init();
   }

   @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);
      float var5 = 3.0F;
      float var6 = 3.0F * (1.0F - this.expandAnimation.getValue());
      float var7 = this.width;
      float var8 = this.x + var6;
      float var9 = this.y + var6;
      float var10 = var7 - var6 * 2.0F;
      float var11 = 22.0F - var6 * 2.0F;
      float var12 = 4.0F * (1.0F - this.expandAnimation.getValue());
      NVGTextRenderer var13 = FontRepository.getFont("productsans-bold");
      NVGRenderer.roundedRect(var8, var9, var10, var11, var12, ColorUtility.scaleOpacity(ColorUtility.getPanelBackgroundColor(), 0.2F));
      var13.drawString(this.getProperty().getName(), var8 + (var10 - var13.getStringWidth(this.getProperty().getName(), 7.0F)) / 2.0F, this.y + 13.5F, 7.0F, -1);
      String var14 = "\ue5cf";
      NVGTextRenderer var15 = FontRepository.getFont("materialicons-regular");
      float var16 = 12.0F;
      float var17 = var15.getStringWidth("\ue5cf", 12.0F);
      NVGRenderer.rotate(
         (double)(this.expandAnimation.getValue() * 180.0F),
         this.x + 3.0F + var7 - 20.0F,
         this.y + 5.0F,
         var17,
         12.0F,
         () -> var15.drawString("\ue5cf", 0.0F, 0.0F, 12.0F, -1, false, 18)
      );
      NVGRenderer.scissor(this.x, this.y, this.width, this.height, () -> {
         this.propertyProvider.setX(this.x);
         this.propertyProvider.setY(this.y + 22.0F);
         this.propertyProvider.setWidth(this.width);
         this.propertyProvider.render(context, mouseX, mouseY, delta);
      });
      float var18 = this.propertyProvider.getExtraHeight();
      this.setHeight(var18 * this.expandAnimation.getValue() + 22.0F);
   }

   @Override
   public void mouseClicked(double mouseX, double mouseY, int button) {
      if (HoverUtility.isHovering(this.x, this.y, this.width, 17.0F, mouseX, mouseY)) {
         this.expanded = !this.expanded;
      }

      this.propertyProvider.mouseClicked(mouseX, mouseY, button);
   }

   @Override
   public void keyPressed(KeyInput keyInput) {
      this.propertyProvider.keyPressed(keyInput);
   }

   @Override
   public void charTyped(char chr, int modifiers) {
      this.propertyProvider.charTyped(chr, modifiers);
   }

   @Override
   public void mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
      this.propertyProvider.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
   }

   @Override
   public void mouseReleased(double mouseX, double mouseY, int button) {
      this.propertyProvider.mouseReleased(mouseX, mouseY, button);
   }
}
