'use strict';

var github = require('./github');
var npm = require('./npm');
var db = require('./db');
var email = require('./email');

var semver = require('semver');

var cache = {};


db.readSubscriptions(function(error, subscriptions) {
	if (error) {
		console.error(error);
		return;
	}

	cache = {};
	for(var i = 0, n = subscriptions.length; i < n; i++) {
		var subscription = subscriptions[i];

		var user = github.getUserFromUrl(subscription.repourl);
		var repo = github.getRepoFromUrl(subscription.repourl);
		if (!user) {
			console.error('db:readSubscriptions user invalid for ' + subscription.repourl);
		} else if (!repo) {
			console.error('db:readSubscriptions repo invalid for ' + subscription.repourl);
		} else {
			github.getPackageJson(user, repo, processPackage.bind(null, subscription));
		}
	}
});

function cacheIt(packageName, version, timeStamp, isNewRelease) {
	cache[packageName] = {
		version: version,
		timeStamp: timeStamp,
		isNewRelease: isNewRelease
	};
}

function checkPackage(emailAdress, repourl, packageName, currentVersion) {
	getLatestVersion(packageName, function(error, latestVersion, isNew) {
		if (error) {
			console.error(error);
			return;
		}
		semver.clean(currentVersion);
		console.log('one ' + (currentVersion || 'noooo'));
		if (isNew && semver.gt(latestVersion, currentVersion)) {
			email.send(emailAdress, repourl, packageName, currentVersion, latestVersion);
		}
	});
}

function isStillValid(timeStamp) {
	return new Date(timeStamp) >= new Date().setDate(-1);
}

function getLatestVersion(packageName, callback) {
	var cached = cache[packageName];
	if (cached) {
		callback(null, cached.version, cached.isNewRelease);
		return;
	}

	db.getPackageByName(packageName, function(error, packages) {
		if (error) {
			console.error(error);
			callback(error);
			return;
		}
		if (packages.length === 0) {
			npm.getLatestVersion(packageName, function(error, version) {
				var timeStamp = new Date().toUTCString();
				db.createPackage(packageName, version, timeStamp);
				cacheIt(packageName, version, timeStamp, false);
				callback(null, version, false);
			});
			return;
		}
		// TODO: is this needed, when there's a cache?
		var pack = packages[0];
		if (isStillValid(pack.timestamp)) {
			cacheIt(packageName, pack.version, pack.timeStamp, false);
			callback(null, pack.version, false);
			return;
		}

		npm.getLatestVersion(packageName, function(error, version) {
			var timeStamp = new Date().toUTCString();
			db.updatePackageByName(packageName, version, timeStamp);
			semver.clean(pack.version);
			console.log('two ' + (pack.version || 'noooo'));
			var isNewRelease = semver.gt(version, pack.version);
			cacheIt(packageName, version, timeStamp, isNewRelease);
			callback(null, version, isNewRelease);
		});
	});
}

function processPackage(subscription, error, json) {
	if (error) {
		console.error(error);
		return;
	}

	var packageName;
	for (packageName in json.dependencies) {
		checkPackage(subscription.email, subscription.repourl, packageName, json.dependencies[packageName]);
	}
	for (packageName in json.devDependencies) {
		checkPackage(subscription.email, subscription.repourl, packageName, json.devDependencies[packageName]);
	}
}
@@ -1043,30 +1043,29 @@
 geName, 
-currentVersion
+expectedRange
 ) %7B%0A%09get
@@ -1190,93 +1190,8 @@
 %09%09%7D%0A
-%09%09semver.clean(currentVersion);%0A%09%09console.log('one ' + (currentVersion %7C%7C 'noooo'));%0A
 %09%09if
@@ -1208,18 +1208,25 @@
  semver.
-gt
+satisfies
 (latestV
@@ -1233,30 +1233,29 @@
 ersion, 
-currentVersion
+expectedRange
 )) %7B%0A%09%09%09
@@ -1304,22 +1304,21 @@
 me, 
-currentVersion
+expectedRange
 , la
@@ -2452,91 +2452,8 @@
 p);%0A
-%09%09%09semver.clean(pack.version);%0A%09%09%09console.log('two ' + (pack.version %7C%7C 'noooo'));%0A
 %09%09%09v
@@ -2485,24 +2485,37 @@
 gt(version, 
+semver.clean(
 pack.version
@@ -2511,24 +2511,25 @@
 ack.version)
+)
 ;%0A%09%09%09cacheIt

Fix semver usage