package net.hearthstats;

import net.hearthstats.config.Environment;
import net.hearthstats.config.OS;
import net.hearthstats.log.Log;
import net.hearthstats.updater.api.GitHubReleases;
import net.hearthstats.updater.api.model.Release;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.io.File;
import java.net.URI;


public final class Updater {
	private Updater() {} // never instantiated

  public static final String MANUAL_DOWNLOAD_URL = "http://hearthstats.net/uploader";

  private final static Logger debugLog = LoggerFactory.getLogger(Updater.class);

  private static Release cachedlatestRelease;


  public static Release getLatestRelease() {
    if (cachedlatestRelease == null) {
      try {
        debugLog.debug("Loading latest release information from GitHub");
        if (Config.os == OS.OSX) {
          cachedlatestRelease = GitHubReleases.getLatestReleaseForOSX();
        } else if (Config.os == OS.WINDOWS) {
          cachedlatestRelease = GitHubReleases.getLatestReleaseForWindows();
        }
        if (cachedlatestRelease == null) {
          Log.warn("Unable to check latest release of HearthStats Uploader");
        }
      } catch (Exception e) {
        Log.warn("Unable to check latest release of HearthStats Uploader due to error: " + e.getMessage(), e);
      }
    }
    return cachedlatestRelease;
  }


  public static void run(Environment environment, Release release) {
    Log.info("Extracting and running updater ...");

    String errorMessage = null;
    try {
      errorMessage = environment.performApplicationUpdate(release);
    } catch (Exception e) {
      debugLog.warn("Unable to run updater", e);
      if (errorMessage == null) {
        errorMessage = e.getMessage();
      }
    }

    if (errorMessage == null) {
      // There was no error, so the updater should be running now
      System.exit(0);

    } else {
      // There was an error
      Main.showMessageDialog(null, errorMessage + "\n\nYou will now be taken to the download page.");
      try {
        Desktop.getDesktop().browse(new URI(MANUAL_DOWNLOAD_URL));
      } catch (Exception e) {
        Main.showErrorDialog("Error launching browser with URL " + MANUAL_DOWNLOAD_URL, e);
      }
      Log.warn("Updater Error: " + errorMessage);
    }


  }


  public static void cleanUp() {
		removeFile(Config.getExtractionFolder() + "/updater.jar");
		removeFile(Config.getExtractionFolder() + "/update.zip");
	}


	private static void removeFile(String path) {
		File file = new File(path);
		if (file.isFile()) {
			file.delete();
		}
	}

}

@@ -229,16 +229,76 @@
 elease;%0A
+import org.apache.commons.io.filefilter.WildcardFileFilter;%0A
 import o
@@ -387,16 +387,43 @@
 o.File;%0A
+import java.io.FileFilter;%0A
 import j
@@ -2430,18 +2430,20 @@
 nUp() %7B%0A
-%09%09
+    
 removeFi
@@ -2469,29 +2469,27 @@
 tionFolder()
- +
+,
  %22
-/
 updater.jar%22
@@ -2491,18 +2491,20 @@
 .jar%22);%0A
-%09%09
+    
 removeFi
@@ -2538,19 +2538,19 @@
 er()
- +
+,
  %22
-/
 update
+-*
 .zip
@@ -2562,111 +2562,566 @@
 %7D%0A%0A%0A
-%09private static void removeFile(String path) %7B%0A%09%09File file = new File(path);%0A%09%09if (file.
+  /**%0A   * Deletes a file or files in the given path.%0A   * @param path The path where the file or files are located.%0A   * @param filename The file or files to delete. Accepts wildcards * and ?.%0A   */%0A%09private static void removeFile(String path, String filename) %7B%0A    debugLog.debug(%22Deleting %7B%7D/%7B%7D%22, path, filename);%0A    File dir = new File(path);%0A    FileFilter fileFilter = new WildcardFileFilter(filename);%0A    File%5B%5D files = dir.l
 is
+t
 File
-()) %7B%0A%09%09%09
+s(fileFilter);%0A    for (File file : files) %7B%0A      debugLog.info(%22Deleting file %7B%7D%22, file.getAbsolutePath());%0A      
 file
@@ -3135,10 +3135,12 @@
 ();%0A
-%09%09
+    
 %7D%0A%09%7D

Update files downloaded by the updater are now deleted on startup.