/// class PD.Actions.ActionDefinition

PD.Actions.ActionDefinition =
Class.extend({

    //===================================
    // FIELDS
    //===================================

    _plugin: null,
    _parameters: null,

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

    init: function (options) {

        this.setOptions(options);

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

    },

    //===================================
    // PROPERTIES
    //===================================

    get_id: function () {
        return this._options.id;
    },

    get_pluginId: function () {
        return this._options.pluginId;
    },

    get_name: function () {
        return this._options.name;
    },

    get_description: function () {
        return this._options.description;
    },

    get_parameters: function () {
        return this._options.parameters;
    },

    get_targetArbiter: function () {
        return this._options.targetArbiter;
    },

    get_targetType: function () {
        return this._options.targetType;
    },

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

    // must override
    method: function () {
        // do nothing on purpose
    },

    getPlugin: function () {

        if (this._plugin == null && this.get_pluginId() != null) {
            this._plugin = PD.Plugins.PluginManager.getPlugin(this.get_pluginId());
        }

        return this._plugin;

    },

    getFullName: function () {
        return this.getPlugin().get_name() + " : " + this.get_name();
    },

    toLiteral: function (data) {

        return _.extend({
            id: this.get_id(),
            name: this.get_name(),
            pluginId: this.get_pluginId(),
            parameters: _.map(this.get_parameters(), function (item, index) {
                return item.toLiteral();
            }),
            description: this.get_description(),
            targetArbiter: this.get_targetArbiter(),
            targetType: this.get_targetType()
        }, data || {});

    },

    toAction: function (values) {
        
        var result = null;

        values = values || [];

        result = new PD.Actions.Action(this.toLiteral({
            parameters: _.map(this.get_parameters(), function (parameter, index) {

                var value = {};

                if (index <= values.length) {
                    value = values[index]
                }

                return new PD.Actions.ActionParameter(parameter.toLiteral(value));
            
            }),
            method: this.method
        }));

        return result;

    },

    //===================================
    // PROTECTED METHODS
    //===================================

    setOptions: function (options) {

        this._options = _.extend({
            id: null,
            name: null,
            pluginId: null,
            parameters: [],
            description: null
        }, options);

    },

    convertParameters: function (fn) {

        var parameters = this._options["parameters"];

        this._options["parameters"] = _.map(parameters, function (parameter, index) {
            if (parameter.hasOwnProperty("name")) {
                return fn(parameter);
            }
            else {
                return parameter;
            }
        })

    },

    parametersToLiteral: function () {

        var result = _.map(this.get_parameters(), function (parameter, index) {
            return parameter.toLiteral();
        });

        return result;

    }

});

PD.Actions.ActionDefinition.getActionDefinition = function (pluginId, actionId) {

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

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

    return result;

};