package org.mcupdater.api;

import org.mcupdater.MCUApp;

import java.io.IOException;
import java.util.Properties;

public class Version {
	public static final int MAJOR_VERSION;
	public static final int MINOR_VERSION;
	public static final int BUILD_VERSION;
	public static final String BUILD_BRANCH;
	public static final String BUILD_LABEL;
	static {
		Properties prop = new Properties();
		try {
			prop.load(Version.class.getResourceAsStream("/version.properties"));
		} catch (IOException e) {
		}
		MAJOR_VERSION = Integer.valueOf(prop.getProperty("major","0"));
		MINOR_VERSION = Integer.valueOf(prop.getProperty("minor","0"));
		BUILD_VERSION = Integer.valueOf(prop.getProperty("build_version","0"));
		BUILD_BRANCH = prop.getProperty("git_branch","unknown");
		if( BUILD_BRANCH.equals("unknown") || BUILD_BRANCH.equals("master") ) {
			BUILD_LABEL = "";
		} else {
			BUILD_LABEL = " ("+BUILD_BRANCH+")";
		}
	}
	
	public static final String API_VERSION = MAJOR_VERSION + "." + MINOR_VERSION;
	public static final String VERSION = "v"+MAJOR_VERSION+"."+MINOR_VERSION+"."+BUILD_VERSION;
	
	public static boolean isVersionOld(String packVersion) {
		if( packVersion == null ) return false;	// can't check anything if they don't tell us
		String parts[] = packVersion.split("\\.");
		try {
			int mcuParts[] = { MAJOR_VERSION, MINOR_VERSION, BUILD_VERSION };
			for( int q = 0; q < mcuParts.length && q < parts.length; ++q ) {
				int packPart = Integer.valueOf(parts[q]);
				if( packPart > mcuParts[q] ) return true;
				if( packPart < mcuParts[q] ) return false; // Since we check major, then minor, then build, if the required value < current value, we can stop checking.
			}
			return false;
		} catch( NumberFormatException e ) {
			log("Got non-numerical pack format version '"+packVersion+"'");
		} catch( ArrayIndexOutOfBoundsException e ) {
			log("Got malformed pack format version '"+packVersion+"'");
		}
		return false;
	}
	
	public static boolean requestedFeatureLevel(String packVersion, String featureLevelVersion) {
		String packParts[] = packVersion.split("\\.");
		String featureParts[] = featureLevelVersion.split("\\.");
		try {
			for (int q = 0; q < featureParts.length; ++q ) {
				if (Integer.valueOf(packParts[q]) > Integer.valueOf(featureParts[q])) return true;
				if (Integer.valueOf(packParts[q]) < Integer.valueOf(featureParts[q])) return false;
			}
			return true;
		} catch( NumberFormatException e ) {
			log("Got non-numerical pack format version '"+packVersion+"'");
		} catch( ArrayIndexOutOfBoundsException e ) {
			log("Got malformed pack format version '"+packVersion+"'");
		}
		return false;
	}
	
	public static boolean isMasterBranch() {
		return BUILD_BRANCH.equals("master");
	}
	public static boolean isDevBranch() {
		return BUILD_BRANCH.equals("develop");
	}
	
	// for error logging support
	public static void setApp( MCUApp app ) {
		_app = app;
	}
	private static MCUApp _app;
	private static void log(String msg) {
		if( _app != null ) {
			_app.log(msg);
		} else {
			System.out.println(msg);
		}
	}
}

@@ -497,29 +497,86 @@
 %7B%0A%09%09%7D%0A%09%09
-MAJOR_VERSION
+int major;%0A%09%09int minor;%0A%09%09int build;%0A%09%09String branch;%0A%09%09try %7B%0A%09%09%09major
  = Integ
@@ -607,24 +607,25 @@
 rty(%22major%22,
+ 
 %220%22));%0A%09%09MIN
@@ -621,29 +621,22 @@
 0%22));%0A%09%09
-MINOR_VERSION
+%09minor
  = Integ
@@ -671,16 +671,17 @@
 %22minor%22,
+ 
 %220%22));%0A%09
@@ -681,29 +681,22 @@
 0%22));%0A%09%09
-BUILD_VERSION
+%09build
  = Integ
@@ -739,16 +739,17 @@
 ersion%22,
+ 
 %220%22));%0A%09
@@ -749,28 +749,23 @@
 0%22));%0A%09%09
-BUILD_BRANCH
+%09branch
  = prop.
@@ -789,16 +789,17 @@
 branch%22,
+ 
 %22unknown
@@ -802,16 +802,213 @@
 nown%22);%0A
+%09%09%7D catch (Exception e) %7B%0A%09%09%09major = 3;%0A%09%09%09minor = 2;%0A%09%09%09build = 999;%0A%09%09%09branch = %22develop%22;%0A%09%09%7D%0A%09%09MAJOR_VERSION = major;%0A%09%09MINOR_VERSION = minor;%0A%09%09BUILD_VERSION = build;%0A%09%09BUILD_BRANCH = branch;%0A
 %09%09if( BU

Update Version classes to handle launch from IDE.