/*
 *  Copyright 2023 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 temporary user (awaiting signup finalization).
 * See #createTargets for more details.
 * @private
 */
Ext.define('Ametys.plugins.web.tempusers.TempUserMessageTargetFactory', {
	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.emails The emails of temporay users
	 * @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)
	{
        if (parameters.emails || parameters.email)
        {
            var emails = parameters.emails || Ext.Array.from(parameters.email);
            Ametys.plugins.web.tempusers.TempUsersDAO.getTempUsers(emails, Ext.bind(this._createTargets, this, [callback, parameters], 1));
        }
        else if (parameters.tempusers)
        {
            this._createTargets (parameters.tempusers, callback, parameters);
        }
	},

	/**
	 * Create the temp users targets
	 * @param {Ametys.plugins.web.tempusers.TempUser[]} tempusers Array of temp users.
	 * @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 parameters needed by the factory to create the message. Can not be null. Handled elements are
     * @param {String[]} parameters.emails The temp user's identifiers.
	 * @private
	 */
	_createTargets: function (tempusers, callback, parameters)
	{
        delete parameters['emails'];
        delete parameters['tempusers'];
        
		var targets = [];
		
		Ext.Array.forEach(tempusers, function(tempuser) {
            
            targets.push(Ext.create("Ametys.message.MessageTarget", {
                id: Ametys.message.MessageTarget.TEMP_USER,
                parameters: Ext.merge(tempuser.getProperties(parameters || {}), {tempuser: tempuser})
            }));
		}, this);
			
		callback(targets);
	}
});

Ext.define("Ametys.message.TempUserMessageTarget",
    {
        override: "Ametys.message.MessageTarget",
        
        statics: 
        {
            /**
	         * @member Ametys.message.MessageTarget
	         * @readonly
	         * @property {String} TEMP_USER The target of the message is an temporary user 
	         */
            TEMP_USER: "temp-user"
        }
    }
);