package dev.crucifixbeta;

import dev.crucifixbeta.api.events.eventbus.EventBus;
import dev.crucifixbeta.api.events.impl.InitEvent;
import dev.crucifixbeta.core.Manager;
import dev.crucifixbeta.core.impl.BlurManager;
import dev.crucifixbeta.core.impl.BreakManager;
import dev.crucifixbeta.core.impl.CleanerManager;
import dev.crucifixbeta.core.impl.CommandManager;
import dev.crucifixbeta.core.impl.ConfigManager;
import dev.crucifixbeta.core.impl.FPSManager;
import dev.crucifixbeta.core.impl.FriendManager;
import dev.crucifixbeta.core.impl.HoleManager;
import dev.crucifixbeta.core.impl.HudItemManager;
import dev.crucifixbeta.core.impl.ModuleManager;
import dev.crucifixbeta.core.impl.PlayerManager;
import dev.crucifixbeta.core.impl.PopManager;
import dev.crucifixbeta.core.impl.RotationManager;
import dev.crucifixbeta.core.impl.ServerManager;
import dev.crucifixbeta.core.impl.ShaderManager;
import dev.crucifixbeta.core.impl.ThreadManager;
import dev.crucifixbeta.core.impl.TimerManager;
import dev.crucifixbeta.core.impl.TradeManager;
import dev.crucifixbeta.core.impl.XrayManager;
import dev.crucifixbeta.mod.features.SpearPort;
import dev.crucifixbeta.mod.gui.WindowTitleScroller;
import dev.crucifixbeta.mod.modules.impl.client.ClientSetting;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;

@Environment(EnvType.CLIENT)
public class CrucifixBeta implements ModInitializer {
   public static final String NAME;
   public static final String VERSION;
   public static final String CONFIG_DIR;
   public static final EventBus EVENT_BUS;
   public static HoleManager HOLE;
   public static PlayerManager PLAYER;
   public static TradeManager TRADE;
   public static CleanerManager CLEANER;
   public static HudItemManager HUD_ITEM;
   public static XrayManager XRAY;
   public static ModuleManager MODULE;
   public static CommandManager COMMAND;
   public static ConfigManager CONFIG;
   public static RotationManager ROTATION;
   public static BreakManager BREAK;
   public static PopManager POP;
   public static FriendManager FRIEND;
   public static TimerManager TIMER;
   public static ShaderManager SHADER;
   public static BlurManager BLUR;
   public static FPSManager FPS;
   public static ServerManager SERVER;
   public static ThreadManager THREAD;
   public static boolean loaded;
   public static long initTime;
   public static String userId;
   private final WindowTitleScroller titleScroller = new WindowTitleScroller();

   public static String getPrefix() {
      return ClientSetting.INSTANCE.prefix.getValue();
   }

   public static void save() {
      CONFIG.save();
      CLEANER.save();
      FRIEND.save();
      XRAY.save();
      TRADE.save();
      HUD_ITEM.save();
      System.out.println("[CrucifiX Beta] Saved");
   }

   private void register() {
      EVENT_BUS.registerLambdaFactory((lookupInMethod, klass) -> (Lookup)lookupInMethod.invoke(null, klass, MethodHandles.lookup()));
      Runtime.getRuntime().addShutdownHook(new Thread(() -> {
         if (loaded) {
            save();
         }
      }));
   }

   public void onInitialize() {
      this.register();
      ClientTickEvents.END_CLIENT_TICK.register(this.titleScroller::tick);
      SpearPort.init();
      MODULE = new ModuleManager();
      CONFIG = new ConfigManager();
      HOLE = new HoleManager();
      COMMAND = new CommandManager();
      FRIEND = new FriendManager();
      XRAY = new XrayManager();
      CLEANER = new CleanerManager();
      TRADE = new TradeManager();
      HUD_ITEM = new HudItemManager();
      ROTATION = new RotationManager();
      BREAK = new BreakManager();
      PLAYER = new PlayerManager();
      POP = new PopManager();
      TIMER = new TimerManager();
      SHADER = new ShaderManager();
      BLUR = new BlurManager();
      FPS = new FPSManager();
      SERVER = new ServerManager();
      CONFIG.load();
      THREAD = new ThreadManager();
      initTime = System.currentTimeMillis();
      loaded = true;
      EVENT_BUS.post(new InitEvent());
      Manager.getFolder();
   }

   static {

      CONFIG_DIR = "CrucifiXbeta";
      VERSION = "V3.0";
      NAME = "CrucifiX Beta";
      EVENT_BUS = new EventBus();
      loaded = false;
   }
}
