package sh.komet.fx.stage;

import de.codecentric.centerdevice.javafxsvg.SvgImageLoaderFactory;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.swing.ImageIcon;
import org.apache.commons.lang3.SystemUtils;
import sh.isaac.api.LookupService;
import static sh.isaac.api.constants.Constants.DATA_STORE_ROOT_LOCATION_PROPERTY;
import sh.isaac.komet.iconography.Iconography;
import static sh.isaac.api.constants.Constants.USER_CSS_LOCATION_PROPERTY;

public class MainApp extends Application {
// TODO add TaskProgressView
// http://dlsc.com/2014/10/13/new-custom-control-taskprogressview/
// http://fxexperience.com/controlsfx/features/   
    public static final String SPLASH_IMAGE =
            "prism-splash.png";

   @Override
   public void start(Stage stage) throws Exception {
      //TODO have SvgImageLoaderFactory autoinstall as part of a HK2 service. 
      SvgImageLoaderFactory.install();
            

      if (Files.exists(Paths.get("target", "data", "meta-db.data"))) {
         System.setProperty(DATA_STORE_ROOT_LOCATION_PROPERTY, "target/data/meta-db.data");
      } else if (Files.exists(Paths.get("target", "data", "solor-db.data"))) {
         System.setProperty(DATA_STORE_ROOT_LOCATION_PROPERTY, "target/data/solor-db.data");
      } else if (Files.exists(Paths.get("meta-db.data"))) {
         System.setProperty(DATA_STORE_ROOT_LOCATION_PROPERTY, "solor-db.data");
      } else if (Files.exists(Paths.get("solor-db.data"))) {
         System.setProperty(DATA_STORE_ROOT_LOCATION_PROPERTY, "meta-db.data");
      } else {
         throw new UnsupportedOperationException("Can't find data directory... Working dir: " + System.getProperty("user.dir"));
      }

      if (Files.exists(Paths.get("target", "data", "user.css"))) {
         System.setProperty(USER_CSS_LOCATION_PROPERTY, Paths.get("target", "data", "user.css").toUri().toURL().toString());

      } else if (Files.exists(Paths.get("user.css"))) {
         System.setProperty(USER_CSS_LOCATION_PROPERTY, Paths.get("user.css").toUri().toURL().toString());
      } else {
         throw new UnsupportedOperationException("Can't find user.css file... Working dir: " + System.getProperty("user.dir"));
      }

      LookupService.startupIsaac();
      Parent root = FXMLLoader.load(getClass().getResource("/fxml/KometStageScene.fxml"));

      Scene scene = new Scene(root);
      //GraphController.setSceneForControllers(scene);
      scene.getStylesheets().add(System.getProperty(USER_CSS_LOCATION_PROPERTY));
      scene.getStylesheets().add(Iconography.getStyleSheetStringUrl());

      // SNAPSHOT
      // Chronology
      // Reflector
      // 
      // Logic, Language, Dialect, Chronology, 
      // LILAC Reflector (LOGIC, 
      // COLLD Reflector: Chronology of Logic, Language, and Dialect : COLLAD
      // COLLDAE Chronology of Logic, Langugage, Dialect, and Extension
      // CHILLDE
      // Knowledge, Language, Dialect, Chronology
      // KOLDAC
      stage.setTitle("KOMET Reflector");
      stage.setScene(scene);
      stage.show();

      //ScenicView.show(scene);
   }

   /**
    * The main() method is ignored in correctly deployed JavaFX application. main() serves only as fallback in case the
    * application can not be launched through deployment artifacts, e.g., in IDEs with limited FX support. NetBeans
    * ignores main().
    *
    * @param args the command line arguments
    */
   public static void main(String[] args) {
      launch(args);
   }

}

@@ -93,29 +93,8 @@
 ry;%0A
-import java.net.URL;%0A
 impo
@@ -354,83 +354,8 @@
 ge;%0A
-import javax.swing.ImageIcon;%0Aimport org.apache.commons.lang3.SystemUtils;%0A
 impo
@@ -1432,16 +1432,24 @@
 ths.get(
+%22data%22, 
 %22meta-db
@@ -1524,21 +1524,320 @@
 PERTY, %22
-solor
+data/meta-db.data%22);%0A      %7D else if (Files.exists(Paths.get(%22data%22, %22solor-db.data%22))) %7B%0A         System.setProperty(DATA_STORE_ROOT_LOCATION_PROPERTY, %22data/solor-db.data%22);%0A      %7D else if (Files.exists(Paths.get(%22meta-db.data%22))) %7B%0A         System.setProperty(DATA_STORE_ROOT_LOCATION_PROPERTY, %22meta
 -db.data
@@ -1957,36 +1957,37 @@
 TION_PROPERTY, %22
-meta
+solor
 -db.data%22);%0A    
@@ -2324,24 +2324,204 @@
 String());%0A%0A
+      %7D else if (Files.exists(Paths.get(%22data%22, %22user.css%22))) %7B%0A         System.setProperty(USER_CSS_LOCATION_PROPERTY, Paths.get(%22data%22, %22user.css%22).toUri().toURL().toString());%0A%0A
       %7D else

update directories to search for database and user.css location.