package com.github.to2mbn.jmccc.version;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.json.JSONException;
import com.github.to2mbn.jmccc.option.MinecraftDirectory;

/**
 * A tool class for resolving versions.
 * 
 * @author yushijinhun
 */
public final class Versions {

    private static final VersionParser PARSER = new VersionParser();

    /**
     * Resolves the version.
     * 
     * @param minecraftDir the minecraft directory
     * @param version the version name
     * @return the version object, null if <code>version==null</code>, or the version does not exist
     * @throws IOException if an I/O error has occurred during resolving version
     * @throws NullPointerException if <code>minecraftDir==null</code>
     */
    public static Version resolveVersion(MinecraftDirectory minecraftDir, String version) throws IOException {
        Objects.requireNonNull(minecraftDir);
        if (version == null) {
            return null;
        }

        if (doesVersionExists(minecraftDir, version)) {
            try {
                return PARSER.parseVersion(minecraftDir, version);
            } catch (JSONException e) {
                throw new IOException("unable to resolve json", e);
            }
        } else {
            return null;
        }
    }

    /**
     * Returns a set of versions in the given minecraft directory.
     * <p>
     * This method returns a non-threaded safe, unordered set.
     * 
     * @param minecraftDir the minecraft directory
     * @return a set of versions
     * @throws NullPointerException if <code>minecraftDir==null</code>
     */
    public static Set<String> getVersions(MinecraftDirectory minecraftDir) {
        Objects.requireNonNull(minecraftDir);
        Set<String> versions = new HashSet<>();

        // null if the 'versions' dir not exists
        File[] subdirs = minecraftDir.getVersions().listFiles();
        if (subdirs != null) {
            for (File file : subdirs) {
                if (file.isDirectory() && doesVersionExists(minecraftDir, file.getName())) {
                    versions.add(file.getName());
                }
            }
        }
        return versions;
    }

    /**
     * Resolves the asset index.
     * 
     * @param minecraftDir the minecraft directory
     * @param assets the name of the asset index, you can get this via {@link Version#getAssets()}
     * @return the asset index
     * @throws IOException if an I/O error has occurred during resolving asset index
     * @throws NullPointerException <code>minecraftDir==null</code>
     */
    public static Set<Asset> resolveAssets(MinecraftDirectory minecraftDir, String assets) throws IOException {
        Objects.requireNonNull(minecraftDir);
        if (assets == null || !minecraftDir.getAssetIndex(assets).isFile()) {
            return null;
        }

        try {
            return PARSER.parseAssets(minecraftDir, assets);
        } catch (JSONException e) {
            throw new IOException("unable to resolve json", e);
        }
    }

    private static boolean doesVersionExists(MinecraftDirectory minecraftDir, String version) {
        return minecraftDir.getVersionJson(version).isFile();
    }

    private Versions() {
    }

}

@@ -606,39 +606,8 @@
  if 
-%3Ccode%3Eversion==null%3C/code%3E, or 
 the 
@@ -765,32 +765,47 @@
 necraftDir==null
+%7C%7Cversion==null
 %3C/code%3E%0A     */%0A
@@ -973,65 +973,40 @@
     
-if (version == null) %7B%0A            return null;%0A        %7D
+Objects.requireNonNull(version);
 %0A%0A  
@@ -1023,33 +1023,32 @@
 doesVersionExist
-s
 (minecraftDir, v
@@ -2059,17 +2059,16 @@
 ionExist
-s
 (minecra
@@ -2584,32 +2584,46 @@
 necraftDir==null
+%7C%7Cassets==null
 %3C/code%3E%0A     */%0A
@@ -2792,30 +2792,52 @@
     
-if (assets == null %7C%7C 
+Objects.requireNonNull(assets);%0A        if (
 !min
@@ -3151,17 +3151,16 @@
 ionExist
-s
 (Minecra

throw NullPointerException in Versions, instead of returning null