package gui;

import java.util.ArrayList;

import encounter.Encounter;
import entity.Creature;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class MainGUI extends Application {

    public static final String IMAGEFOLDER = System.getProperty("user.dir") + "\\images\\";

    public static ArrayList<Creature> creatureList;
    public static Encounter encounter;
    public static Stage mainStage;

    private static Thread autoSaveThread;
    private static final int AUTOSAVEINTERVAL = 60000;
    private static boolean autosave = true;

    public static void main(String[] args) {
	creatureList = new ArrayList<Creature>();
	launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
	Parent root = FXMLLoader.load(getClass().getResource("gui.fxml"));
	Scene scene = new Scene(root, 1280, 720);
	scene.getStylesheets().add(getClass().getResource("customstyle.css").toString());
	mainStage = primaryStage;
	autoSaveThread = new Thread(new Runnable() {
	    @Override
	    public void run() {
		try {
		    Thread.sleep(5000);
		} catch (InterruptedException e1) {
		    e1.printStackTrace();
		}
		while (autosave) {
		    if (encounter.getCreatureList().size() > 0) {
			encounter.autoSave();
			System.out.println("AutoSave Complete");
		    }
		    try {
			Thread.sleep(AUTOSAVEINTERVAL);
		    } catch (InterruptedException e) {
			e.printStackTrace();
		    }
		}
	    }
	});
	autoSaveThread.setDaemon(true);
	autoSaveThread.start();

	primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("icon.png")));
	primaryStage.setTitle("D&D Encounter Tool v2.0");
	primaryStage.setScene(scene);
	primaryStage.show();
    }
}

@@ -134,16 +134,51 @@
 ation;%0D%0A
+import javafx.event.EventHandler;%0D%0A
 import j
@@ -197,24 +197,24 @@
 XMLLoader;%0D%0A
-
 import javaf
@@ -319,16 +319,50 @@
 .Stage;%0D
+%0Aimport javafx.stage.WindowEvent;%0D
 %0A%0D%0Apubli
@@ -762,16 +762,22 @@
 autosave
+Thread
  = true;
@@ -777,16 +777,61 @@
 = true;%0D
+%0A    public static boolean autosave = false;%0D
 %0A%0D%0A    p
@@ -1214,16 +1214,18 @@
 ng());%0D%0A
+%0D%0A
 %09mainSta
@@ -1244,16 +1244,18 @@
 Stage;%0D%0A
+%0D%0A
 %09autoSav
@@ -1341,117 +1341,40 @@
 %0D%0A%09%09
-try %7B%0D%0A%09%09    Thread.sleep(5000);%0D%0A%09%09%7D catch (InterruptedException e1) %7B%0D%0A%09%09    e1.printStackTrace();%0D%0A%09%09%7D%0D%0A%09%09
+while (autosaveThread) %7B%0D%0A%09%09    
 whil
@@ -1391,20 +1391,17 @@
 e) %7B%0D%0A%09%09
-    
+%09
 if (enco
@@ -1442,16 +1442,20 @@
 ) %7B%0D%0A%09%09%09
+    
 encounte
@@ -1472,16 +1472,20 @@
 ();%0D%0A%09%09%09
+    
 System.o
@@ -1514,24 +1514,30 @@
 omplete%22);%0D%0A
+%09%09%09%7D%0D%0A
 %09%09    %7D%0D%0A%09%09 
@@ -1739,16 +1739,186 @@
 t();%0D%0A%0D%0A
+%09primaryStage.setOnCloseRequest(new EventHandler%3CWindowEvent%3E() %7B%0D%0A%09    @Override%0D%0A%09    public void handle(WindowEvent event) %7B%0D%0A%09%09autosaveThread = false;%0D%0A%09    %7D%0D%0A%09%7D);%0D%0A
 %09primary

add window close handler