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

import com.google.gson.internal.LinkedTreeMap;
import java.util.Arrays;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import wtf.opal.client.feature.module.property.Property;
import wtf.opal.client.feature.module.property.impl.mode.ModuleMode;
import wtf.opal.client.screen.click.dropdown.panel.property.PropertyPanel;
import wtf.opal.client.screen.click.dropdown.panel.property.impl.MultipleBooleanPropertyComponent;

@Environment(EnvType.CLIENT)
public final class MultipleBooleanProperty extends Property<List<BooleanProperty>> {
   private int subPropertyIndex;

   public MultipleBooleanProperty(String name, BooleanProperty... booleanProperties) {
      super(name);
      this.setValue(Arrays.asList(booleanProperties));
   }

   public MultipleBooleanProperty(String name, ModuleMode<?> parent, BooleanProperty... booleanProperties) {
      super(name, parent);
      this.setValue(Arrays.asList(booleanProperties));
   }

   public BooleanProperty getProperty(String name) {
      return this.getValue().stream().filter(booleanProperty -> booleanProperty.getName().equals(name)).findFirst().orElse(null);
   }

   @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 (BooleanProperty var9 : this.getValue()) {
               if (var6.equals(var9.getId())) {
                  var9.setValue(Boolean.valueOf(Boolean.parseBoolean(String.valueOf(var7))));
               }
            }
         }
      }
   }

   public int getSubPropertyIndex() {
      return this.subPropertyIndex;
   }

   public void setSubPropertyIndex(int subPropertyIndex) {
      this.subPropertyIndex = subPropertyIndex;
   }

   public void cycleSubPropertyIndex() {
      if (!this.getValue().isEmpty()) {
         this.subPropertyIndex = (this.subPropertyIndex + 1) % this.getValue().size();
      }
   }

   public BooleanProperty getSelectedSubProperty() {
      return this.getValue().get(this.subPropertyIndex);
   }

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