var M = process.mono;
var fs = require('fs');

// TODO make this configs configurable
var systemStoreApiKey = 'systemStore';
var storesCollection = 'm_stores';
var adapatersPath = __dirname + '/adapters/';

// adapter cache
var adapters = {};

// read available store adapters
var availableAdapters = fs.readdirSync(adapatersPath);

// save adapter paths in adapter cache
for (var i = 0; i < availableAdapters.length; ++i) {
    
    // TODO how should the adapter file structure look like?
    adapters[availableAdapters[i]] = adapatersPath + availableAdapters[i] + '/' + availableAdapters[i] + '.js';
}

module.exports = setup;

// create a new store
function factory (adapter, config, callback) {
    
    // check if adpater exists
    if (!adapters[adapter]) {
        return callback('store: adapter "' + adapter + '" not found.');
    }
    
    // require adapter
    if (typeof adapters[adapter] === 'string') {
        adapters[adapter] = require(adapters[adapter]);
    }
    
    // connect to store
    adapters[adapter].connect(config, callback);
}

// fetch a store config from the system store
function fetchStore (name, callback) {
    
    // check is system store exists
    if (!M[systemStoreApiKey]) {
        return callback('store factory not ready.');
    }
    
    // get store model (mongodb)
    var collection = M[systemStoreApiKey].collection(storesCollection);
    collection.findOne({name: name}, function (err, storeConfig) {
        
        if (err || !storeConfig) {
            return callback(err || 'store "' + name + '" not found.');
        }
        
        // create store
        factory(storeConfig.adapter, storeConfig.config, callback);
    });
}

// setup store factory
function setup (adapter, config, callback) {
    
    // create system store
    factory(adapter, config, function (err, store) {
        
        if (err) {
            return callback(err);
        }
        
        // add system store to api 
        M[systemStoreApiKey] = store;
        
        // add fetch store method to api
        M.store = fetchStore;
        
        callback(null, store);
    });
}

@@ -198,16 +198,53 @@
 pters/';
+%0Avar systemAdapter;%0Avar systemConfig;
 %0A%0A// ada
@@ -1166,16 +1166,25 @@
 e (name,
+ project,
  callbac
@@ -1210,202 +1210,876 @@
 eck 
-is system store exists%0A    if (!M%5BsystemStoreApiKey%5D) %7B%0A        return callback('store factory not ready.');%0A    %7D%0A    %0A    // get store model (mongodb)%0A    var collection = M%5B
+callback%0A    if (typeof project === 'function') %7B%0A        callback = project;%0A        project = undefined;%0A    %7D%0A    %0A    // check is system store exists%0A    if (!M%5BsystemStoreApiKey%5D) %7B%0A        return callback('store factory not ready.');%0A    %7D%0A    %0A    // connect to systemStores of other projects%0A    var config;%0A    if (project) %7B%0A        %0A        // copy system db config%0A        config = %7B%7D;%0A        for (var key in systemConfig) %7B%0A            config%5Bkey%5D = systemConfig%5Bkey%5D;%0A        %7D%0A        %0A        // overwrite system db config%0A        config.database = 'app_' + project;%0A    %7D%0A    %0A    factory(systemAdapter, config %7C%7C systemConfig, function (err, store) %7B%0A        %0A        if (err %7C%7C !store) %7B%0A            return callback(err %7C%7C '
 system
-S
+ s
 tore
-ApiKey%5D
+ not found. ' + project);%0A        %7D%0A        %0A        // get store model (mongodb)%0A        var collection = store
 .col
@@ -2109,16 +2109,20 @@
 n);%0A    
+   
+ 
 collecti
@@ -2176,33 +2176,41 @@
 nfig) %7B%0A        
-%0A
+    %0A    
         if (err 
@@ -2220,32 +2220,36 @@
 !storeConfig) %7B%0A
+    
             retu
@@ -2307,34 +2307,40 @@
 ');%0A        
-%7D%0A
+    %7D%0A  
         
 %0A        // 
@@ -2319,33 +2319,39 @@
     %7D%0A          
-%0A
+  %0A    
         // creat
@@ -2362,24 +2362,28 @@
 ore%0A        
+ 
+   
 factory(stor
@@ -2430,16 +2430,28 @@
 lback);%0A
+        %7D);%0A
     %7D);%0A
@@ -2518,32 +2518,93 @@
 allback) %7B%0A    %0A
+    systemAdapter = adapter;%0A    systemConfig = config;%0A    %0A
     // create sy

connect to stores from different projects