package wtf.opal.client;

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.impl.client.ClickGui;
import dev.crucifixbeta.mod.modules.impl.client.hud.HudSetting;
import java.util.ArrayList;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import wtf.opal.client.binding.repository.BindRepository;
import wtf.opal.client.feature.module.impl.crucifix.CrucifixModuleAdapter;
import wtf.opal.client.feature.module.repository.ModuleRepository;
import wtf.opal.client.notification.NotificationManager;

@Environment(EnvType.CLIENT)
public final class OpalClient {
   private final NotificationManager notificationManager;
   private final BindRepository bindRepository;
   private ModuleRepository moduleRepository;
   private String user = "User";
   private boolean postInitialization;
   private boolean shutdownHookRegistered;
   private static OpalClient instance;

   private OpalClient() {
      this.notificationManager = new NotificationManager();
      this.bindRepository = new BindRepository();
   }

   public void runPostInitializations() {
      if (this.moduleRepository == null || this.shouldRebuildCrucifixRepository()) {
         this.moduleRepository = this.buildCrucifixRepository();
      }

      if (!this.shutdownHookRegistered) {
         Runtime.getRuntime().addShutdownHook(new Thread(this::onShutdown));
         this.shutdownHookRegistered = true;
      }

      this.postInitialization = true;
   }

   private boolean shouldRebuildCrucifixRepository() {
      return CrucifixBeta.MODULE != null && (this.moduleRepository == null || this.moduleRepository.getModules().isEmpty());
   }

   private ModuleRepository buildCrucifixRepository() {
      ArrayList<wtf.opal.client.feature.module.Module> var1 = new ArrayList<>();
      if (CrucifixBeta.MODULE != null) {
         for (Module var3 : CrucifixBeta.MODULE.getModules()) {
            if (!(var3 instanceof ClickGui) && !(var3 instanceof HudSetting)) {
               var1.add(new CrucifixModuleAdapter(var3));
            }
         }
      }

      return ModuleRepository.fromModules(var1.toArray(new wtf.opal.client.feature.module.Module[0]));
   }

   private void onShutdown() {
   }

   public boolean isPostInitialization() {
      return this.postInitialization;
   }

   public ModuleRepository getModuleRepository() {
      if (this.shouldRebuildCrucifixRepository()) {
         this.moduleRepository = this.buildCrucifixRepository();
      }

      return this.moduleRepository;
   }

   public BindRepository getBindRepository() {
      return this.bindRepository;
   }

   public NotificationManager getNotificationManager() {
      return this.notificationManager;
   }

   public String getUser() {
      return this.user;
   }

   public void setUser(String user) {
      this.user = user;
   }

   public static OpalClient getInstance() {
      if (instance == null) {
         instance = new OpalClient();
      }

      return instance;
   }

   public static void setInstance() {
      instance = new OpalClient();
      instance.runPostInitializations();
   }
}
