/* application.js: AmanKata Node.js Express Edition
 * Dibuat atas dasar kegalauan oleh manusia.
 */


var http = require('http');
var express = require('express');
var app = express();

// EXPRESS SETTING
app.configure(function() {
    app.set('views', __dirname + '/templates');
    app.set('view engine', 'ejs');
    app.use('/static', express.static(__dirname + "/static"));
    app.use(express.bodyParser());
});

var server = app.listen(5000);
var io = require('socket.io').listen(server);

//HELPER
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
function generate_new_group_key() {
	var new_group_key = getRandomInt(1000000,9999999);
	while (new_group_key in groups)
	{
		new_group_key = getRandomInt(1000000,9999999);
	}
	return new_group_key;
}

// GLOBAL VARIABLE
var user_of_socket = {}
var users = {}
var groups = {}

// ROUTING
app.get('/', function(req, res) {
   res.render('login.ejs');
});

app.get('/login', function(req, res) {
   res.render('login.ejs');
});

app.get('/register', function(req, res) {
   res.render('register.ejs');
});

app.get('/chat', function(req, res) {
   res.render('chat.ejs');
});

// SOCKETIO
io.sockets.on('connection', function(socket) {
	socket.on('registerUser', function(data, fn) {
		var cert = data["new_user_certificate"];
		//sign
		fn(cert);
	});

	socket.on('doLogin', function(data, fn) {
		//check(data["user_id"], data["password"]);
		fn(true);
	});

	socket.on('doLoginChat', function(data, fn) {
		//check(data["user_id"], data["password"]);
		var user_id = data.user_id;
		user_of_socket[socket] = user_id;
		users[user_id] = {};
		users[user_id]["socket"] = socket;
		users[user_id]["groups"] = [];
		fn(true);
	});

	socket.on('requestGroupSession', function(fn) {
		var user = user_of_socket[socket];
		var user_group = [];
		for (group_id in users[user]["groups"]){
			var group = users[user]["groups"][group_id];
			user_group.push(group); 
		}
		user_group = [
			{'group_id':1212124,'group_host_user_id':'ihe','group_guest_user_id':['haidar','ali','wira']},
			{'group_id':1343444,'group_host_user_id':'ihe','group_guest_user_id':['haidar','ali','wira']}
		];
		fn(user_group);
	});

	socket.on('newGroup', function(data) {
		var new_group = data;
		var group_id = generate_new_group_key();
		new_group["group_id"] = group_id;
		groups[group_id] = new_group;
		users[data["group_host_user_id"]]["groups"].push(group_id);
		for (user_id in data["group_guest_user_id"]) {
			var user = data["group_guest_user_id"][user_id];
			users[user].push(group_id);
		}
	});

	socket.on('disconnect', function() {
	});

});

@@ -95,17 +95,16 @@
 a.%0A */%0A%0A
-%0A
 var http
@@ -607,36 +607,30 @@
 ion generate
-_new_g
+G
 roup
-_k
+K
 ey() %7B%0A%09var 
@@ -788,16 +788,75 @@
 p_key;%0A%7D
+%0Afunction checkUserValid(user) %7B%0A%09return (user in users);%0A%7D
 %0A%0A// GLO
@@ -2285,20 +2285,291 @@
 ion(data
+, fn
 ) %7B%0A
+%09%09boolean invalid_user = false;%0A%09%09for (user_id in data%5B%22group_guest_user_id%22%5D) %7B%0A%09%09%09var user = data%5B%22group_guest_user_id%22%5D%5Buser_id%5D;%0A%09%09%09if (!checkUserValid(user))%0A%09%09%09%7B%0A%09%09%09%09invalid_user = true;%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if (invalid_user)%0A%09%09%7B%09%0A%09%09%09fn(false);%0A%09%09%09return;%0A%09%09%7D%0A
 %09%09var ne
@@ -2613,20 +2613,14 @@
 rate
-_new_g
+G
 roup
-_k
+K
 ey()
@@ -2887,16 +2887,140 @@
 d);%0A%09%09%7D%0A
+%09%09fn(true);%0A%09%7D);%0A%0A%09socket.on('sendChat', function(data) %7B%0A%09%09users%5Bdata%5B'to_id'%5D%5D%5B'socket'%5D.emit('retrieveChatClient',data);%0A
 %09%7D);%0A%0A%09s

send chat