/// class PD.Manage.Modules.ObjectModel.ProcessNodeUpdateCommand

PD.Manage.Modules.ObjectModel.ProcessNodeUpdateCommand =
PD.Commands.Command.extend({

    //===================================
    // PUBLIC METHODS
    //===================================

    setDefaults: function () {

        this.props("id", "process-node/update");
        this.props("enabled", true);
        this.props("icon", PD.Path.to16x16("floppy_disk.png"));
        this.props("label", "Save");
        this.props("visible", true);

        this.state("editor", null);
        this.state("processId", null);
        this.state("nodeId", null);
        this.state("nodeRole", null);
        this.state("nodeType", null);

    },

    setState: function (state) {
        this._super(state);
    },

    execute: function (e) {
        this._super(e);

        var that = this,
            editor = this.state("editor"),
            success = e.success,
            error = e.error;

        editor.save(function (e) {
            that._editorOnSaveSuccess(e, success);
        },
        function (e) {
            that._editorOnSaveError(e, error);
        });

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _editorOnSaveSuccess: function (e, fn) {

        var isProcessNode = this.state("nodeType") === "Process",
            args = this._transformSaveResult(isProcessNode, e);

        if (isProcessNode) {
            PD.context.get_default().get_events().publish("process/update", args);
        }

        PD.context.get_default().get_events().publish("process-node/update", args);

        PD.monitor.getActiveSource().setClean();

        if (fn) {
            fn(args);
        }

    },

    _editorOnSaveError: function (e, fn) {
        if (fn) {
            fn(e);
        }
    },

    _transformSaveResult: function (isProcessNode,data) {

        var item = data.general,
            result = {
                id: isProcessNode ? 0 : item.id,
                icon: item.icon,
                name: item.name
            };

        return result;

    }

});