package net.sf.jailer.ui.util;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StringReader;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Date;
import java.util.UUID;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import net.sf.jailer.JailerVersion;
import net.sf.jailer.ui.Environment;

public class UpdateInfoManager {

	private static final String versionURL = "http://jailer.sourceforge.net/currentVersion.php";
	private static final String downloadURL = "https://sourceforge.net/projects/jailer/files/";
	private static final long CHECK_INTERVALL = 1000L * 60 * 60 * 46;
	private static final long DELAY = 1000L * 10;
	private static final String LAST_TS_FILE = ".lastcuats";
	private static boolean checked = false;

	public static void checkUpdateAvailability(final JComponent ui, final JLabel infoLabel, final String modul) {
		ui.setVisible(false);

		if (checked) {
			return;
		}
		checked = true;
		
		Runnable check = new Runnable() {
			@Override
			public void run() {
				try {
					boolean inIntervall = true;
					File lastTSFile = Environment.newFile(LAST_TS_FILE);
					if (lastTSFile.exists()) {
						ObjectInputStream in = new ObjectInputStream(new FileInputStream(lastTSFile));
						long lastTS = in.readLong();
						in.close();
						if (System.currentTimeMillis() < lastTS + CHECK_INTERVALL) {
							inIntervall = false;
						}
					}
					
					Object uuid = UISettings.restore("uuid");
					if (uuid == null) {
						uuid = UUID.randomUUID().toString();
						UISettings.store("uuid", uuid);
					}
					String content = HttpUtil.get(versionURL
							+ "?jversion=" + URLEncoder.encode(System.getProperty("java.version") + "/" + System.getProperty("java.vm.vendor") + "/" + System.getProperty("java.vm.name") + "/" + System.getProperty("os.name"), "UTF-8")
							+ "&modul=" + URLEncoder.encode(modul, "UTF-8")
							+ "&ts=" + URLEncoder.encode(new Date().toString(), "UTF-8")
							+ "&uuid=" + URLEncoder.encode(uuid.toString(), "UTF-8")
							+ "&version=" + URLEncoder.encode(JailerVersion.VERSION, "UTF-8"));
					BufferedReader in = new BufferedReader(new StringReader(content));
			        String inputLine = in.readLine();
			        in.close();
			        
			        if (inputLine != null && !inputLine.trim().isEmpty() && inIntervall) {
			        	final String[] versions = inputLine.trim().split(",");
			        	for (String version: versions) {
			        		if (version.trim().equals(JailerVersion.VERSION)) {
			        			return;
			        		}
			        	}
			        	if (isValid(versions[0].trim())) {
			        		Thread.sleep(DELAY);
							ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(lastTSFile));
							out.writeLong(System.currentTimeMillis());
							out.close();
							SwingUtilities.invokeLater(new Runnable() {
								@Override
								public void run() {
									infoLabel.setText("Release " + versions[0].trim() + " available");
									ui.setVisible(true);
								}
							});
			        	}
			        }
				} catch (Throwable t) {
					// ignore
				}
			}
		};
		Thread checkThread = new Thread(check);
		checkThread.setDaemon(true);
		checkThread.start();
	}

	private static boolean isValid(String version) {
		return version.matches("\\d+\\.\\d+(\\.\\d+)?");
	}

	public static void download() {
		try {
			URI uri = new URI(downloadURL);
			Desktop.getDesktop().browse(uri);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

@@ -353,32 +353,8 @@
 te;%0D
-%0Aimport java.util.UUID;%0D
 %0A%0D%0Ai
@@ -1728,16 +1728,73 @@
 %09%09uuid =
+ String.valueOf(System.currentTimeMillis() %25 1000000); //
  UUID.ra

smaller UUID