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

import dev.crucifixbeta.core.impl.FontManager;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import dev.crucifixbeta.mod.modules.settings.impl.EnumSetting;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;

@Environment(EnvType.CLIENT)
public class Fonts extends Module {
   public static Fonts INSTANCE;
   private static final int UI_SIZE;
   private static final int SMALL_SIZE;
   private static final int FONT_STYLE;
   public final EnumSetting<Fonts.GlobalFont> globalFont = this.add(
      new EnumSetting<>("GlobalFont", Fonts.GlobalFont.DefaultEnglish).injectTask(this::refresh)
   );
   public final BooleanSetting replaceMinecraftFont = this.add(new BooleanSetting("ReplaceMinecraftFont", true));
   private Fonts.GlobalFont lastGlobalFont;
   private boolean pendingRefresh;

   public Fonts() {
      super("Fonts", Module.Category.Client);
      this.setChinese("\u5b57\u4f53");
      INSTANCE = this;
   }

   @Override
   public void onEnable() {
      this.refresh();
   }

   @Override
   public void onDisable() {
      if (!this.canRefreshNow()) {
         this.pendingRefresh = true;
      } else {
         FontManager.init();
      }
   }

   @Override
   public void onRender2D(DrawContext drawContext, float tickDelta) {
      if (this.pendingRefresh || this.globalFont.getValue() != this.lastGlobalFont) {
         this.pendingRefresh = false;
         this.refresh();
      }
   }

   public void refresh() {
      if (this.isOn()) {
         if (!this.canRefreshNow()) {
            this.pendingRefresh = true;
         } else {
            try {
               this.lastGlobalFont = this.globalFont.getValue();
               FontManager.ui = FontManager.alien(8.0F, this.lastGlobalFont.fileName(), 0);
               FontManager.small = FontManager.alien(6.0F, this.lastGlobalFont.fileName(), 0);
               FontManager.refreshSettingsFont(8.0F, 0);
            } catch (Exception var2) {
               var2.printStackTrace();
            }
         }
      }
   }

   private boolean canRefreshNow() {
      return mc != null && mc.getWindow() != null;
   }

   public static boolean shouldReplaceMinecraftFont() {
      return INSTANCE != null && INSTANCE.isOn() && INSTANCE.replaceMinecraftFont.getValue();
   }

   static {

      FONT_STYLE = 0;
      SMALL_SIZE = 6;
      UI_SIZE = 8;
   }
@Environment(EnvType.CLIENT)
   public static enum GlobalFont {
      DefaultEnglish("default", "default"),
      Font1("Font1", "font1"),
      Font2("Font2", "font2"),
      Font3("Font3", "font3"),
      Font4("Font4", "font4"),
      Font5("Font5", "font5"),
      Font6("Font6", "font6"),
      Font7("Font7", "font7");

      private final String displayName;
      private final String fileName;

      private GlobalFont(String displayName, String fileName) {
         this.displayName = displayName;
         this.fileName = fileName;
      }

      public String fileName() {
         return this.fileName;
      }

      @Override
      public String toString() {
         return this.displayName;
      }

}
}
