package net.buycraft.plugin.shared.util;

import com.google.gson.GsonBuilder;
import lombok.experimental.UtilityClass;
import net.buycraft.plugin.data.responses.Version;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.io.IOException;

@UtilityClass
public class VersionUtil {
    public static Version getVersion(OkHttpClient client, String platform) throws IOException {
        Request request = new Request.Builder()
                .url("https://plugin.buycraft.net/versions/" + platform)
                .build();

        Response response = client.newCall(request).execute();

        if (!response.isSuccessful()) {
            return null;
        }

        try (ResponseBody body = response.body()) {
            return new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
                    .create()
                    .fromJson(body.string(), Version.class);
        }
    }

    public static boolean isVersionGreater(String one, String two) {
        String[] componentsOne = one.split("\\.");
        String[] componentsTwo = two.split("\\.");

        int verLen = Math.max(componentsOne.length, componentsTwo.length);

        int[] numOne = new int[verLen];
        int[] numTwo = new int[verLen];

        // We expect integers only
        for (int i = 0; i < componentsOne.length; i++) {
            numOne[i] = Integer.parseInt(componentsOne[i]);
        }
        for (int i = 0; i < componentsTwo.length; i++) {
            numTwo[i] = Integer.parseInt(componentsTwo[i]);
        }

        // Compare the versions
        for (int i = 0; i < numOne.length; i++) {
            if (numTwo[i] == numOne[i])
                continue;

            if (numTwo[i] > numOne[i])
                return true;
        }

        return false;
    }
}

@@ -1186,10 +1186,10 @@
 th.m
-ax
+in
 (com
@@ -1375,36 +1375,22 @@
  0; i %3C 
-componentsOne.length
+verLen
 ; i++) %7B
@@ -1488,36 +1488,22 @@
  0; i %3C 
-componentsTwo.length
+verLen
 ; i++) %7B
@@ -1638,21 +1638,14 @@
 i %3C 
-numOne.length
+verLen
 ; i+

10.2 should be greater than 10.1.4