/*
 * This file is part of Bitsquare.
 *
 * Bitsquare is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bitsquare is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
 */

package io.bitsquare;

import io.bitsquare.di.BitsquareModule;
import io.bitsquare.gui.AWTSystemTray;
import io.bitsquare.gui.Navigation;
import io.bitsquare.gui.components.Popups;
import io.bitsquare.gui.util.ImageUtil;
import io.bitsquare.persistence.Persistence;
import io.bitsquare.settings.Settings;
import io.bitsquare.user.User;
import io.bitsquare.util.ViewLoader;

import com.google.common.base.Throwables;

import com.google.inject.Guice;
import com.google.inject.Injector;

import java.io.IOException;

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.stage.Stage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import akka.actor.ActorSystem;
import lighthouse.files.AppDirectory;

public class BitsquareUI extends Application {
    private static final Logger log = LoggerFactory.getLogger(BitsquareUI.class);

    private static Stage primaryStage;

    private final BitsquareModule bitsquareModule;
    private final Injector injector;

    public BitsquareUI() {
        this.bitsquareModule = new BitsquareModule();
        this.injector = Guice.createInjector(bitsquareModule);
    }

    public static Stage getPrimaryStage() {
        return primaryStage;
    }

    @Override
    public void start(Stage primaryStage) {
        BitsquareUI.primaryStage = primaryStage;

        Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> Popups.handleUncaughtExceptions
                (Throwables.getRootCause(throwable)));

        try {
            AppDirectory.initAppDir(Bitsquare.getAppName());
        } catch (IOException e) {
            log.error(e.getMessage());
        }


        // currently there is not SystemTray support for java fx (planned for version 3) so we use the old AWT
        AWTSystemTray.createSystemTray(primaryStage, injector.getInstance(ActorSystem.class), this);

        // apply stored data
        User user = injector.getInstance(User.class);
        Settings settings = injector.getInstance(Settings.class);
        Persistence persistence = injector.getInstance(Persistence.class);
        persistence.init();

        User persistedUser = (User) persistence.read(user);
        user.applyPersistedUser(persistedUser);

        settings.applyPersistedSettings((Settings) persistence.read(settings.getClass().getName()));

        primaryStage.setTitle("Bitsquare (" + Bitsquare.getAppName() + ")");

        // sometimes there is a rendering bug, see https://github.com/bitsquare/bitsquare/issues/160
        if (ImageUtil.isRetina())
            primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/images/window_icon@2x.png")));
        else
            primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/images/window_icon.png")));

        ViewLoader.setInjector(injector);

        ViewLoader loader = new ViewLoader(Navigation.Item.MAIN, false);
        Parent view = loader.load();

        Scene scene = new Scene(view, 1000, 600);
        scene.getStylesheets().setAll(
               "/io/bitsquare/gui/bitsquare.css",
               "/io/bitsquare/gui/images.css");

        setupCloseHandlers(primaryStage, scene);

        primaryStage.setScene(scene);

        primaryStage.setMinWidth(75);
        primaryStage.setMinHeight(50);

        primaryStage.show();
    }

    private void setupCloseHandlers(Stage primaryStage, Scene scene) {
        primaryStage.setOnCloseRequest(e -> AWTSystemTray.setStageHidden());

        KeyCodeCombination keyCodeCombination = new KeyCodeCombination(KeyCode.W, KeyCombination.SHORTCUT_DOWN);
        scene.setOnKeyReleased(keyEvent -> {
            if (keyCodeCombination.match(keyEvent))
                AWTSystemTray.setStageHidden();
        });
    }

    @Override
    public void stop() {
        bitsquareModule.close(injector);
        System.exit(0);
    }
}

@@ -2079,24 +2079,87 @@
 maryStage;%0A%0A
+%0A        // route uncaught exceptions to a user-facing dialog%0A%0A
         Thre
@@ -2227,16 +2227,32 @@
 able) -%3E
+%0A               
  Popups.
@@ -2275,33 +2275,16 @@
 ceptions
-%0A                
 (Throwab
@@ -2311,24 +2311,88 @@
 owable)));%0A%0A
+%0A        // configure the Bitsquare application data directory%0A%0A
         try 
@@ -2554,238 +2554,43 @@
  // 
-currently there is not SystemTray support for java fx (planned for version 3) so we use the old AWT%0A        AWTSystemTray.createSystemTray(primaryStage, injector.getInstance(ActorSystem.class), this);%0A%0A        // apply stored data
+load and apply any stored settings%0A
 %0A   
@@ -3025,458 +3025,64 @@
 );%0A%0A
-        primaryStage.setTitle(%22Bitsquare (%22 + Bitsquare.getAppName() + %22)%22);%0A%0A        // sometimes there is a rendering bug, see https://github.com/bitsquare/bitsquare/issues/160%0A        if (ImageUtil.isRetina())%0A            primaryStage.getIcons().add(new Image(getClass().getResourceAsStream(%22/images/window_icon@2x.png%22)));%0A        else%0A            primaryStage.getIcons().add(new Image(getClass().getResourceAsStream(%22/images/window_icon.png%22)));
+%0A        // load the main view and create the main scene
 %0A%0A  
@@ -3117,25 +3117,24 @@
 (injector);%0A
-%0A
         View
@@ -3420,24 +3420,25 @@
 css%22);%0A%0A
+%0A
         
 setupClo
@@ -3433,273 +3433,138 @@
     
-setupCloseHandlers(primaryStage, scene);%0A%0A        primaryStage.setScene(scene);%0A%0A        primaryStage.setMinWidth(75);%0A        primaryStage.setMinHeight(50);%0A%0A        primaryStage.show();%0A    %7D%0A%0A    private void setupCloseHandlers(Stage primaryStage, Scene scene) %7B
+// configure the system tray%0A%0A        AWTSystemTray.createSystemTray(primaryStage, injector.getInstance(ActorSystem.class), this);
 %0A   
@@ -3637,17 +3637,16 @@
 den());%0A
-%0A
         
@@ -3907,16 +3907,491 @@
     %7D);%0A
+%0A%0A        // configure the primary stage%0A%0A        primaryStage.setTitle(%22Bitsquare (%22 + Bitsquare.getAppName() + %22)%22);%0A        primaryStage.setScene(scene);%0A        primaryStage.setMinWidth(75);%0A        primaryStage.setMinHeight(50);%0A        primaryStage.getIcons().add(new Image(getClass().getResourceAsStream(%0A                ImageUtil.isRetina() ? %22/images/window_icon@2x.png%22 : %22/images/window_icon.png%22)));%0A%0A%0A        // make the UI visible%0A%0A        primaryStage.show();%0A
     %7D%0A%0A 

Add comment for each major step in BitsquareUI#start