'use strict';

var _         = require('underscore');
var npm       = require('npm');
var Client    = require('npm-registry-client');
var urlParser = require('github-url-from-git');
var npmconf   = require('npmconf');
var path      = require('path');
var config    = require('../config');
var store     = require('./store');
var utils      = require('./utils');

var Registry = module.exports = function (options) {
	this._client = null;
	this._options = options;
	this._syncURL = this._options.registry + 
					config.registry.dumpURL;
	this._cachePath = path.resolve(__dirname + 
							'/../' + 
							config.directory.cache);
	this._urlParserConfig = {
		extraBaseUrls: [options.gitDomain || config.defaultDomain]
	}
};

Registry.prototype._sync = function (callback) {
	this._client.get(this._syncURL, 
				config.registry, 
				function (err, data) {
					if (err) { throw err; }

                    // remove _id and _tag
                    // from the response data
                    delete data._id;
                    delete data._etag;

					var packages = _.values(data);
                    store.update(utils.packageCleaner(packages));

					if (typeof callback === 'function') { callback(packages); }
				}
	); 
};

Registry.prototype.init = function (callback) {
	var self = this;
	npmconf.load({}, function (err, conf) {
		if (err) { throw err; }
		conf.set('cache', self._cachePath);
		conf.set('always-auth', false);
		conf.set('strict-ssl', false);

		self._client = new Client(conf);
		self._sync(function (packages) {
			// start to regularly sync the packages
            setInterval(self._sync.bind(self), config.syncInterval);
            return callback();
		});
	});
};

Registry.prototype.packages = function (page, callback) {
	var self = this;
    store.getPackages(page, function (err, packages) {
        if (err) {
            throw err;
        }
        //TODO: make this a DB operation!
        packages.map(function (pkg) {
	        if (pkg.repository && pkg.repository.url) {
	        	pkg.repository.webURL = urlParser(pkg.repository.url, self._urlParserConfig);
	        }
        }, this);
        callback(packages);
    });
};

Registry.prototype.packageInfo = function (name, callback) {
	this._client.get(this._options.registry + name, 
		config.registry, 
		function (err, data, raw, res) {
			if (err) { throw err; }
			callback(data);
		}
	);
};
@@ -714,16 +714,17 @@
 main%5D%0A%09%7D
+;
 %0A%7D;%0A%0AReg
@@ -1962,24 +1962,18 @@
 pkg) %7B%0A%09
-        
+%09%09
 if (pkg.
@@ -2009,24 +2009,18 @@
 url) %7B%0A%09
-        
+%09%09
 %09pkg.rep
@@ -2091,24 +2091,18 @@
 nfig);%0A%09
-        
+%09%09
 %7D%0A      

fix build - jshint issues