/*
 *  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.
 */

/**
 * DAO for manipulating sites
 * @private
 */
Ext.define("Ametys.web.site.SiteDAO", {
    singleton: true,

    constructor: function() {
    		
    	/**
    	 * @callable
    	 * @param member Ametys.web.site.SiteDAO
    	 * @method deleteSite
    	 * This calls the method 'deleteSite' of the server DAO 'Ametys.web.site.SiteDAO'.
    	 * @param {Object[]} parameters The parameters to transmit to the server method
    	 * @param {String} parameters.id The id of site to delete
    	 * @param {String} parameters.name The name of site to delete
    	 */
        this.addCallables({
            role: "org.ametys.web.repository.site.SiteDAO",
            methodName: "deleteSite",
            callback: {
                handler: this._deleteSiteCB,
                scope: this,
                ignoreOnError: true
            },
            waitMessage: true,
            errorMessage: {
            	 msg: "{{i18n PLUGINS_WEB_ADMINISTRATOR_SITES_HANDLE_DELETE_ERROR}}"
            },
            localParamsIndex: 1
        });
        
    	/**
    	 * @callable
    	 * @param member Ametys.web.site.SiteDAO
    	 * @method moveSite
    	 * This calls the method 'moveSite' of the server DAO 'Ametys.web.site.SiteDAO'.
    	 * @param {Object[]} parameters The parameters to transmit to the server method
    	 * @param {String} parameters.target The target id for the move action.
    	 * @param {String[]} parameters.ids The IDs of the sites to move.
    	 */
        this.addCallables({
            role: "org.ametys.web.repository.site.SiteDAO",
            methodName: "moveSite",
            callback: {
                handler: this._moveSiteCB,
                scope: this,
                ignoreOnError: true,
            },
            waitMessage: true,
            errorMessage: {
            	 msg: "{{i18n PLUGINS_WEB_ADMINISTRATOR_SITES_HANDLE_MOVE_ERROR}}"
            }
        });
    },
    
    /**
	 * @private
	 * Callback function after deleting site
	 * @param {Object} response The server response
	 * @param {Object} args The callback arguments
	 * @param {Object} params The callback parameters (server-side and client-side)
	 */
    _deleteSiteCB: function (response, args, params)
	{
		Ext.create('Ametys.message.Message', {
			type: Ametys.message.Message.DELETED,
			targets: [{
				id: Ametys.message.MessageTarget.SITE,
				parameters: {
					id: params[0],
					name: params[1]
				}
			}]
		});
	},

	/**
	 * @private
	 * Callback function after moving site
	 * @param {Object} response The server response
     * @param {Object} args The transmitted arguments. Empty
	 */
    _moveSiteCB: function (response, args)
    {
    	var ids = response.ids || [];
    	var target = response.target;
    	
    	if (ids.length > 0)
		{
    		var targets = [];
    		
    		Ext.Array.forEach (ids, function (id) {
    			targets.push({
					id: Ametys.message.MessageTarget.SITE,
					parameters: { 
					    id: id,
					    target: target
				    }
				})
    		});
    		
			Ext.create("Ametys.message.Message", {
				type: Ametys.message.Message.MOVED,
				targets: targets,
				parameters: {
					major: true
				}
			});
		}
    }
});