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

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import wtf.opal.client.feature.module.Module;
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.ModePropertyComponent;

@Environment(EnvType.CLIENT)
public final class ModeProperty<T extends Enum<T>> extends Property<T> {
   private final T[] values;
   private Module module;
   private boolean theme;

   public ModeProperty(String name, T value) {
      super(name);
      this.setValue((T)value);
      this.values = this.getEnumConstants();
   }

   public ModeProperty(String name, T value, T[] values) {
      super(name);
      this.setValue((T)value);
      this.values = (T[])values;
   }

   public ModeProperty(String name, T value, boolean theme) {
      super(name);
      this.setValue((T)value);
      this.values = this.getEnumConstants();
      this.theme = theme;
   }

   public ModeProperty(String name, ModuleMode<?> parent, T value) {
      super(name, parent);
      this.setValue((T)value);
      this.values = this.getEnumConstants();
   }

   public ModeProperty(String name, Module module, T value) {
      super(name);
      this.setValue((T)value);
      this.values = this.getEnumConstants();
      this.module = module;
      module.setModeProperty(this);
   }

   private T[] getEnumConstants() {
      return (T[])this.getValue().getClass().getEnumConstants();
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.NullPointerException: Cannot invoke "org.jetbrains.java.decompiler.struct.gen.VarType.isGeneric()" because "newRet" is null
      //   at org.jetbrains.java.decompiler.modules.decompiler.exps.InvocationExprent.getInferredExprType(InvocationExprent.java:634)
      //   at org.jetbrains.java.decompiler.modules.decompiler.exps.FunctionExprent.getInferredExprType(FunctionExprent.java:243)
      //   at org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor.getCastedExprent(ExprProcessor.java:966)
      //   at org.jetbrains.java.decompiler.modules.decompiler.exps.ExitExprent.toJava(ExitExprent.java:86)
      //   at org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor.listToJava(ExprProcessor.java:895)
      //   at org.jetbrains.java.decompiler.modules.decompiler.stats.BasicBlockStatement.toJava(BasicBlockStatement.java:90)
      //   at org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement.toJava(RootStatement.java:36)
      //   at org.jetbrains.java.decompiler.main.ClassWriter.writeMethod(ClassWriter.java:1283)
      //
      // Bytecode:
      // 00: aload 0
      // 01: invokevirtual wtf/opal/client/feature/module/property/impl/mode/ModeProperty.getValue ()Ljava/lang/Object;
      // 04: checkcast java/lang/Enum
      // 07: invokevirtual java/lang/Object.getClass ()Ljava/lang/Class;
      // 0a: invokevirtual java/lang/Class.getEnumConstants ()[Ljava/lang/Object;
      // 0d: checkcast [Ljava/lang/Enum;
      // 10: areturn
   }

   public T[] getValues() {
      return this.values;
   }

   public void setValueOrdinal(int value) {
      if (this.module != null && this.module.isEnabled()) {
         this.module.getModuleModes().forEach(ModuleMode::onDisable);
      }

      this.setValue(this.values[value]);
      if (this.module != null) {
         for (ModuleMode var3 : this.module.getModuleModes()) {
            if (var3.getEnumValue().ordinal() == value && this.module.isEnabled()) {
               var3.onEnable();
               break;
            }
         }
      }
   }

   public void cycle(boolean forwards) {
      int var2 = this.getValue().ordinal();
      int var3 = (var2 + (forwards ? 1 : this.values.length - 1)) % this.values.length;
      this.setValueOrdinal(var3);
   }

   public boolean isTheme() {
      return this.theme;
   }

   public boolean is(T value) {
      return this.getValue() == value;
   }

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

   @Override
   public void applyValue(Object propertyValue) {
      if (propertyValue instanceof String var2) {
         for (Enum var6 : this.values) {
            if (var6.name().equals(var2)) {
               this.setValueOrdinal(var6.ordinal());
               break;
            }
         }
      }
   }
}
