/**
 * QuickBlox VideoChat WebRTC signaling library
 * version 0.1.0
 *
 * Author: Igor Khomenko (igor@quickblox.com)
 *
 */

/*
  Public methods:
    - call(userID, sessionDescription, sessionID)
    - accept(userID, sessionDescription, sessionID)
    - reject(userID, sessionID)
    - stop(userID, reason, sessionID)
    - sendCandidate(userID, candidate, sessionID)
  
  Public callbacks:
    - onCall(fromUserID, sessionDescription, sessionID)
    - onAccept(fromUserID, sessionDescription, sessionID)
    - onReject(fromUserID)
    - onStop(fromUserID, reason)
    - onCandidate(fromUserID, candidate)
 */

var QBSignalingType = {
	CALL: 'qbvideochat_call',
	ACCEPT: 'qbvideochat_acceptCall',
	REJECT: 'qbvideochat_rejectCall',
	STOPCALL: 'qbvideochat_stopCall',
	CANDIDATE: 'qbvideochat_candidate'
};

function QBVideoChatSignaling() {
	var self = this;
	
	this.onCallCallbacks = [];
 	this.onAcceptCallbacks = [];
 	this.onRejectCallbacks = [];
 	this.onStopCallbacks = [];
 	this.onCandidateCallbacks = null;
	
	this.onMessage = function(msg) {
		var to = msg.getAttribute('to');
		var from = msg.getAttribute('from');
		var type = msg.getAttribute('type');
		var elems = msg.getElementsByTagName('body');
		var body = Strophe.getText(elems[0]);
		var fromUserID = from.split('-')[0];
		var sessionID = '110101010101';
	
		switch (type) {
		case QBSignalingType.CALL:
			for (var i=0; i < self.onCallCallbacks.length; i++) {
				var callback = self.onCallCallbacks[i];
				if (typeof(callback) === "function") {
					callback(sessionID, body);
				}
			}
			break;
		case QBSignalingType.ACCEPT:
			for (var i=0; i < self.onAcceptCallbacks.length; i++) {
				var callback = self.onAcceptCallbacks[i];
				if (typeof(callback) === "function") {
					callback(body);
				}
			}
			break;
		case QBSignalingType.REJECT:
			for (var i=0; i < self.onRejectCallbacks.length; i++) {
				var callback = self.onRejectCallbacks[i];
				if (typeof(callback) === "function") {
					callback(fromUserID);
				}
			}
			break;
		case QBSignalingType.STOPCALL:
			for (var i=0; i < self.onStopCallbacks.length; i++) {
				var callback = self.onStopCallbacks[i];
				if (typeof(callback) === "function") {
					callback(fromUserID, body, sessionID);
				}
			}
			break;
		case QBSignalingType.CANDIDATE:
			var jsonCandidate = self.xmppTextToDictionary(body);
			self.onCandidateCallbacks(jsonCandidate);
			break;
		}
		
		return true;
	};
	
	this.sendMessage = function(userID, type, data, sessionID) {
		var opponentJID = userID + "-" + QBPARAMS.app_id + "@" + CHAT.server;
		var body = data == null ? '' : data;
	
		var reply = $msg({to: opponentJID, 
						 from: this.userJID, 
						 type: type})
				.cnode(Strophe.xmlElement('body', body));
		
		this.connection.send(reply);
	};

	this.xmppTextToDictionary = function(data) {
		try {
			return $.parseJSON(Strophe.unescapeNode(data));
		} catch(err) {
			return Strophe.unescapeNode(data);
		}
	};

	this.xmppDictionaryToText = function(data) {
		return Strophe.escapeNode(JSON.stringify(data));
	};
}

// methods
QBVideoChatSignaling.prototype.call = function(userID, sessionDescription, sessionID) {
	traceS('call ' + userID);
	//this.sendMessage(userID, QBSignalingType.CALL, sessionDescription, sessionID);
};

QBVideoChatSignaling.prototype.accept = function(userID, sessionDescription, sessionID) {
	traceS('accept ' + userID);
	this.sendMessage(userID, QBSignalingType.ACCEPT, sessionDescription, sessionID);
};

QBVideoChatSignaling.prototype.reject = function(userID, sessionID) {
	traceS('reject ' + userID);
	this.sendMessage(userID, QBSignalingType.REJECT, null, sessionID);
};

QBVideoChatSignaling.prototype.stop = function(userID, reason, sessionID) {
	traceS('stop ' + userID);
	this.sendMessage(userID, QBSignalingType.STOPCALL, reason, sessionID);
};

QBVideoChatSignaling.prototype.sendCandidate = function(userID, candidate, sessionID) {
	this.sendMessage(userID, QBSignalingType.CANDIDATE, candidate, sessionID);
};

// callbacks
QBVideoChatSignaling.prototype.addOnCallCallback = function(callback) {
	this.onCallCallbacks.push(callback);
};

QBVideoChatSignaling.prototype.addOnAcceptCallback = function(callback) {
	this.onAcceptCallbacks.push(callback);
};

QBVideoChatSignaling.prototype.addOnRejectCallback = function(callback) {
	this.onRejectCallbacks.push(callback);
};

QBVideoChatSignaling.prototype.addOnStopCallback = function(callback) {
	this.onStopCallbacks.push(callback);
};

function traceS(text) {
	console.log("[qb_videochat_signalling]: " + text);
}

@@ -1044,24 +1044,46 @@
 on(msg) %7B%0A%09%09
+console.log(msg);%0A%09%09/*
 var to = msg
@@ -2426,16 +2426,18 @@
 eak;%0A%09%09%7D
+*/
 %0A%09%09%0A%09%09re
@@ -2536,124 +2536,104 @@
 tJID
- = userID + %22-%22 + QBPARAMS.app_id + %22@%22 + CHAT.server;%0A%09%09var body = data == null ? '' : data
+, body, reply;%0A%09%09%0A%09%09opponentJID = getJID(userID);%0A%09%09body = data ? data : ''
 ;%0A%09
+%09
 %0A%09%09
-var reply = $msg(%7B
+params = %7B%0A%09%09%09
 to: 
@@ -2648,45 +2648,38 @@
 JID,
- 
 %0A%09%09%09
-%09%09%09 
 from: 
-this.userJID
+connection.jid
 , %0A%09%09%09
-%09%09%09 
 type
@@ -2688,67 +2688,61 @@
 type
-%7D)
+%0A%09%09%7D;
 %0A%09%09
+%0A
 %09%09
-.cnode(Strophe.xmlElement
+reply = $msg(params).c
 ('body'
-, 
+).t(
 body)
-)
 ;%0A%09%09
-%0A%09%09this.
 conn
@@ -3152,16 +3152,19 @@
 S('call 
+to 
 ' + user
@@ -3169,18 +3169,16 @@
 erID);%0A%09
-//
 this.sen

send video message