"use strict";

var _ = require("lodash");
var fs = require("fs");
var Client = require("./client");
var mkdirp = require("mkdirp");
var Helper = require("./helper");
var moment = require("moment");

module.exports = ClientManager;

/**
 *  @constructor
 */
function ClientManager() {
	this.clients = [];
}

/**
 *  @param  {string} name
 *  @return {Client}
 */
ClientManager.prototype._findClient = function(name) {
	for (var i in this.clients) {
		var client = this.clients[i];
		if (client.name === name) {
			return client;
		}
	}
	return null;
};

/**
 * @return {void}
 */
ClientManager.prototype.loadUsers = function() {
	var users = this.getUsers();
	for (var i in users) {
		this.loadUser(users[i]);
	}
};

/**
 * @param {string} name
 * @return {void}
 */
ClientManager.prototype.loadUser = function(name) {
	var json = null;
	try {
		json = fs.readFileSync(
			Helper.HOME + "/users/" + name + ".json",
			"utf-8"
		);
		json = JSON.parse(json);
	} catch(e) {
		console.log(e);
		return;
	}

	if (this._findClient(name) === null) {
		this.clients.push(new Client(
			this.sockets,
			name,
			json
		));
		console.log(
			"User '" + name + "' loaded."
		);
	}
};

/**
 * @return {Array}
 */
ClientManager.prototype.getUsers = function() {
	var users = [];
	var path = Helper.HOME + "/users";
	mkdirp.sync(path);
	try {
		var files = fs.readdirSync(path);
		files.forEach(function(file) {
			if (file.indexOf(".json") !== -1) {
				users.push(file.replace(".json", ""));
			}
		});
	} catch(e) {
		console.log(e);
		return null;
	}
	return users;
};

/**
 * @param  {string} name
 * @param  {string} password
 * @return {boolean}
 *
 * @throws {Error}
 */
ClientManager.prototype.addUser = function(name, password) {
	var users = this.getUsers();
	if (users.indexOf(name) !== -1) {
		return false;
	}
	try {
		var path = Helper.HOME + "/users";
		var user = {
			user: name,
			password: password || "",
			log: false,
			networks: []
		};
		mkdirp.sync(path);
		fs.writeFileSync(
			path + "/" + name + ".json",
			JSON.stringify(user, null, "  "),
			{mode: "0777"}
		);
	} catch(e) {
		throw e;
	}
	return true;
};

/**
 * @param  {string} name
 * @return {boolean}
 *
 * @throws {Error}
 */
ClientManager.prototype.removeUser = function(name) {
	var users = this.getUsers();
	if (users.indexOf(name) === -1) {
		return false;
	}
	try {
		var path = Helper.HOME + "/users/" + name + ".json";
		fs.unlinkSync(path);
	} catch(e) {
		throw e;
	}
	return true;
};

/**
 * @return {void}
 */
ClientManager.prototype.autoload = function() {
	var self = this;
	var loaded = [];
	setInterval(function() {
		var loaded = _.pluck(
			self.clients,
			"name"
		);
		var added = _.difference(self.getUsers(), loaded);
		_.each(added, function(name) {
			self.loadUser(name);
		});
		var removed = _.difference(loaded, self.getUsers());
		_.each(removed, function(name) {
			var client = _.find(
				self.clients, {
					name: name
				}
			);
			if (client) {
				client.quit();
				self.clients = _.without(self.clients, client);
				console.log(
					"User '" + name + "' disconnected."
				);
			}
		});
	}, 1000);
};


@@ -816,20 +816,19 @@
  %7B%0A%09var 
-json
+raw
  = null;
@@ -837,20 +837,19 @@
 try %7B%0A%09%09
-json
+raw
  = fs.re
@@ -921,16 +921,78 @@
 8%22%0A%09%09);%0A
+%09%7D%0A%09catch(e) %7B%0A%09%09console.log(e);%0A%09%7D%0A%0A%09var json = null;%0A%09try %7B%0A
 %09%09json =
@@ -1003,20 +1003,19 @@
 N.parse(
-json
+raw
 );%0A%09%7D ca
@@ -1033,32 +1033,58 @@
 console.log(e);%0A
+%09%7D%0A%0A%09if (json === null) %7B%0A
 %09%09return;%0A%09%7D%0A%0A%09i
@@ -1109,17 +1109,17 @@
 t(name) 
-=
+!
 == null)
@@ -1127,26 +1127,32 @@
 %7B%0A%09%09
-this.clients.push(
+return;%0A%09%7D%0A%0A%09var user = 
 new 
@@ -1158,20 +1158,16 @@
  Client(
-%0A%09%09%09
 this.soc
@@ -1175,50 +1175,64 @@
 ets,
-%0A%09%09%09
+ 
 name,
-%0A%09%09%09json%0A%09%09));%0A%09%09console.log(%0A%09%09%09
+ json);%0A%09this.clients.push(user);%0A%0A%09var message = 
 %22Use
@@ -1256,24 +1256,40 @@
 loaded.%22
+;
 %0A%09
-%09);%0A%09%7D
+console.log(message);
 %0A%7D;%0A%0A/**

Clean up ClientManager.prototype.loadUser