package de.prob2.ui.eclipse;

import java.io.FileNotFoundException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import de.prob.cli.CliVersionNumber;
import de.prob.scripting.Api;
import de.prob.servlet.Main;

public class VersionController {

	private static volatile boolean checked = false;

	private void check() {
		if (!"false".equals(System.getProperty("checkVersion"))) {
			final Api api = Main.getInjector().getInstance(Api.class);
			final CliVersionNumber version = api.getVersion();

			if (version == null) {
				final Display display = Display.getDefault();
				final Shell shell = display.getActiveShell();
				final String dialogMessage = "You have no ProB binaries installed in your home directory."
						+ " Press \"Ok\" to download a compatible version.\n"
						+ "Make sure that you have a working internet connection.\n";
				final MessageDialog popup = new MessageDialog(shell,
						"Download ProB Binaries", null, dialogMessage,
						MessageDialog.WARNING, new String[] { "Ok", "Cancel" },
						0);
				final int result = popup.open();
				if (result == 0) {
					api.upgrade("milestone-25");
				}
			} else if (!version.revision
					.equals("28f6135804e7218347092f146d37cdd492e95eca")) {
				final Display display = Display.getDefault();
				final Shell shell = display.getActiveShell();
				final String dialogMessage = "The ProB binary in your home directory may not be compatible with this version of the ProB 2.0 Plug-in."
						+ " Press \"Ok\" to download a compatible version.\n"
						+ "Make sure that you have a working internet connection.\n"
						+ "If you press \"Cancel\" we cannot guarantee that the plug-in will work correctly.";
				final MessageDialog popup = new MessageDialog(shell,
						"Download ProB Binaries", null, dialogMessage,
						MessageDialog.WARNING, new String[] { "Ok", "Cancel" },
						0);
				final int result = popup.open();
				if (result == 0) {
					try {
						api.upgrade("milestone-25");
					} catch (final Exception e) {
						if (e instanceof FileNotFoundException) {
							final MessageDialog popup2 = new MessageDialog(
									shell,
									"Download ProB Binaries",
									null,
									"The download failed because your ProB binary is in use."
											+ " We will try to load your machine with your current binary.",
											MessageDialog.ERROR, new String[] { "Ok" },
											0);
							popup2.open();
						}
					}
				}
			}
		}
	}

	public static synchronized void ensureInstalled() {
		if (checked) {
			return;
		}
		checked = true;

		final VersionController c = new VersionController();
		c.check();

	}

}

@@ -1178,36 +1178,38 @@
 pi.upgrade(%22
-milestone-25
+2.0.0-snapshot
 %22);%0A%09%09%09%09%7D%0A%09%09
@@ -1256,48 +1256,48 @@
 ls(%22
-28f6135804e7218347092f146d37cdd492e95eca
+fbaa6d5c9323581aa64ba9e2526808ff27040922
 %22)) 
@@ -2033,20 +2033,22 @@
 de(%22
-milestone-25
+2.0.0-snapshot
 %22);%0A

bump CLI version for prob2-plugin