/**
 * Created by jklingler on 4/1/2014.
 */

define(['plugin/PluginConfig',
        'plugin/PluginBase',
        'plugin/ImportFMUs/ImportFMUs/FMU'],
    function (PluginConfig, PluginBase, FMU) {
    'use strict';

    var ImportFMUsPlugin = function () {
        // Call base class's constructor
        PluginBase.call(this);
    };

    ImportFMUsPlugin.prototype = Object.create(PluginBase.prototype);

    ImportFMUsPlugin.prototype.constructor = ImportFMUsPlugin;

    ImportFMUsPlugin.prototype.getName = function () {
        return "ImportFMUs";
    };

    ImportFMUsPlugin.getDefaultConfig = function () {
        return new PluginConfig();
    };

    ImportFMUsPlugin.prototype.main = function (callback) {
        var self = this,
            core = this.core,
            rootNode = this.rootNode;

        // FIXME: this is a hack to get intellisense
//        var name;
//        for (name in config.META) {
//            if (config.META.hasOwnProperty(name)) {
//                FMU[name] = config.META[name];
//            }
//        }

        this.updateMETA(FMU)

        var newFmuLib = core.createNode({parent: rootNode, base: FMU.FMU_Library});
        core.setAttribute(newFmuLib, 'name', 'NewImportedFMUs');

        // Commit changes.
        core.persist(rootNode, function (err) {
        });

        var newRootHash = core.getHash(rootNode);
        console.info(config.project.makeCommit);

        var result = {'commitHash': config.commitHash};
        result.commitHash = config.project.makeCommit([result.commitHash], newRootHash, 'Plugin updated the model.', function (err) {});

        // End Commit

//        if (selectedNode && typeof selectedNode == FMU.FMU_Library) {
        if (selectedNode) {

            core.loadChildren(selectedNode, function (err, childNodes) {
                var i;
                console.log('%j has children::', core.getAttribute(selectedNode, 'name'));

                for (i = 0; i < childNodes.length; i += 1) {
                    console.log('  - %j', core.getAttribute(childNodes[i], 'name'));
                }

                if (callback) {
                    callback(null, {'success': true});
                }
            });
        } else {
            callback('selectedNode is not defined', {'success': false});
        }
    };

    return ImportFMUsPlugin;
});
@@ -1271,355 +1271,333 @@
     
-core.persist(rootNode, function (err) %7B%0A        %7D);%0A%0A        var newRootHash = core.getHash(rootNode);%0A        console.info(config.project.makeCommit);%0A%0A        var result = %7B'commitHash': config.commitHash%7D;%0A        result.commitHash = config.project.makeCommit(%5Bresult.commitHash%5D, newRootHash, 'Plugin updated the model.', function (err) %7B
+this.save('Committing changes to database for ImportFMUs plugin', function (err) %7B%0A            // FIXME: on error?%0A%0A            if (callback) %7B%0A                // TODO: we need a function to set/update success%0A                self.result.success = true;%0A                callback(null, self.result);%0A            %7D%0A        
 %7D);%0A
-%0A
     
@@ -1695,32 +1695,28 @@
         if (
-selected
+root
 Node) %7B%0A%0A   
@@ -1742,24 +1742,20 @@
 hildren(
-selected
+root
 Node, fu
@@ -1871,24 +1871,20 @@
 tribute(
-selected
+root
 Node, 'n
@@ -2217,24 +2217,20 @@
 llback('
-selected
+root
 Node is 

change method of committing changes...