/*
 *  Copyright 2015 Anyware Services
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * This factory creates Ametys.message.MessageTarget for a repository node.
 * @private
 */
Ext.define('Ametys.repository.RepositoryNodeMessageTargetFactory', {
    extend: 'Ametys.message.factory.DefaultMessageTargetFactory',

    /**
     * Create the targets for a message
     * @param {Object} parameters The parameters needed by the factory to create the message. Can not be null. Handled elements are
     * @param {String[]} parameters.ids The content's identifiers. Must be present if resources is empty
     * @param {Ametys.repository.data.RepositoryNode[]} parameters.resources The resources themselves. Must be present if ids is empty
     * @param {Object} [parameters.errorMessage] The error message to display
     * @param {Function} callback The callback function called when the targets are created. Parameters are
     * @param {Ametys.message.MessageTarget[]} callback.targets The targets created. Cannot be null.
     */
    createTargets: function(parameters, callback)
    {
        var targets = [];
        
        if (parameters.paths)
        {
            Ametys.repository.RepositoryDao.getNodes(parameters.paths, parameters.workspaceName, Ext.bind(this._createTargets, this, [callback, parameters], true), parameters.errorMessage);
        }
        else if (parameters.nodes)
        {
            this._createTargets(parameters.nodes, callback, parameters);
        }
    },

    /**
     * Create the content targets
     * @param {Ametys.repository.data.RepositoryNode[]} nodes The resources nodes
     * @param {Function} callback The callback function called when the targets are created. Parameters are
     * @param {Ametys.message.MessageTarget[]} callback.targets The targets created. Cannot be null.
     * @param {Object} parameters The initial parameters of the #createTargets method
     * @private
     */
    _createTargets: function(nodes, callback, parameters)
    {
        delete parameters['paths'];
        delete parameters['nodes'];
        
        var targets = [];
        
        for (var i = 0; i < nodes.length; i++)
        {
            targets.push(Ext.create('Ametys.message.MessageTarget', {
                id: this.getId(),
                parameters: nodes[i].getProperties(parameters)
            }));
        }
        
        callback(targets);
    }
});

Ext.define('Ametys.message.RepositoryNodeMessageTarget', {
    override: 'Ametys.message.MessageTarget',
    
    statics:
    {
        /**
         * @member Ametys.message.MessageTarget
         * @readonly
         * @property {String} REPOSITORY_NODE The target is a repository node. See {@link Ametys.repository.RepositoryNodeMessageTargetFactory} parameters to know more about the associated parameters. 
         */
        REPOSITORY_NODE: 'repository-node',
        
        /**
         * @member Ametys.message.MessageTarget
         * @readonly
         * @property {String} REPOSITORY_SESSION The target is the repository session. Parameters are:
         * @property {String} REPOSITORY_SESSION.workspaceName The workspace name
         */
        REPOSITORY_SESSION: 'repository-session'
    }

});