package tech.nevets.jaml.util;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.io.FileUtils;
import tech.nevets.jaml.Config;
import tech.nevets.jaml.JAML;
import tech.nevets.jaml.objects.Version;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class VersionUtils {
    public static Version[] versionsList;

    public static Version[] getVersionsList() {
        Gson gson = new Gson();
        JsonObject versionManifestJson = getList();
        JsonArray versionsJsonArray = versionManifestJson.getAsJsonArray("versions");
        Version[] versionArray = gson.fromJson(versionsJsonArray, Version[].class);

        if (Config.CONFIG.getBoolean("game.show-snapshots")) {
            return versionArray;
        } else {
            ArrayList<Version> versionArrayList = new ArrayList<>();
            for (Version version : versionArray) {
                if (version.getType().equalsIgnoreCase("release")) {
                    versionArrayList.add(version);
                }
            }

            return versionArrayList.toArray(new Version[versionArrayList.size()]);
        }
    }

    public static Version toVersion(String version) {
        Version[] versionArray = getVersionsList();

        for (Version value : versionArray) {
            if (value.getId().equalsIgnoreCase(version)) {
                return value;
            }
        }
        return null;
    }

    /**
     *
     * @param branch, release or snapshot
     * @return something
     */
    public static String getLatestVersion(String branch) {
        JsonObject versionManifestJson = getList();
        JsonObject latestJsonObject = versionManifestJson.get("latest").getAsJsonObject();

        return latestJsonObject.get(branch).getAsString();
    }

    public static void downloadList() {
        try {
            URL versionManifestUrl = new URL("https://launchermeta.mojang.com/mc/game/version_manifest.json");
            FileUtils.copyURLToFile(versionManifestUrl, new File(JAML.path + "\\data\\version.json"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static JsonObject getList() {
        try {
            File versionManifestFile = new File(JAML.path + "\\data\\version.json");

            return new Gson().fromJson(FileUtils.readFileToString(versionManifestFile, StandardCharsets.UTF_8), JsonObject.class);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void getLibs() {
        try {
            Version version = toVersion(ProfileUtils.getActiveProfile().getVersion());
            URL versionManifestUrl = version.getUrl();
            FileUtils.copyURLToFile(versionManifestUrl, new File(JAML.path + "\\data\\" + version.getId() + ".json"));

            File versionFile = new File(JAML.path + "\\data\\" + version.getId() + ".json");
            JsonObject versionJson = new Gson().fromJson(FileUtils.readFileToString(versionFile, StandardCharsets.UTF_8), JsonObject.class);

            JsonArray libsJson = versionJson.getAsJsonArray("libraries");
            ArrayList<String> cache = new ArrayList<>();
            String[] cacheArray = CacheUtils.getCache("libs");

            if (cacheArray != null) {
                for (String value : CacheUtils.getCache("libs")) {
                    cache.add(value);
                }
            }

            //TODO Account for only native downloads
            //TODO Implement OS Check and set constants accordingly
            if (!cache.contains(version.getId())) {
                for (int i = 0; i < libsJson.size(); i++) {
                    JsonElement downloads = libsJson.get(i);
                    JsonObject download = downloads.getAsJsonObject().getAsJsonObject("downloads");
                    JsonElement artifact = download.getAsJsonObject().get("artifact");
                    JsonElement classifiers = download.getAsJsonObject().get("classifiers");
                    JsonElement pathJson;

                    if (artifact != null) {
                        pathJson = artifact.getAsJsonObject().get("path");
                    } else if (classifiers != null) {
                        //TODO add logic for classifiers
                        System.out.println("classifier");
                        continue;
                    } else {
                        System.out.println("Something unexpected was in the libraries array...");
                        return;
                    }

                    String path = pathJson.getAsString();

                    String url = artifact.getAsJsonObject().get("url").toString();

                    FileUtils.copyURLToFile(new URL(url.replace("\"", "")), new File((ProfileUtils.getActiveProfile().getGamePath()) + "\\libraries\\" + path));
                }

                CacheUtils.addToCache("libs", version.getId());
            }

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



    public static void updateVersionsList() {
        versionsList = getVersionsList();
    }
}
