/**
 * Copyright (c) 2014 Richard Warburton (richard.warburton@gmail.com)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 **/
package com.insightfullogic.honest_profiler.ports.javafx.landing;

import com.insightfullogic.honest_profiler.core.Monitor;
import com.insightfullogic.honest_profiler.core.MachineListener;
import com.insightfullogic.honest_profiler.core.ProfileListener;
import com.insightfullogic.honest_profiler.core.sources.VirtualMachine;
import com.insightfullogic.honest_profiler.ports.javafx.WindowViewModel;
import com.insightfullogic.honest_profiler.ports.sources.FileLogSource;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;

import static com.insightfullogic.honest_profiler.ports.javafx.WindowViewModel.Window.Profile;

public class LandingViewModel implements MachineListener {

    @FXML
    private VBox landingView;

    @FXML
    private Button monitorButton;

    private final ToggleGroup toggleMachines;
    private final Logger logger;
    private final Monitor monitor;
    private final WindowViewModel windowModel;
    private final ProfileListener profileListener;

    public LandingViewModel(
            final Logger logger,
            final Monitor monitor,
            final WindowViewModel windowModel,
            final ProfileListener profileListener) {
        this.logger = logger;
        this.monitor = monitor;
        this.windowModel = windowModel;
        this.profileListener = profileListener;
        toggleMachines = new ToggleGroup();
    }

    @FXML
    private void initialize() {
        toggleMachines.selectedToggleProperty()
                      .addListener((of, from, to) -> {
                          monitorButton.setDisable(to == null);
                      });
    }

    public void open(ActionEvent actionEvent) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open a log file");
        File file = fileChooser.showOpenDialog(null);
        if (file != null) {
            windowModel.display(Profile);
            try {
                monitor.consumeFile(new FileLogSource(file), profileListener);
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    public void monitorButton(ActionEvent actionEvent) {
        windowModel.display(Profile);
        MachineButton selectedButton = (MachineButton) toggleMachines.getSelectedToggle();
        VirtualMachine vm = selectedButton.getJvm();
        try {
            monitor.pipeFile(vm.getLogSource(), profileListener);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }

    @Override
    public void onNewMachine(VirtualMachine machine) {
        Platform.runLater(() -> {
            ObservableList<Node> children = landingView.getChildren();
            MachineButton button = new MachineButton(machine);
            button.setToggleGroup(toggleMachines);
            children.add(button);
        });
    }

    @Override
    public void onClosedMachine(VirtualMachine machine) {
        Platform.runLater(() -> {
            String id = machine.getId();
            ObservableList<Node> children = landingView.getChildren();
            children.removeIf(node -> {
                return id.equals(node.getId());
            });
        });
    }

}

@@ -1274,21 +1274,29 @@
 r.core.M
-onito
+achineListene
 r;%0Aimpor
@@ -1335,37 +1335,29 @@
 filer.core.M
-achineListene
+onito
 r;%0Aimport co
@@ -2008,16 +2008,64 @@
 ception;
+%0Aimport java.util.HashSet;%0Aimport java.util.Set;
 %0A%0Aimport
@@ -2511,16 +2511,64 @@
 istener;
+%0A    private final Set%3CVirtualMachine%3E machines;
 %0A%0A    pu
@@ -2948,24 +2948,60 @@
 gleGroup();%0A
+        machines = new HashSet%3C%3E();%0A
     %7D%0A%0A    @
@@ -3033,24 +3033,79 @@
 tialize() %7B%0A
+        logger.debug(%22Initializing LandingViewModel%22);%0A
         togg
@@ -3277,32 +3277,80 @@
             %7D);%0A
+        machines.forEach(this::displayMachine);%0A
     %7D%0A%0A    publi
@@ -3361,16 +3361,22 @@
 id open(
+final 
 ActionEv
@@ -3865,16 +3865,22 @@
 rButton(
+final 
 ActionEv
@@ -4291,32 +4291,38 @@
 id onNewMachine(
+final 
 VirtualMachine m
@@ -4343,38 +4343,227 @@
     
-Platform.runLater(() -%3E %7B%0A    
+logger.debug(%22New machine: %7B%7D%22, machine.getDisplayName());%0A        machines.add(machine);%0A        Platform.runLater(() -%3E displayMachine(machine));%0A    %7D%0A%0A    private void displayMachine(final VirtualMachine machine) %7B%0A
     
@@ -4629,28 +4629,24 @@
 ();%0A        
-  
-  
 MachineButto
@@ -4692,20 +4692,16 @@
         
-    
 button.s
@@ -4731,36 +4731,32 @@
 hines);%0A        
-   
- 
 children.add(but
@@ -4761,28 +4761,16 @@
 utton);%0A
-        %7D);%0A
     %7D%0A%0A 
@@ -4814,16 +4814,22 @@
 Machine(
+final 
 VirtualM
@@ -4838,32 +4838,94 @@
 hine machine) %7B%0A
+        logger.debug(%22Closed: %7B%7D%22, machine.getDisplayName());%0A
         Platform
@@ -5095,33 +5095,8 @@
 e -%3E
- %7B%0A                return
  id.
@@ -5115,31 +5115,16 @@
 getId())
-;%0A            %7D
 );%0A     

fix bug where the monitored JVMs disappear when you go back to the landing view