package tech.nevets.jaml;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import tech.nevets.jaml.gui.StartupGui;
import tech.nevets.jaml.util.HttpUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;

public class AutoUpdater {

    public static void checkForUpdate() {
        try {
            String version = HttpUtils.get(new URI("https://api.nevets.tech/jaml/version.json"), new HashMap<>());
            JsonElement versionJson = new Gson().fromJson(version, JsonElement.class);

            if (versionJson.getAsJsonObject().get("version").toString().replace("\"", "").equalsIgnoreCase(Config.CONFIG.getString("launcher.version"))) {
                String command = JAML.path + "\\jre\\bin\\javaw.exe -jar " + JAML.path + "\\JAML-Updater.jar";
                Runtime.getRuntime().exec(command);
            }

        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
    }
}
