var fs = require('fs'),
    url = require('url'),

    Router = require('mixu_minimal').Router,

    Package = require('./package.js'),
    Resource = require('./resource.js'),

    remoteUrl = 'http://registry.npmjs.org/',
    logger = console;

var api = new Router();

api.configure = function(config) {
  if(typeof config.remoteUrl !== 'undefined') {
    remoteUrl = config.remoteUrl;
  }
  if (typeof config.logger !== 'undefined') {
    logger = config.logger;
  }
};

// GET /package
api.get(new RegExp('^/([^/]+)$'), function(req, res, match) {
  var name = match[1];
  Package.getIndex(name, function(err, data) {
    if (err) {
      logger.error('[500] Error: ', err);
      res.statusCode = 500;
      res.end();
      return;
    }
    res.end(JSON.stringify(data));
  });
});

// GET /package/-/package-version.tgz
api.get(new RegExp('^/(.+)/-/(.+)$'), function(req, res, match) {
  var name = match[1],
      file = match[2],
      uri = remoteUrl + name + '/-/' + file;
  // direct cache access - this is a file get, not a metadata get
  logger.log('cache get', uri);

  Resource.get(uri)
          .getReadablePath(function(err, fullpath) {
            if (err) {
              logger.error('[500] Error: ', err);
              res.statusCode = 500;
              res.end();
              return;
            }
            res.setHeader('Content-type', 'application/octet-stream');
            fs.createReadStream(fullpath).pipe(res);
          });
});

// /-/ or /package/-/ are special
api.get(new RegExp('^/-/(.+)$'), Package.proxy);
api.get(new RegExp('^/(.+)/-(.*)$'), Package.proxy);

// GET /package/version
api.get(new RegExp('^/(.+)/(.+)$'), function(req, res, match) {
  var name = match[1],
      version = match[2];
  Package.getVersion(name, version, function(err, data) {
    if (err) {
      logger.error('[500] Error: ', err);
      res.statusCode = 500;
      res.end();
      return;
    }
    res.end(JSON.stringify(data));
  });
});

module.exports = api;

var fs = require('fs'),
    url = require('url'),

    Router = require('mixu_minimal').Router,

    Package = require('./package.js'),
    Resource = require('./resource.js'),

    remoteUrl = 'http://registry.npmjs.org/',
    logger = console;

var api = new Router();

api.configure = function(config) {
  if(typeof config.remoteUrl !== 'undefined') {
    remoteUrl = config.remoteUrl;
  }
  if (typeof config.logger !== 'undefined') {
    logger = config.logger;
  }
};

// GET /package
api.get(new RegExp('^/([^/]+)$'), function(req, res, match) {
  var name = match[1];
  Package.getIndex(name, function(err, data) {
    if (err) {
      logger.error('[500] Error: ', err);
      res.statusCode = 500;
      res.end();
      return;
    }
    res.end(JSON.stringify(data));
  });
});

// GET /package/download/package-version.tgz
// GET /package/-/package-version.tgz
api.get(new RegExp('^/([^/]+)/(.+)/([^/]+)$'), function(req, res, match) {
  var name = match[1],
      name2 = match[2],
      file = match[3],
      uri = remoteUrl + name + '/' + name2 + '/' + file;
  // direct cache access - this is a file get, not a metadata get
  logger.log('cache get', uri);

  Resource.get(uri)
          .getReadablePath(function(err, fullpath) {
            if (err) {
              logger.error('[500] Error: ', err);
              res.statusCode = 500;
              res.end();
              return;
            }
            res.setHeader('Content-type', 'application/octet-stream');
            fs.createReadStream(fullpath).pipe(res);
          });
});

// /-/ or /package/-/ are special
api.get(new RegExp('^/-/(.+)$'), Package.proxy);
api.get(new RegExp('^/(.+)/-(.*)$'), Package.proxy);

// GET /package/version
api.get(new RegExp('^/([^/]+)/([^/]+)$'), function(req, res, match) {
  var name = match[1],
      version = match[2];
  Package.getVersion(name, version, function(err, data) {
    if (err) {
      logger.error('[500] Error: ', err);
      res.statusCode = 500;
      res.end();
      return;
    }
    res.end(JSON.stringify(data));
  });
});

module.exports = api;

support some mirror dist url like /package/download/package-version.tgz