var path = require('path');
var fs = require('fs');
var protocols = require('../rules/protocols');
var util = require('./util');
var mp = require('./module-paths');
var config = require('../config');

var systemPluginPath = config.systemPluginPath;
var CUSTOM_PLUGIN_PATH = config.CUSTOM_PLUGIN_PATH;

function readPluginMoudles(dir, callback, plugins, isCustom) {
  fs.readdir(dir, function(err, list) {
    plugins = plugins || {};
    if (err) {
      return callback(plugins);
    }

    var count = 0;
    var callbackHandler = function() {
      list.forEach(function(name) {
        if (!plugins[name]) {
          plugins[name] = isCustom ? path.join(dir, name, 'node_modules', name) : path.join(dir, name);
        }
      });
      callback(plugins);
    };
    list = list.filter(function(name) {
      if (util.isWhistleModule(name)) {
        return true;
      }

      if (util.isOrgModule(name)) {
        try {
          var _dir = path.join(dir, name);
          ++count;
          var org = name;
          fs.readdir(_dir, function(err, list) {
            if (!err) {
              list.forEach(function(name) {
                if (!plugins[name] && util.isWhistleModule(name)) {
                  plugins[name] = isCustom ? path.join(_dir, name, 'node_modules', org, name) : path.join(_dir, name);
                }
              });
            }

            if (--count <= 0) {
              callbackHandler();
            }
          });
        } catch(e) {}
      }

      return false;
    });

    if (!count) {
      callbackHandler();
    }
  });
}

module.exports = function(callback) {
  var plugins = {};
  var result = {};
  var paths = mp.getPaths();
  var count = paths.length;
  if (!count) {
    return callback(result);
  }

  var loadPlugins = function(dir, cb) {
    var isCustom = CUSTOM_PLUGIN_PATH === dir;
    var isSys = isCustom || systemPluginPath === dir;
    readPluginMoudles(dir, function() {
      var list = Object.keys(plugins).filter(function(name) {
        return !protocols.contains(name.split('.')[1]);
      });
      var len = list.length;
      list.forEach(function(name) {
        var dir = plugins[name];
        var pkgPath = path.join(dir, 'package.json');
        fs.stat(pkgPath, function(err, stats) {
          if (stats && stats.mtime) {
            result[name.split('.')[1] + ':'] = {
              isSys: isSys,
              path: dir,
              pkgPath: pkgPath,
              mtime: stats.mtime.getTime()
            };
          }
          if (--len <= 0) {
            cb();
          }
        });
      });

      if (!len) {
        cb();
      }
    }, plugins, isCustom);
  };
  var index = 0;
  var callbackHandler = function() {
    var dir = paths[++index];
    if (dir) {
      return loadPlugins(dir, callbackHandler);
    }
    callback(result);
  };
  loadPlugins(paths[index], callbackHandler);
};



@@ -2322,15 +2322,15 @@
     
-result%5B
+name = 
 name
@@ -2349,19 +2349,60 @@
 1%5D + ':'
-%5D =
+;%0A            result%5Bname%5D = result%5Bname%5D %7C%7C
  %7B%0A     

load plugins