/// class PD.Manage.Modules.ObjectModel.ProcessNodeCreateCommand

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

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

    setDefaults: function () {

        this.props("id", this.inits("id") || "process-node/create");
        this.props("enabled", true);
        this.props("label", "");
        this.props("tooltip", "");
        this.props("canExecute", true);

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

    },

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

        this.props("enabled", this._isEnabled());
        this._updateProps(this.state("nodeType"));
    },

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

        var that = this,
            processId = this.state("processId"),
            nodeId = this.state("nodeId"),
            nodeType = this.state("nodeType");

        PD.monitor.execute(function () {
            that._executeServiceCall(processId, nodeId);
        });

    },

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

    _updateProps: function (nodeType) {

        var icon = "gear.png",
            label = "Node",
            tooltip = null;

        nodeType = nodeType ? nodeType.toLowerCase() : "";

        switch (nodeType) {
            case "process":
                icon = "box_white_surprise.png";
                label = "Key";
                tooltip = "Add new Process Key.";
                break;
            case "processkey":
                label = "Stage";
                tooltip = "Add new Process Stage.";
                break;
            case "processstage":
                icon = "breakpoint.png";
                label = "Decision";
                tooltip = "Add new Process Decision.";
                break;
            case "processdecision":
                label = "Stage";
                break;
        }

        this.props("label", "Add " + label);
        this.props("tooltip", tooltip);
        this.props("icon", ProspectDirector.RUNTIME.resolveUrl("~/application/icons/16x16/" + icon));

    },

    _executeServiceCall: function (processId, parentId) {

        var that = this,
            service = new PD.Manage.Services.ProcessModelService();

        this._enabledAndCanExecute(false);

        service.addNode(processId, parentId === 0 ? null : parentId, function (data) {

            setTimeout(function () {
                that._enabledAndCanExecute(true);
            }, 250);

            PD.context.get_default().get_events().publish("process-node/create", {
                node: data.node,
                parentId: parentId
            });

        });

    },

    _enabledAndCanExecute: function (value) {

        if (value) {
            this.props("canExecute", true);
            this.props("enabled", this._isEnabled());
        }
        else {
            this.props("canExecute", false);
            this.props("enabled", false);
        }
    },

    _isEnabled: function () {
        return this.state("processId") != null &&
            this.state("nodeId") != null &&
            this.state("nodeType") != "ProcessDecision" &&
            this.props("canExecute");
    }

});