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

import java.util.ArrayList;
import java.util.List;
import java.util.function.BooleanSupplier;
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.IPropertyListProvider;
import wtf.opal.client.feature.module.property.Property;
import wtf.opal.client.screen.click.IOpalComponent;
import wtf.opal.client.screen.click.OpalPanelComponent;

@Environment(EnvType.CLIENT)
public final class PropertyProvider extends OpalPanelComponent {
   private final List<PropertyPanel<?>> propertyPanelList = new ArrayList<>();
   private final IPropertyListProvider propertyListProvider;
   private final BooleanSupplier expanded;
   private final BooleanSupplier lastPropertyListProvider;
   private boolean hasProperties;
   private boolean updated;
   private float extraHeight;

   public PropertyProvider(IPropertyListProvider propertyListProvider, BooleanSupplier expanded, BooleanSupplier lastPropertyListProvider) {
      this.propertyListProvider = propertyListProvider;
      this.expanded = expanded;
      this.initProperties();
      this.updateHasProperties();
      this.lastPropertyListProvider = lastPropertyListProvider;
   }

   private void initProperties() {
      for (Property var2 : this.propertyListProvider.getPropertyList()) {
         PropertyPanel var3 = var2.createClickGUIComponent();
         if (var3 != null) {
            this.propertyPanelList.add(var3);
         } else {
            System.err.println("Unimplemented property: " + var2.getClass().getSimpleName());
         }
      }
   }

   public boolean isHasProperties() {
      if (!this.updated) {
         this.updateHasProperties();
         this.updated = true;
      }

      return this.hasProperties;
   }

   private void updateHasProperties() {
      this.hasProperties = this.propertyListProvider.getPropertyList().stream().anyMatch(p -> !p.isHidden());
   }

   private boolean isClosed() {
      return !this.expanded.getAsBoolean();
   }

   @Override
   public void init() {
      if (!this.isClosed()) {
         this.propertyPanelList.forEach(IOpalComponent::init);
      }
   }

   @Override
   public void close() {
      if (!this.isClosed()) {
         this.propertyPanelList.forEach(IOpalComponent::close);
      }
   }

   @Override
   public void render(DrawContext context, int mouseX, int mouseY, float delta) {
      if (!this.isClosed()) {
         float var5 = 0.0F;
         int var6 = -1;

         for (int var7 = this.propertyPanelList.size() - 1; var7 >= 0; var7--) {
            if (!this.propertyPanelList.get(var7).isHidden()) {
               var6 = var7;
               break;
            }
         }

         for (int var9 = 0; var9 < this.propertyPanelList.size(); var9++) {
            PropertyPanel var8 = this.propertyPanelList.get(var9);
            if (!var8.isHidden()) {
               var8.setX(this.x);
               var8.setY(this.y + var5);
               var8.setWidth(this.width);
               var8.lastProperty = this.lastPropertyListProvider != null && this.lastPropertyListProvider.getAsBoolean() && var9 == var6;
               var8.render(context, mouseX, mouseY, delta);
               var5 += var8.getHeight();
            }
         }

         this.extraHeight = var5;
      }
   }

   @Override
   public void keyPressed(KeyInput keyInput) {
      if (!this.isClosed()) {
         this.propertyPanelList.forEach(propertyPanel -> propertyPanel.keyPressed(keyInput));
      }
   }

   @Override
   public void charTyped(char chr, int modifiers) {
      if (!this.isClosed()) {
         this.propertyPanelList.forEach(propertyPanel -> propertyPanel.charTyped(chr, modifiers));
      }
   }

   public float getExtraHeight() {
      return this.extraHeight;
   }

   @Override
   public void mouseClicked(double mouseX, double mouseY, int button) {
      if (!this.isClosed()) {
         for (PropertyPanel var7 : this.propertyPanelList) {
            if (!var7.isHidden()) {
               var7.mouseClicked(mouseX, mouseY, button);
            }
         }
      }
   }

   @Override
   public void mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
      if (!this.isClosed()) {
         for (PropertyPanel var10 : this.propertyPanelList) {
            if (!var10.isHidden()) {
               var10.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
            }
         }
      }
   }

   @Override
   public void mouseReleased(double mouseX, double mouseY, int button) {
      if (!this.isClosed()) {
         for (PropertyPanel var7 : this.propertyPanelList) {
            if (!var7.isHidden()) {
               var7.mouseReleased(mouseX, mouseY, button);
            }
         }
      }
   }
}
