/*
 * LICENCE[[
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1/CeCILL 2.O 
 *
 * The contents of this file are subject to the Mozilla Public License Version 
 * 1.1 (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.mozilla.org/MPL/ 
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
 * for the specific language governing rights and limitations under the 
 * License. 
 * 
 * The Original Code is utc.fr code. 
 * 
 * The Initial Developer of the Original Code is 
 * david.rivron@utc.fr
 * 
 * Portions created by the Initial Developer are Copyright (C) 2012
 * the Initial Developer. All Rights Reserved. 
 * 
 * Contributor(s): 
 * 
 * 
 * Alternatively, the contents of this file may be used under the terms of 
 * either of the GNU General Public License Version 2 or later (the "GPL"), 
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 
 * or the CeCILL Licence Version 2.0 (http://www.cecill.info/licences.en.html), 
 * in which case the provisions of the GPL, the LGPL or the CeCILL are applicable 
 * instead of those above. If you wish to allow use of your version of this file 
 * only under the terms of either the GPL or the LGPL, and not to allow others 
 * to use your version of this file under the terms of the MPL, indicate your 
 * decision by deleting the provisions above and replace them with the notice 
 * and other provisions required by the GPL or the LGPL. If you do not delete 
 * the provisions above, a recipient may use your version of this file under 
 * the terms of any one of the MPL, the GPL, the LGPL or the CeCILL.
 * ]]LICENCE
 */
 
 var MessagesHandler = require('./MessagesHandler.js'),
	util = require('util');
/**
 * Handler d'un message de type 'uriRequest'.
 * 
 * Permet de récupérer les états des URIS
 * 
 * Exemple de requête des uriStates
 * { type: 'request', requestType: 'uriStates', rootUri: 'wspCd/space/item' } } : retourne la liste des clients et leurs états sur l'item
 * { type: 'request', requestType: 'uriStates', rootUri: 'wspCd', states: { writing: true } } : retourne la liste des clients étant en écriture sur le workspace
 * { type: 'request', requestType: 'clients' } : retourne la liste des clients
 * 
 * Exemple de réponse
 * [ { type: 'request', requestType: 'uriStates', clientId: 'client1', account: 'user1', uri: 'foo/bar1', states: { reading: true, writing: true } ],
 * [ { type: 'request', requestType: 'uriStates', clientId: 'client2', account: 'user2', uri: 'foo/bar2', states: { reading: true, writing: true } ],
 */
	
var RequestMessagesHandler = module.exports = function (pCollabServer) {
	MessagesHandler.call(this, 'request', pCollabServer);
}

util.inherits(RequestMessagesHandler, MessagesHandler);

RequestMessagesHandler.prototype.onMessage = function (pClient, pMessage) {
	if (pMessage.requestType == 'uriStates') this.xHandleUriStatesRequest(pClient, pMessage);
	else if (pMessage.requestType == 'clients') this.xHandleClientsRequest(pClient, pMessage);
}

RequestMessagesHandler.prototype.xHandleUriStatesRequest = function (pClient, pMessage) {
	var vWspCdEnd = pMessage.rootUri.indexOf('/');
	var vWspCd = vWspCdEnd == - 1 ? pMessage.rootUri : pMessage.rootUri.substr(0, vWspCdEnd);
	this.server.requirePerm(pClient, vWspCd, MessagesHandler.OBSERVE_PERM, function () {
		var vResponse = { type: 'response', responseType: 'uriStates', fromRequestId: pMessage.requestId, rootUri: pMessage.rootUri, excludeOwnStates: pMessage.excludeOwnStates, clientId: pClient.clientId, authorId: pMessage.authorId, uriStates: [] };
		for (var vClientId in this.server.clients) {
			if (!pMessage.excludeOwnStates || pClient.clientId != vClientId) {
				var vClient = this.server.clients[vClientId];
				for (var vUri in vClient.urisStates) {
					if (vUri.indexOf(pMessage.rootUri) == 0) {
						var vReqStates = pMessage.states, vUriStates = vClient.urisStates[vUri];
						if (!vReqStates || Object.keys(vReqStates).every(function (pState) { return vReqStates[pState] == vUriStates[pState] })) {
							vResponse.uriStates.push({ uri: vUri, states: vUriStates, account: vClient.account, clientId: vClient.clientId });
						}
					}
				}
			}
 		}
		this.server.logMessage(vResponse, pClient.account);
		pClient.socket.emit('message', vResponse);
	}.bind(this));
}

RequestMessagesHandler.prototype.xHandleClientsRequest = function (pClient, pMessage) {
	this.server.requirePerm(pClient, null, MessagesHandler.ADMIN_PERM, function () {
		var vResponse = { type: 'response', responseType: 'clients', fromRequestId: pMessage.requestId, clients: {} };
		for (var vClientId in this.server.clients) {
			vResponse.clients[vClientId] = this.server.clients[vClientId].account;
 		}
		this.server.logMessage(vResponse, pClient.account);
		pClient.socket.emit('message', vResponse);
	}.bind(this));
}

