package at.fhtw.mcs.util;

import java.io.IOException;
import java.util.ResourceBundle;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;

public class VersionCompare implements Runnable {
	private String newestVersion;
	private ResourceBundle bundle;
	private Document doc;

	public VersionCompare(ResourceBundle bundle) {
		this.bundle = bundle;
	}

	@Override
	public void run() {
		try {
			if (!newestVersion()) {
				Platform.runLater(() -> showAlert());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private Boolean newestVersion() throws IOException {
		doc = Jsoup.connect("https://github.com/flpa/mcs/releases").get();
		Elements versions = doc.getElementsByClass("css-truncate-target");

		newestVersion = versions.get(0).text();
		String version = bundle.getString("project.version");
		// TODO: delete the following Variable, it's just for testing
		// version = "bla";
		int newVersion = parseVersion(newestVersion);
		int thisVersion = parseVersion(version);

		System.out.println("NV: " + newVersion + ": TV: " + thisVersion);

		return newVersion <= thisVersion;
	}

	private int parseVersion(String version) {
		version = version.replaceAll("[^0-9]", "");
		int newVers = Integer.parseInt(version);
		return newVers;
	}

	public void showAlert() {

		Alert alert = new Alert(AlertType.INFORMATION);
		alert.setTitle(bundle.getString("alert.newVersionAvailable.title"));
		alert.setHeaderText(bundle.getString("alert.newVersionAvailable.header"));
		alert.setContentText(bundle.getString("alert.newVersionAvailable.content"));

		Elements infos = doc.getElementsByClass("markdown-body");

		String info = infos.get(0).text();

		TextArea textArea = new TextArea(info);
		textArea.setEditable(false);
		textArea.setWrapText(true);

		textArea.setMaxWidth(Double.MAX_VALUE);
		textArea.setMaxHeight(Double.MAX_VALUE);
		GridPane.setVgrow(textArea, Priority.ALWAYS);
		GridPane.setHgrow(textArea, Priority.ALWAYS);

		GridPane expContent = new GridPane();
		expContent.setMaxWidth(Double.MAX_VALUE);
		expContent.add(textArea, 0, 1);

		// Set expandable Exception into the dialog pane.
		alert.getDialogPane().setExpandableContent(expContent);
		alert.getDialogPane().setExpanded(true);

		alert.showAndWait();
	}

}

@@ -174,16 +174,50 @@
 ments;%0A%0A
+import at.fhtw.mcs.model.Version;%0A
 import j
@@ -701,9 +701,8 @@
 if (
-!
 newe
@@ -712,16 +712,20 @@
 ersion()
+ %3C 0
 ) %7B%0A%09%09%09%09
@@ -840,15 +840,11 @@
 ate 
-Boolean
+int
  new
@@ -1118,77 +1118,11 @@
 %22);%0A
+%0A
 %09%09
-// TODO: delete the following Variable, it's just for testing%0A%09%09// 
 vers
@@ -1132,19 +1132,27 @@
  = %22
-bla
+v0.5.1
 %22;%0A
+%0A
 %09%09
-int
+Version
  new
@@ -1153,37 +1153,36 @@
 on newVersion = 
-parse
+new 
 Version(newestVe
@@ -1195,308 +1195,95 @@
 ;%0A%09%09
-int thisVersion = parseVersion(version);%0A%0A%09%09System.out.println(%22NV: %22 +
+Version thisVersion =
  new
+ 
 Version
- + %22: TV: %22 + thisVersion);%0A%0A%09%09return newVersion %3C= thisVersion;%0A%09%7D%0A%0A%09private int parseVersion(String version) %7B%0A%09%09version = version.replaceAll(%22%5B%5E0-9%5D%22, %22%22);%0A%09%09int newVers = Integer.parseInt(version);%0A%09%09return newVers
+(version);%0A%0A%09%09return thisVersion.compareTo(newVersion)
 ;%0A%09%7D

version compare was rebuild to work with a comparable java class