package codeOrchestra.colt.core.ui;

import codeOrchestra.colt.core.COLTException;
import codeOrchestra.colt.core.COLTProjectManager;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;

/**
 * @author Alexander Eliseyev
 */
public class COLTApplication extends Application {

    public static COLTApplication instance;

    private VBox root;
    private Node currentPluginNode;

    @Override
    public void start(Stage primaryStage) throws Exception {
        instance = this;

        root = new VBox();
        root.setFillWidth(true);
        root.setMaxHeight(Double.MAX_VALUE);
        primaryStage.setTitle("COLT 1.1");
        primaryStage.setScene(new Scene(root, 800, 700));

        Menu menu = new Menu("File");
        MenuItem menuLoad = new MenuItem("Load");
        menuLoad.setOnAction(t -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("COLT", "*.colt2"));
            File file = fileChooser.showOpenDialog(primaryStage.getScene().getWindow());
            if (file != null) {
                try {
                    COLTProjectManager.getInstance().load(file.getPath());
                } catch (COLTException e) {
                    throw new RuntimeException(e); // TODO: handle nicely
                }
            }
        });

        MenuItem menuSave = new MenuItem("Save");
        menuSave.setOnAction(t -> {

        });

        menu.getItems().addAll(menuLoad, menuSave);

        MenuBar menuBar = new MenuBar();
        menuBar.getMenus().addAll(menu);
        menuBar.setUseSystemMenuBar(true);

        root.getChildren().add(menuBar);

        primaryStage.show();
    }

    public void setPluginPane(Node node) {
        if (currentPluginNode != null) {
            root.getChildren().remove(currentPluginNode);
        }

        currentPluginNode = node;
        VBox.setVgrow(currentPluginNode, Priority.ALWAYS);
        root.getChildren().add(node);
    }

    public static void main(String[] args) {
        launch(args);
    }

}

@@ -123,24 +123,74 @@
 ectManager;%0A
+import codeOrchestra.colt.core.model.COLTProject;%0A
 import javaf
@@ -212,24 +212,24 @@
 pplication;%0A
-
 import javaf
@@ -595,16 +595,71 @@
 io.File;
+%0Aimport java.io.FileWriter;%0Aimport java.io.IOException;
 %0A%0A/**%0A *
@@ -1906,16 +1906,796 @@
 (t -%3E %7B%0A
+            COLTProject project = COLTProjectManager.getInstance().getCurrentProject();%0A            if (project != null) %7B%0A                String xml = project.toXmlString();%0A                FileChooser fileChooser = new FileChooser();%0A                fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(%22COLT%22, %22*.colt2%22));%0A                File file = fileChooser.showSaveDialog(primaryStage);%0A                if (file != null) %7B%0A                    try %7B%0A                        FileWriter fileWriter = new FileWriter(file);%0A                        fileWriter.write(xml);%0A                        fileWriter.close();%0A                    %7D catch (IOException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A                %7D%0A            %7D
 %0A       

add save logic