package dev.crucifixbeta.core;

import dev.crucifixbeta.CrucifixBeta;
import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;

@Environment(EnvType.CLIENT)
public class Manager {
   public static final MinecraftClient mc;

   public static File getFile(String s) {
      File var1 = getFolder();
      File var2 = new File(var1, s);
      migrateRootFileFromNestedCfg(var1, s, var2);
      return var2;
   }

   public static File getFolder() {
      File var0 = new File(resolveVersionDirectory(), "CrucifiXbeta");
      if (!var0.exists()) {
         var0.mkdirs();
      }

      return var0;
   }

   public static File getClientFolder() {
      return getFolder();
   }

   private static void migrateRootFileFromNestedCfg(File folder, String name, File target) {
      if (name != null && !name.contains("/") && !name.contains("\\") && !target.exists()) {
         File var3 = new File(new File(folder, "cfg"), name);
         if (var3.isFile()) {
            try {
               Files.copy(var3.toPath(), target.toPath(), StandardCopyOption.COPY_ATTRIBUTES);
            } catch (Exception var5) {
            }
         }
      }
   }

   private static File resolveVersionDirectory() {
      try {
         URI var0 = CrucifixBeta.class.getProtectionDomain().getCodeSource().getLocation().toURI();
         Path var1 = Path.of(var0).toAbsolutePath();
         Path var2 = var1.getParent();
         if (var2 != null && ("mods").equalsIgnoreCase(var2.getFileName().toString())) {
            Path var3 = var2.getParent();
            if (var3 != null) {
               return var3.toFile();
            }
         }
      } catch (Exception var4) {
      }

      return mc.runDirectory;
   }

   static {

      mc = MinecraftClient.getInstance();
   }
}
