/// class PD.Manage.Modules.ObjectModel.ProcessNodeMoveCommand

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

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

    setDefaults: function () {

        this.props("id", "process-node/move");
        this.props("enabled", false);
        this.props("label", "Delete");
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("tooltip", "Delete");

        this.state("moveNode", null);

    },

    setState: function (state) {
        this._super(state);
        this.props("enabled", this.state("moveNode") != null);
    },

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

        var args = this.state("moveNode");

        if (!this._hasSourceAndDestinationNode(args)) {
            return;
        }

        if (!this._isValidDrop(args.src, args.dst, args.position)) {
            
            PD.Web.UI.MessageDialog.warn({
                title: "Invalid Move",
                message: "A " + this._getNodeTypeName(args.src.type) + " cannot be moved to a " + this._getNodeTypeName(args.dst.type) + "."
            });
            
            return;
        }

        this._executeMove(args.src, args.dst, args.position, args.commit);

    },

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

    _hasSourceAndDestinationNode: function (e) {
        return e.src != null || e.dst != null;
    },

    _isValidDrop: function (src, dst, position) {
        
        var result = false;

        if (position === "over") {
            result = (dst.type === "Process" && src.type === "ProcessKey")
                            || (dst.type === "ProcessKey" && src.type === "ProcessStage")
                            || (dst.type === "ProcessStage" && src.type === "ProcessDecision");
        }
        else {
            result = src.type === dst.type;
        }

        return result;

    },

    _getNodeTypeName: function (type) {
    
        var result = "node";

        switch (type) {
            case "Process":
                result = "process";
                break;
            case "ProcessKey":
                result = "key";
                break;
            case "ProcessStage":
                result = "stage";
                break;
            case "ProcessDecision":
                result = "decision";
                break;
        }

        return result;

    },

    _executeMove: function (src, dst, position, commit) {

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

        service.moveNode(src.id, dst.id, position, function (result) {

            commit();

            //PD.context.get_default().get_events().publish("process-node/move", {
            //    nodeId: nodeId
            //});

        });

    }

});