/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



var MWEBC2S = function () {
    var channelPacketCallbacks= [];
    var privatePacketCallbacks= [];
    var disconnectCallbacks= [];
    var cometURL=null;
    var request = null;
    var connected = false;
    var _this = null;
    return { 	
    	reconnect: function() {
			console.log("reconnect>>>");
			request = $.ajax({
				url:cometURL,
				type:'GET',
				dataType:'json',
				error:this.errorFunction,
				success:this.dataArrived,
				cache:false,
				ifModified:false
			});
			console.log("reconnect<<<");
		},
    	registerChannelPacketCallback: function(callbackFunction) {
    		channelPacketCallbacks.push(callbackFunction);
    	},
    	registerPrivatePacketCallback: function(callbackFunction) {
    		channelPacketCallbacks.push(callbackFunction);
    	},
    	registerDisconnectCallback: function(callbackFunction) {
    		disconnectCallbacks.push(callbackFunction)
    	},
    	dataArrived: function(jsonData){
    		if (jsonData.hasOwnProperty("packetType")){
    			console.log("DataArrived>>>");
    			console.log(jsonData);
    			if (jsonData.packetType == "channel"){
    				for(var i = 0; i<channelPacketCallbacks.length; i += 1){
    					channelPacketCallbacks[i](jsonData.from,jsonData.channelName,jsonData.data);
    				} 
    			} else if (jsonData.packetType == "private"){
    				for(var i = 0; i<privatePacketCallbacks.length; i += 1){
    					privatePacketCallbacks[i](jsonData.from,jsonData.data);
    				} 
    			} else {
    				console.log("Unknown packet type received: "+json.packetType);
    			}
    			console.log("DataArrived<<<");
    		}
    		_this.reconnect();
    	},
    	
    	errorFunction: function(){
    		console.log("errorFunction>>>");
        	if (connected){ // probably a timeout
        		_this.reconnect();
        	} else {
        		for(var i = 0; i<disconnectCallbacks.length; i += 1){
        			disconnectCallbacks[i]();
        		}
        	}
        	console.log("errorFunction<<<");
    	},
    	connect: function(connectionPointURL,connectedCallback){
    		console.log("connect>>>");
    		cometURL = connectionPointURL;
    		_this = this;
    		connected=true;
    		request = $.ajax({
    			url:connectionPointURL,
    			type:'GET',
    			dataType:'json',
    			error:this.errorFunction,
    			cache:false,
    			ifModified:false,
    			success:this.dataArrived 
    		});
    		console.log("connect<<<");
    	},
    	disconnect: function() {
    		connected=false;
    		request.abort();
    	},
    	sendPacketToUser: function(usrName,packetObject){
    		console.log("sendPacketToUser>>>");
    		var packet = {
    				packetType: "private",
    				userName:usrName,
    				data:packetObject
    		};
    		$.ajax({
    			url:cometURL,
    			type:"POST",
    			dataType:"json",
    			data:"packet="+JSON.stringify(packet)
    		});
    		console.log("sendPacketToUser<<<");
    	},
    	sendPacketToChannel: function (chanName,packetObject){
    		var packet = {
    				packetType: "channel",
    				channelName: chanName,
    				data:packetObject
    		};
    		$.ajax({
    			url:cometURL,
    			type:"POST",
    			dataType:"json",
    			data:"packet="+JSON.stringify(packet)
    		});
    	}
    	
    }
}

var mwebc2s = MWEBC2S();

