Plugins.ThreeGiG.ProspectDirector.BuiltIn.ExecuteRuleAction =
PD.Actions.ActionDefinition.extend({

    init: function (options) {

        this._super({
            id: "execute-rule",
            pluginId: "builtin",
            name: "Execute Rule",
            description: "Executes rules contained within a named rule script.",
            parameters: [
                new PD.Actions.ActionParameterDefinition({
                    name: "Entity ID",
                    type: PD.Actions.ActionParameterType.INTEGER,
                    description: "The ID of the entity to be the context within which the rule should be executed."
                }),
                new PD.Actions.ActionParameterDefinition({
                    name: "Script Name",
                    type: PD.Actions.ActionParameterType.STRING,
                    description: "The script name containing the rules to be executed."
                }),
                new PD.Actions.ActionParameterDefinition({
                    name: "Refresh",
                    type: PD.Actions.ActionParameterType.BOOLEAN,
                    description: "Set to true if the canvas should be refreshed after the rules are executed.",
                    optional: true
                })
            ]
        });

    },

    method: function (entityId, scriptName, refresh) {

        var that = this;

        PD.ajax({
            type:"POST",
            url: PD.Path.resolve("~/Shared/Rule/ExecuteByScriptName"),
            data: {
                entityId: entityId,
                scriptName: scriptName
            },
            success: function (e) {
                that._onScriptExecuteSuccess(e, entityId, refresh);
            }
        });

    },

    _onScriptExecuteSuccess: function (e, entityId, refresh) {

        if (e.hasScriptErrors) {
            PD.Web.UI.BoxedDialog.error({
                title: "Errors",
                items: e.scriptErrors
            });
            return;
        }

        if (e.hasScriptAlerts) {
            PD.Web.UI.BoxedDialog.info({
                title: "Message",
                items: e.scriptAlerts
            });
        }

        if (refresh) {
            ProspectDirector.RUNTIME.MODULES.Explorer.loadContent(
                ProspectDirector.RUNTIME.CURRENT_FOLDER_ID,
                entityId);
        }

    }

});