var Utils = require("util");
var File = require("fs");
var JSONSaver = require("../jsonsaver");

var FactoidServer = module.exports = function(filename) {
	this.filename = filename;
	this.changed = false;
	this.loaded = false;
	this.db = new JSONSaver(filename);
};


FactoidServer.prototype.learn = function(key, value) {

	var db = this.db.object.factoids;

	var popularity = 0;
	key = key.toLowerCase();

	if (typeof db[key] !== "undefined") {
		if (typeof db[key].alias !== "undefined") {
			return this.learn(db[key].alias, value);
		}
		popularity = db[key].popularity;
	}
	
	db[key] = {value: value, popularity: popularity};
	this.db.activity();
};


FactoidServer.prototype.alias = function(alias, key) {
	key = key.toLowerCase();
	alias = alias.toLowerCase();
	
	var db = this.db.object.factoids;
	
	if (typeof db[key] === "undefined") throw new Error("Factoid `"+key+"` doesn't exist.");
	
	if (typeof db[key].alias !== "undefined") {
		return this.alias(alias, db[key].alias);
	}
	
	if (alias === key) throw new Error("Cannot alias yourself.");
	
	db[alias] = {alias: key};
	this.db.activity();
	return key;
};


FactoidServer.prototype.find = function(key, incpop) {
	key = key.toLowerCase();
	var db = this.db.object.factoids;
	
	if (typeof db[key] === "undefined") {
		throw new Error("Factoid `"+key+"` was not found.");
	}
	
	if (typeof db[key].alias !== "undefined") {
		return this.find(db[key].alias);
	}
	
	var thing = db[key];
	if (incpop) thing.popularity++;
	this.db.activity();
	
	return thing.value;
};


FactoidServer.prototype.search = function(pattern, num) {
	if (typeof num !== "number") num = 5;
	var found = [], cat, db = this.db.object.factoids;
	
	for (var i in db) {
		if (db.hasOwnProperty(i)) {
			if (typeof db[i].value === "undefined") break;
		
			cat = (i+" "+db[i].value).toLowerCase();
			if (~cat.indexOf(pattern.toLowerCase())) {
				found.push(i);
			}
		}
	}
	
	found.sort(function(a, b) { return db[b].popularity - db[a].popularity; });
	return found.slice(0, num);
};


FactoidServer.prototype.forget = function(key) {
	key = key.toLowerCase();
	var db = this.db.object.factoids;
	
	if (typeof db[key] === "undefined") {
		throw new Error("`"+key+"` was not a factoid.");
	}
	delete db[key];
	this.db.activity();
	return true;
};


FactoidServer.prototype.clean = function() {
	var db = this.db.object.factoids, j, i;
	for (i in db) {
		if (db.hasOwnProperty(i)) {
			if (typeof db[i].alias !== "undefined") {
				for (j in db[i]) {
					if (db[i].hasOwnProperty(j)) {
						if (j !== "alias") delete db[i][j];
					}
				}
				continue;
			}
			if (typeof db[i].value !== "undefined") {
				for (j in db[i]) {
					if (db[i].hasOwnProperty(j)) {
						if (j !== "value" && j !== "popularity") delete db[i][j];
					}
				}
				continue;
			}
			delete db[i];
		}
	}
	this.db.activity();
};

@@ -354,16 +354,18 @@
 oids;%0A%0A%09
+//
 var popu
@@ -538,16 +538,18 @@
 ;%0A%09%09%7D%0A%09%09
+//
 populari
@@ -579,16 +579,18 @@
 ;%0A%09%7D%0A%09%0A%09
+//
 db%5Bkey%5D 
@@ -627,24 +627,51 @@
 opularity%7D;%0A
+%09db%5Bkey%5D = %7Bvalue: value%7D;%0A
 %09this.db.act
@@ -1475,16 +1475,18 @@
 %5Bkey%5D;%0A%09
+//
 if (incp
@@ -1937,16 +1937,18 @@
 %7D%0A%09%7D%0A%09%0A%09
+//
 found.so
@@ -2724,32 +2724,34 @@
 rty(j)) %7B%0A%09%09%09%09%09%09
+//
 if (j !== %22value
@@ -2784,32 +2784,74 @@
 elete db%5Bi%5D%5Bj%5D;%0A
+%09%09%09%09%09%09if (j !== %22value%22) delete db%5Bi%5D%5Bj%5D;%0A
 %09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09

Remove popularity handling code in factoids