/// class PD.Actions.Action
/// using PD.Actions.ActionDefinition

PD.Actions.Action =
PD.Actions.ActionDefinition.extend({

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (options) {

        this.setOptions(_.extend({},  options));

        this.convertParameters(function (parameter) {
            return new PD.Actions.ActionParameter(parameter);
        });

    },

    getActionDefinition: function () {
        return PD.Actions.ActionDefinition.getActionDefinition(this.get_pluginId(), this.get_id());
    }

});

PD.Actions.Action.invoke = function (literal) {

    var action = new PD.Actions.Action(literal);
    var params = [];
    var result = null;
    var definition = null;

    if (action != null && action.get_id() != null) {

        _.each(literal.parameters, function (o) {
            params[params.length] = o.value;
        });

        definition = action.getActionDefinition()

        if (definition != null) {
            result = definition.method.apply(definition, params);
        }
        
    }

    if (definition == null) {
        PD.Web.UI.MessageDialog.error({
            title: "Execute Action Error",
            message: "The action \"" + literal.pluginId + ":" + literal.id + "\" cannot be executed. Contact your system administrator."
        });
    }

    return result;

};

PD.Actions.Action.getAction = function (pluginId, actionId, values) {

    var plugin = PD.Plugins.PluginManager.getPlugin(pluginId);
    var definition = null;
    var result = null;

    if (plugin != null) {
        definition = plugin.get_actions().getActionDefinitionById(actionId);
    }

    if (definition != null) {
        result = definition.toAction(values || []);
    }

    return result;

};