var telepath = require('tele')
, stream = require('stream')
, async = require ('async')

module.exports = function (usr) { // BRICOLEUR
  var self = this
  , map = null
  , _ = {} // module scope
  telepath(this)

  // detect if running in node or browser
  if (global.process && global.process.title === 'node') self.scope = 'server'
  if (!global.process) self.scope = 'client'

  if (usr) self.usr = usr
    
  this.recv = function (buffer) {
    var data = JSON.parse(buffer.toString())

    if (data.meta === 'module_map') { // add map 
      map = data[self.scope]
      self.build()
    }
  }

  this.build = function () { // load modules
    if (!map) throw new Error('no map')

    async.forEach(map, match, function () {
      console.log(usr.host+' modules loaded')
    })

    function match (mod, cb) {
      var modules = self.usr.modules
      for (var i=0;i<modules.length;i++) {
        if (modules[i]===mod.id) {
          self.loadModule(mod)
          cb()
          break
        } else cb()
      }
    }
  }

  this.connModule = function (connect) { // ['modA>modB','modB>modC']

  }

  this.disconnModule = function (disconnect) {

  }

  this.loadModule = function (mod) { // load module!
    _[mod.id.toUpperCase()] = require(mod.filePath)
    _[mod.id] = new _[mod.id.toUpperCase()]()
    if (mod.html) _[mod.id].template = mod.html
  }

  this.unloadModule = function (mod) {

  }

  // ----------------------------------------------------
  this.addConnection = function (key) {
    self[key] = {}
    var s = self[key]

    // add write stream
    s.in = new stream.Writable()
    s.in._write = function (chunk, encoding, cb) {
      self.recv(chunk)
      cb()
    }

    // add read stream
    s.out = new stream()
    s.out.readable = true
    s.send = function (data) {
      s.out.emit('data',JSON.stringify(data))
    }
  }

  this.removeConnection = function (key) {
    self[key].out.emit('close')
    delete self[key]
  }// ---------------------------------------------------
}

@@ -1059,19 +1059,16 @@
 on (conn
-ect
 ) %7B // %5B
@@ -1092,16 +1092,311 @@
 %3EmodC'%5D%0A
+    var modA = null%0A    , modB = null%0A%0A    for (key in conn) %7B%0A      modA = _%5Bconn%5Bkey%5D.split('%3E')%5B0%5D%5D%0A      modB = _%5Bconn%5Bkey%5D.split('%3E')%5B1%5D%5D%0A     if (!modA.out %7C%7C !modB.in) throw new Error(modA+','+modB+' :no .out or .in connections')%0A     if (modA.out && modB.in) modA.out.pipe(modB.in)%0A    %7D
 %0A  %7D%0A%0A  
@@ -1433,19 +1433,16 @@
 (disconn
-ect
 ) %7B%0A%0A  %7D

update connModule method in brico