package net.minecraft.launcher.updater;

import com.mojang.launcher.OperatingSystem;
import com.mojang.launcher.versions.CompleteVersion;
import com.mojang.launcher.versions.Version;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Set;
import net.minecraft.launcher.game.MinecraftReleaseType;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LocalVersionList extends FileBasedVersionList {
   private static final Logger LOGGER = LogManager.getLogger();
   private final File baseDirectory;
   private final File baseVersionsDir;

   public LocalVersionList(File baseDirectory) {
      if (baseDirectory != null && baseDirectory.isDirectory()) {
         this.baseDirectory = baseDirectory;
         this.baseVersionsDir = new File(this.baseDirectory, "versions");
         if (!this.baseVersionsDir.isDirectory()) {
            this.baseVersionsDir.mkdirs();
         }

      } else {
         throw new IllegalArgumentException("Base directory is not a folder!");
      }
   }

   protected InputStream getFileInputStream(String path) throws FileNotFoundException {
      return new FileInputStream(new File(this.baseDirectory, path));
   }

   public void refreshVersions() throws IOException {
      this.clearCache();
      File[] files = this.baseVersionsDir.listFiles();
      if (files != null) {
         File[] var2 = files;
         int var3 = files.length;

         for(int var4 = 0; var4 < var3; ++var4) {
            File directory = var2[var4];
            String id = directory.getName();
            File jsonFile = new File(directory, id + ".json");
            if (directory.isDirectory() && jsonFile.exists()) {
               try {
                  String path = "versions/" + id + "/" + id + ".json";
                  this.getContent(path);
                  CompleteVersion version = (CompleteVersion)this.gson.fromJson(this.getContent(path), CompleteMinecraftVersion.class);
                  if (version.getType() == null) {
                     LOGGER.warn("Ignoring: " + path + "; it has an invalid version specified");
                     return;
                  }

                  if (version.getId().equals(id)) {
                     this.addVersion(version);
                  } else {
                     LOGGER.warn("Ignoring: " + path + "; it contains id: '" + version.getId() + "' expected '" + id + "'");
                  }
               } catch (RuntimeException var11) {
                  LOGGER.error((String)("Couldn't load local version " + jsonFile.getAbsolutePath()), (Throwable)var11);
               }
            }
         }

         Iterator var12 = this.getVersions().iterator();

         while(true) {
            Version version;
            MinecraftReleaseType type;
            do {
               if (!var12.hasNext()) {
                  return;
               }

               version = (Version)var12.next();
               type = (MinecraftReleaseType)version.getType();
            } while(this.getLatestVersion(type) != null && !this.getLatestVersion(type).getUpdatedTime().before(version.getUpdatedTime()));

            this.setLatestVersion(version);
         }
      }
   }

   public void saveVersion(CompleteVersion version) throws IOException {
      String text = this.serializeVersion(version);
      File target = new File(this.baseVersionsDir, version.getId() + "/" + version.getId() + ".json");
      if (target.getParentFile() != null) {
         target.getParentFile().mkdirs();
      }

      PrintWriter writer = new PrintWriter(target);
      writer.print(text);
      writer.close();
   }

   public File getBaseDirectory() {
      return this.baseDirectory;
   }

   public boolean hasAllFiles(CompleteMinecraftVersion version, OperatingSystem os) {
      Set<String> files = version.getRequiredFiles(os);
      Iterator var4 = files.iterator();

      String file;
      do {
         if (!var4.hasNext()) {
            return true;
         }

         file = (String)var4.next();
      } while((new File(this.baseDirectory, file)).isFile());

      return false;
   }

   public void uninstallVersion(Version version) {
      super.uninstallVersion(version);
      File dir = new File(this.baseVersionsDir, version.getId());
      if (dir.isDirectory()) {
         FileUtils.deleteQuietly(dir);
      }

   }
}
