package dev.crucifixbeta.core.impl;

import dev.crucifixbeta.mod.gui.fonts.FontRenderer;
import dev.crucifixbeta.mod.modules.impl.client.Fonts;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Character.UnicodeBlock;
import java.util.Objects;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

@Environment(EnvType.CLIENT)
public class FontManager {
   public static FontRenderer ui;
   public static FontRenderer small;
   public static FontRenderer icon;
   public static FontRenderer settings;
   public static FontRenderer mainMenu;
   public static FontRenderer mainMenuTitle;
   public static FontRenderer mainMenuSubtitle;
   private static final String ALIEN_FONT_PATH;
   private static final String CRUCIFIX_FONT_PATH;

   public static boolean isCustomFontEnabled() {
      return Fonts.INSTANCE != null && Fonts.INSTANCE.isOn();
   }

   public static boolean isShadowEnabled() {
      return false;
   }

   public static void init() {
      try {
         ui = alien(8.0F, "default", 0);
         small = alien(6.0F, "default", 0);
         icon = assetsWithoutOffset(8.0F, "icon", 0);
         settings = alien(8.0F, "default", 0);
         mainMenu = crucifix(12.0F, "productsans-bold", 0);
         mainMenuTitle = crucifix(22.0F, "productsans-bold", 0);
         mainMenuSubtitle = crucifix(11.0F, "productsans-bold", 0);
      } catch (Exception var1) {
         var1.printStackTrace();
      }
   }

   public static FontRenderer alien(float size, String name, int style) throws IOException, FontFormatException {
      ClassLoader var3 = FontManager.class.getClassLoader();
      InputStream var4 = var3.getResourceAsStream("assets/alien/fonts/" + name + ".ttf");
      InputStream var5 = var3.getResourceAsStream("assets/alien/fonts/default.ttf");
      InputStream var6 = var3.getResourceAsStream("assets/minecraft/font/font.ttf");
      InputStream var7 = var4 != null ? var4 : (var5 != null ? var5 : var6);
      return new FontRenderer(
         Font.createFont(0, Objects.requireNonNull(var7)).deriveFont(style, size), getFont("msyh", style, (int)size), size
      );
   }

   public static FontRenderer crucifix(float size, String name, int style) throws IOException, FontFormatException {
      ClassLoader var3 = FontManager.class.getClassLoader();
      InputStream var4 = var3.getResourceAsStream("assets/crucifixbeta/fonts/" + name + ".ttf");
      InputStream var5 = var3.getResourceAsStream("assets/crucifixbeta/fonts/productsans-regular.ttf");
      InputStream var6 = var3.getResourceAsStream("assets/alien/fonts/default.ttf");
      InputStream var7 = var4 != null ? var4 : (var5 != null ? var5 : var6);
      return new FontRenderer(
         Font.createFont(0, Objects.requireNonNull(var7)).deriveFont(style, size), getFont("msyh", style, (int)size), size
      );
   }

   public static FontRenderer assets(float size, String font, int style, String alternate) throws IOException, FontFormatException {
      ClassLoader var4 = FontManager.class.getClassLoader();
      InputStream var5 = var4.getResourceAsStream("assets/frogclient/font/" + font + ".ttf");
      InputStream var6 = var4.getResourceAsStream("assets/minecraft/font/font.ttf");
      InputStream var7 = var5 != null ? var5 : var6;
      return new FontRenderer(Font.createFont(0, Objects.requireNonNull(var7)).deriveFont(style, size), getFont(alternate, style, (int)size), size);
   }

   public static FontRenderer assetsWithoutOffset(float size, String name, int style) throws IOException, FontFormatException {
      ClassLoader var3 = FontManager.class.getClassLoader();
      InputStream var4 = var3.getResourceAsStream("assets/frog/icon/" + name + ".ttf");
      InputStream var5 = var3.getResourceAsStream("assets/minecraft/font/font.ttf");
      InputStream var6 = var4 != null ? var4 : var5;
      return new FontRenderer(Font.createFont(0, Objects.requireNonNull(var6)).deriveFont(style, size), size);
   }

   public static FontRenderer assets(float size, String name, int style) throws IOException, FontFormatException {
      ClassLoader var3 = FontManager.class.getClassLoader();
      InputStream var4 = var3.getResourceAsStream("assets/frogclient/font/" + name + ".ttf");
      InputStream var5 = var3.getResourceAsStream("assets/minecraft/font/font.ttf");
      InputStream var6 = var4 != null ? var4 : var5;
      return new FontRenderer(Font.createFont(0, Objects.requireNonNull(var6)).deriveFont(style, size), size);
   }

   public static FontRenderer create(int size, String font, int style, String alternate) {
      return new FontRenderer(getFont(font, style, size), getFont(alternate, style, size), (float)size);
   }

   public static FontRenderer create(int size, String font, int style) {
      return new FontRenderer(getFont(font, style, size), (float)size);
   }

   public static boolean shouldUseClientFont(String text, boolean customFont) {
      return customFont || ui != null && containsCjk(text);
   }

   public static boolean containsCjk(String text) {
      if (text != null && !text.isEmpty()) {
         for (int var1 = 0; var1 < text.length(); var1++) {
            UnicodeBlock var2 = UnicodeBlock.of(text.charAt(var1));
            if (var2 == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
               || var2 == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
               || var2 == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
               || var2 == UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
               || var2 == UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
               || var2 == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
               || var2 == UnicodeBlock.GENERAL_PUNCTUATION) {
               return true;
            }
         }

         return false;
      } else {
         return false;
      }
   }

   public static boolean shouldUseSettingsFont(String text) {
      return settings != null && containsCjk(text);
   }

   public static void refreshSettingsFont(float size, int style) throws IOException, FontFormatException {
      settings = alien(size, "default", style);
   }

   private static Font getFont(String font, int style, int size) {
      File var3 = new File("C:\\Windows\\Fonts");

      try {
         for (File var7 : var3.listFiles()) {
            if (var7.getName()
               .replace(".ttf", "")
               .replace(".ttc", "")
               .replace(".otf", "")
               .equalsIgnoreCase(font)) {
               try {
                  return Font.createFont(0, var7).deriveFont(style, (float)size);
               } catch (Exception var9) {
                  var9.printStackTrace();
               }
            }
         }

         for (File var15 : var3.listFiles()) {
            if (var15.getName().startsWith(font)) {
               try {
                  return Font.createFont(0, var15).deriveFont(style, (float)size);
               } catch (Exception var10) {
                  var10.printStackTrace();
               }
            }
         }
      } catch (Exception var11) {
      }

      return new Font("Dialog", style, size);
   }

   static {

      CRUCIFIX_FONT_PATH = "assets/crucifixbeta/fonts/";
      ALIEN_FONT_PATH = "assets/alien/fonts/";
   }
}
