/*globals define*/
/*eslint-env node, browser*/

/**
 * Generated by PluginGenerator 2.20.5 from webgme on Mon Oct 22 2018 15:43:37 GMT-0500 (Central Daylight Time).
 * A plugin that inherits from the PluginBase. To see source code documentation about available
 * properties and methods visit %host%/docs/source/PluginBase.html.
 */
if (typeof define !== 'undefined') {
    define([
        'plugin/PluginConfig',
        'text!./metadata.json',
        'plugin/PluginBase'
    ], function (
        PluginConfig,
        pluginMetadata,
        PluginBase) {
        'use strict';

        pluginMetadata = JSON.parse(pluginMetadata);

        /**
         * Initializes a new instance of InvokerPlugin.
         * @class
         * @augments {PluginBase}
         * @classdesc This class represents the plugin InvokerPlugin.
         * @constructor
         */
        function InvokerPlugin() {
            // Call base class' constructor.
            PluginBase.call(this);
            this.pluginMetadata = pluginMetadata;
        }

        /**
         * Metadata associated with the plugin. Contains id, name, version, description, icon, configStructure etc.
         * This is also available at the instance at this.pluginMetadata.
         * @type {object}
         */
        InvokerPlugin.metadata = pluginMetadata;

        // Prototypical inheritance from PluginBase.
        InvokerPlugin.prototype = Object.create(PluginBase.prototype);
        InvokerPlugin.prototype.constructor = InvokerPlugin;

        /**
         * Main function for the plugin to execute. This will perform the execution.
         * Notes:
         * - Always log with the provided logger.[error,warning,info,debug].
         * - Do NOT put any user interaction logic UI, etc. inside this method.
         * - callback always has to be called even if error happened.
         *
         * @param {function(Error|null, plugin.PluginResult)} callback - the result callback
         */
        InvokerPlugin.prototype.main = function (callback) {
            var self = this,
                config = self.getCurrentConfig(),
                core = self.core,
                root = core.getRoot(self.activeNode);

            if (config.callSelf) {
                core.createNode({relid: 'I0', parent: root, base: self.META.FCO});
                this.invokePlugin('InvokerPlugin', {
                    namespace: config.useNamespace,
                    pluginConfig: {callSelf: false, useNamespace: config.useNamespace}
                }, function (err, result) {
                    if (result.success) {
                        self.result.setSuccess(true);
                        self.createMessage(root, JSON.stringify(Object.keys(self.META)), 'debug');

                        result.messages.forEach(function (message) {
                            self.createMessage(root, message.message, message.severity);
                        });
                        callback(null, self.result);
                    } else {
                        self.result.setSuccess(false);
                        callback(null, self.result);
                    }
                });
            } else {
                core.createNode({relid: 'I', parent: root, base: self.META.FCO});
                this.invokePlugin('InvokedPlugin', {
                    namespace: config.useNamespace,
                    pluginConfig: {doCheck: true}
                }, function (err, result) {
                    if (result.success) {
                        self.result.setSuccess(true);
                        self.createMessage(root, JSON.stringify(Object.keys(self.META)), 'debug');

                        result.messages.forEach(function (message) {
                            self.createMessage(root, message.message, message.severity);
                        });
                        callback(null, self.result);
                    } else {
                        self.result.setSuccess(false);
                        callback(null, self.result);
                    }

                });
            }


        };

        return InvokerPlugin;
    });
}