package gvs.ui.logic.app;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import gvs.interfaces.IPersistor;
import gvs.interfaces.ISessionController;
import gvs.ui.application.controller.ApplicationController;
import gvs.ui.application.model.ApplicationModel;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 * The ViewModel class for the GVS Application. Corresponds to the classical
 * ViewModel of the MVVM Pattern. It observs the ApplicationModel.
 * 
 * @author muriele
 *
 */
public class AppViewModel implements Observer {

  private ApplicationModel appModel;
  private ApplicationController appController;
  private IPersistor persistor;

  private final ObservableList<String> sessionControllers = FXCollections
      .observableArrayList();
  private StringProperty currentSessionName = new SimpleStringProperty();
  private final Map<String, ISessionController> controllerMap = new HashMap<>();

  //TODO: do we still need the persistor here? @mtrentini
  public AppViewModel(ApplicationModel appModel,
      ApplicationController appController, IPersistor persistor) {
    this.appModel = appModel;
    this.appModel.addObserver(this);
    this.appController = appController;
    this.persistor = persistor;
    currentSessionName.setValue("no active session");
  }

  public ObservableList<String> getSessionControllers() {
    return sessionControllers;
  }

  public StringProperty getCurrentSessionName() {
    return currentSessionName;
  }

  /**
   * This method is invoked whenever a new current session is set in the
   * ApplicationModel.
   */
  @Override
  public void update(Observable o, Object arg) {
    ISessionController c = ((ApplicationModel) o).getSession();
    String name = c.getSessionName();
    controllerMap.put(name, c);
    sessionControllers.add(name);
    Platform.runLater(() -> currentSessionName.set(name));
  }

  // TODO: still shows session in dropdown
  public void removeCurrentSession() {
    ISessionController currentSession = appModel.getSession();
    appController.deleteSession(currentSession);
    sessionControllers.remove(currentSession.getSessionName());
    controllerMap.remove(currentSession.getSessionName());
  }

  public void loadSession(File file) {
    if (file != null) {
      appController.setRequestedFile(file.getPath(), persistor);
    }
  }

  public void saveSession() {
    appModel.getSession().saveSession();
  }

  public void changeSession(String name) {
    ISessionController c = controllerMap.get(name);
    appController.changeCurrentSession(c);
  }

}

@@ -146,16 +146,74 @@
 erver;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import g
@@ -238,16 +238,16 @@
 sistor;%0A
-
 import g
@@ -332,32 +332,85 @@
 tionController;%0A
+import gvs.ui.application.controller.GVSApplication;%0A
 import gvs.ui.ap
@@ -1136,16 +1136,107 @@
 rsistor;
+%0A  private static final Logger logger = LoggerFactory%0A      .getLogger(AppViewModel.class);
 %0A%0A  priv
@@ -1493,16 +1493,17 @@
 );%0A%0A  //
+ 
 TODO: do
@@ -2271,24 +2271,102 @@
 tSession();%0A
+    if (c != null) %7B%0A      logger.debug(%22Set new current session in GUI.%22);%0A  
     String n
@@ -2391,24 +2391,26 @@
 Name();%0A    
+  
 controllerMa
@@ -2433,96 +2433,290 @@
     
-sessionControllers.add(name);%0A    Platform.runLater(() -%3E currentSessionName.set(name));
+  if (!sessionControllers.contains(name)) %7B%0A        sessionControllers.add(name);%0A      %7D%0A      //TODO: change selected item in combobox%0A      Platform.runLater(() -%3E currentSessionName.set(name));%0A    %7D else %7B%0A      logger.warn(%22ApplicationModel holds no current session.%22);%0A    %7D%0A
 %0A  %7D

add logger and fix NullPointer Exception