package wtf.opal.client.renderer.repository;

import dev.crucifixbeta.mod.modules.impl.client.Fonts;
import java.io.InputStream;
import java.util.HashMap;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.impl.launch.knot.Knot;
import wtf.opal.client.renderer.text.NVGTextRenderer;

@Environment(EnvType.CLIENT)
public final class FontRepository {
   private static final HashMap<String, NVGTextRenderer> TEXT_RENDERER_MAP;

   public static NVGTextRenderer getFont(String name) {
      String var1 = getCacheName(name);
      if (TEXT_RENDERER_MAP.containsKey(var1)) {
         return TEXT_RENDERER_MAP.get(var1);
      } else {
         InputStream var2 = Knot.getLauncher().getTargetClassLoader().getResourceAsStream(getResourcePath(name));
         if (var2 != null) {
            TEXT_RENDERER_MAP.put(var1, new NVGTextRenderer(var1, var2));
            return TEXT_RENDERER_MAP.get(var1);
         } else {
            throw new RuntimeException("Font not found: " + name);
         }
      }
   }

   private static String getCacheName(String name) {
      Fonts.GlobalFont var1 = getGlobalFont();
      return var1 != null && !isIconFont(name) ? "crucifix-global-" + var1.fileName() : name;
   }

   private static String getResourcePath(String name) {
      Fonts.GlobalFont var1 = getGlobalFont();
      return var1 != null && !isIconFont(name) ? "assets/alien/fonts/" + var1.fileName() + ".ttf" : "assets/crucifixbeta/fonts/" + name + ".ttf";
   }

   private static Fonts.GlobalFont getGlobalFont() {
      return Fonts.INSTANCE == null ? null : Fonts.INSTANCE.globalFont.getValue();
   }

   private static boolean isIconFont(String name) {
      return name != null && name.startsWith("material");
   }

   static {

      TEXT_RENDERER_MAP = new HashMap<>();
   }
}
