package wtf.opal.client.renderer.repository;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader;
import wtf.opal.client.renderer.image.NVGImageRenderer;

@Environment(EnvType.CLIENT)
public final class ImageRepository {
   private static final HashMap<String, NVGImageRenderer> imageMap;

   public static NVGImageRenderer getImage(Path path, int flags) {
      String var2 = path.toString();
      if (imageMap.containsKey(var2)) {
         return imageMap.get(var2);
      } else {
         try {
            imageMap.put(var2, new NVGImageRenderer(Files.newInputStream(path), flags));
            return imageMap.get(var2);
         } catch (IOException var4) {
            var4.printStackTrace();
            return null;
         }
      }
   }

   public static NVGImageRenderer getImage(String path, int flags) {
      if (imageMap.containsKey(path)) {
         return imageMap.get(path);
      } else {
         Path var2 = FabricLoader.getInstance()
            .getModContainer("crucifixbeta")
            .flatMap(c -> c.findPath("assets/crucifixbeta/" + path))
            .orElse(null);

         try {
            imageMap.put(path, new NVGImageRenderer(Files.newInputStream(var2), flags));
            return imageMap.get(path);
         } catch (IOException var4) {
            var4.printStackTrace();
            return null;
         }
      }
   }

   public static NVGImageRenderer getImage(String path) {
      if (imageMap.containsKey(path)) {
         return imageMap.get(path);
      } else {
         Path var1 = FabricLoader.getInstance()
            .getModContainer("crucifixbeta")
            .flatMap(c -> c.findPath("assets/crucifixbeta/" + path))
            .orElse(null);

         try {
            imageMap.put(path, new NVGImageRenderer(Files.newInputStream(var1)));
            return imageMap.get(path);
         } catch (IOException var3) {
            var3.printStackTrace();
            return null;
         }
      }
   }

   static {

      imageMap = new HashMap<>();
   }
}
