class Updater {

    public static int isVersionNewerThan(String version, String check) {
        // If version starts with RecipeManager, remove it
        if (version.startsWith("RecipeManager")) {
            version = version.substring("RecipeManager".length()).trim();
        }

        if (check.startsWith("RecipeManager")) {
            check = check.substring("RecipeManager".length()).trim();
        }

        // Remove initial "v". Ex: v1.0.0 -> 1.0.0
        if (version.startsWith("v")) {
            version = version.substring(1).trim();
        }

        if (check.startsWith("v")) {
            check = check.substring(1).trim();
        }

        if (version.equals(check)) {
            return 0;
        }

        String[] versionSplit = version.split("[ -]");
        String[] checkSplit = check.split("[ -]");

        int compare = -2;
        String[] currentArray = versionSplit[0].split("\\.");
        String[] latestArray = checkSplit[0].split("\\.");

        int shortest = currentArray.length;
        int latestLength = latestArray.length;
        if (latestLength < shortest) {
            shortest = latestLength;
        }

        for (int i = 0; i < shortest; i++) {
            int c = Integer.parseInt(currentArray[i]);
            int l = Integer.parseInt(latestArray[i]);

            if (c > l) {
                compare = 1;
                break;
            } else if (l > c) {
                compare = -1;
                break;
            }
        }

        if (compare == -2) {
            boolean versionHasBeta = versionSplit.length > 1;
            boolean checkHasBeta = checkSplit.length > 1;
            if (versionHasBeta && checkHasBeta) {
                String versionBeta = versionSplit[1];
                versionBeta = versionBeta.replace("dev", "0.");
                versionBeta = versionBeta.replace("alpha", "1.");
                versionBeta = versionBeta.replace("beta", "2.");

                String checkBeta = checkSplit[1];
                checkBeta = checkBeta.replace("dev", "0.");
                checkBeta = checkBeta.replace("alpha", "1.");
                checkBeta = checkBeta.replace("beta", "2.");

                try {
                    double versionDouble = Double.parseDouble(versionBeta);
                    double checkDouble = Double.parseDouble(checkBeta);
                    if (versionDouble > checkDouble) {
                        compare = 1;
                    } else if (versionDouble < checkDouble) {
                        compare = -1;
                    }
                } catch (NumberFormatException e) {
                    // Versions aren't doubles, fail quietly
                }
            } else if (versionHasBeta) {
                // Only beta status on version means it's newer
                compare = 1;
            } else if (checkHasBeta) {
                // Only beta status on check means version is older
                compare = -1;
            }
        }

        return compare;
    }

}

class UpdaterTest {

    @Test
    public void testVersionEquals() {

        assertEquals(0, Updater.isVersionNewerThan("2.10.0", "RecipeManager v2.10.0"));
        assertEquals(0, Updater.isVersionNewerThan("2.10.0 alpha", "RecipeManager v2.10.0 alpha"));
        assertEquals(0, Updater.isVersionNewerThan("2.10.0 beta", "v2.10.0 beta"));
    }

}