package dev.crucifixbeta.mod.modules.settings.impl;

import dev.crucifixbeta.mod.modules.settings.EnumConverter;
import dev.crucifixbeta.mod.modules.settings.Setting;
import java.util.function.BooleanSupplier;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

@Environment(EnvType.CLIENT)
public class EnumSetting<T extends Enum<T>> extends Setting {
   private final T defaultValue;
   private T value;
   private Runnable task = null;
   private boolean injectTask = false;

   public EnumSetting(String name, T defaultValue) {
      super(name);
      this.value = (T)defaultValue;
      this.defaultValue = (T)defaultValue;
   }

   public EnumSetting(String name, T defaultValue, BooleanSupplier visibilityIn) {
      super(name, visibilityIn);
      this.value = (T)defaultValue;
      this.defaultValue = (T)defaultValue;
   }

   public void loadSetting(String enumString) {
      EnumConverter var2 = new EnumConverter();
      if (enumString == null) {
         this.value = this.defaultValue;
      } else {
         Enum var3 = var2.get(this.defaultValue, enumString);
         this.value = (T)(var3 != null ? var3 : this.defaultValue);
      }
   }

   public void increaseEnum() {
      this.value = (T)EnumConverter.increaseEnum(this.value);
      if (this.injectTask) {
         this.task.run();
      }
   }

   public void resetValue() {
      this.value = this.defaultValue;
   }

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

   public void setValue(T value) {
      this.value = (T)value;
   }

   public void setEnumValue(String value) {
      for (Enum var5 : (Enum[])this.value.getDeclaringClass().getEnumConstants()) {
         if (var5.name().equalsIgnoreCase(value)) {
            this.value = (T)var5;
            if (this.injectTask) {
               this.task.run();
            }
         }
      }
   }

   public EnumSetting<T> injectTask(Runnable task) {
      this.task = task;
      this.injectTask = true;
      return this;
   }

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

   public T getDefaultValue() {
      return this.defaultValue;
   }
}
