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 wtf.opal.client.feature.module.property.impl.bool.BooleanProperty;
import wtf.opal.client.feature.module.property.impl.bool.MultipleBooleanProperty;
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.panel.property.PropertyPanel;
import wtf.opal.utility.misc.HoverUtility;
import wtf.opal.utility.render.ColorUtility;

@Environment(EnvType.CLIENT)
public final class MultipleBooleanPropertyComponent extends PropertyPanel<MultipleBooleanProperty> {

   public MultipleBooleanPropertyComponent(MultipleBooleanProperty property) {
      super(property);
   }

   @Override
   public void render(DrawContext context, int mouseX, int mouseY, float delta) {
      super.render(context, mouseX, mouseY, delta);
      MultipleBooleanProperty var5 = this.getProperty();
      NVGTextRenderer var6 = FontRepository.getFont("productsans-medium");
      var6.drawString(var5.getName(), this.x + 5.0F, this.y + 8.5F, 7.0F, -1);
      float var7 = this.processText(this.x, this.y, this.width, false);
      float var8 = this.x + 5.0F;
      float var9 = this.y + 13.0F;
      float var10 = this.width - 10.0F;
      float var11 = 10.0F + var7;
      float var12 = 2.5F;
      NVGRenderer.roundedRectOutline(var8, var9, var10, var11, 2.5F, 1.5F, -11513776);
      NVGRenderer.roundedRect(var8, var9, var10, var11, 2.5F, -15132391);
      this.setHeight(17.0F + var11);
      this.processText(this.x, this.y, this.width, true);
   }

   @Override
   public void mouseClicked(double mouseX, double mouseY, int button) {
      NVGTextRenderer var6 = FontRepository.getFont("productsans-medium");
      float var7 = 0.0F;
      float var8 = 2.0F;

      for (BooleanProperty var10 : this.getProperty().getValue()) {
         if (!var10.isHidden()) {
            float var11 = var6.getStringWidth(var10.getName(), 6.0F) + 8.75F;
            if (var8 + var11 > this.width - 10.0F) {
               var7 += 10.0F;
               var8 = 2.0F;
            }

            if (HoverUtility.isHovering(this.x + 4.5F + var8, this.y + 13.0F + var7 + 7.0F - 5.5F, var11 - 4.0F, 8.5F, mouseX, mouseY)) {
               var10.toggle();
            }

            var8 += var11 - 2.5F;
         }
      }
   }

   private float processText(float x, float y, float width, boolean render) {
      NVGTextRenderer var5 = FontRepository.getFont("productsans-medium");
      float var6 = 0.0F;
      float var7 = 2.0F;

      for (BooleanProperty var9 : this.getProperty().getValue()) {
         if (!var9.isHidden()) {
            float var10 = var5.getStringWidth(var9.getName(), 6.0F) + 8.75F;
            if (var7 + var10 > width - 10.0F) {
               var6 += 10.0F;
               var7 = 2.0F;
            }

            if (render) {
               NVGRenderer.roundedRect(
                  x + 4.5F + var7,
                  y + 13.0F + var6 + 7.0F - 5.5F,
                  var10 - 4.0F,
                  8.5F,
                  2.5F,
                  var9.getValue() ? ColorUtility.applyOpacity((Integer)ColorUtility.getClientTheme().first, 0.4F) : ColorUtility.darker(-8355712, 0.6F)
               );
               var5.drawString(var9.getName(), x + 7.0F + var7, y + 13.0F + var6 + 8.0F, 6.0F, var9.getValue() ? ColorUtility.applyOpacity(-1, 0.9F) : -6250336);
            }

            var7 += var10 - 2.5F;
         }
      }

      return var6 + 1.5F;
   }
}
