package wtf.opal.client.feature.module.property.impl;

import com.google.gson.internal.LinkedTreeMap;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.BooleanSupplier;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import wtf.opal.client.feature.module.property.IPropertyListProvider;
import wtf.opal.client.feature.module.property.Property;
import wtf.opal.client.screen.click.dropdown.panel.property.PropertyPanel;
import wtf.opal.client.screen.click.dropdown.panel.property.impl.GroupPropertyComponent;

@Environment(EnvType.CLIENT)
public final class GroupProperty extends Property<List<Property<?>>> implements IPropertyListProvider {
   private final BooleanSupplier enabledSupplier;

   public GroupProperty(String name, BooleanSupplier enabledSupplier, Property<?>... children) {
      super(name);
      this.enabledSupplier = enabledSupplier;
      this.setValue(Arrays.stream(children).filter(Objects::nonNull).toList());
   }

   public GroupProperty(String name, Property<?>... children) {
      this(name, null, children);
   }

   @Override
   public List<Property<?>> getPropertyList() {
      return this.getValue();
   }

   @Override
   public PropertyPanel<?> createClickGUIComponent() {
      return new GroupPropertyComponent(this);
   }

   public boolean isEnabled() {
      return this.enabledSupplier != null && this.enabledSupplier.getAsBoolean();
   }

   @Override
   public void applyValue(Object propertyValue) {
      if (propertyValue instanceof List) {
         for (Object var4 : (List)propertyValue) {
            LinkedTreeMap var5 = (LinkedTreeMap)var4;
            String var6 = (String)var5.get("name");
            Object var7 = var5.get("value");

            for (Property var9 : this.getPropertyList()) {
               if (var6.equals(var9.getName())) {
                  var9.applyValue(var7);
               }
            }
         }
      }
   }
}
