 # *    If not, see <http://opensource.org/licenses/mit-license.php>.
 #
 
+
+# Factory : 
+# load all dependencies
 fs = require "fs"
 winston = require "winston"
 logger = new winston.Logger

    new winston.transports.Console(colorize: true)
  ]


actors = {}
adapters = {}



newActor = (type, properties) ->
  # Controls and warning about builtinAdapters override
  throw new Error "Actor's type undefined" if not type
  if not actors[type] 
    actors[type] = require type
  else if typeof actors[type] is "string" 
    actors[type] = require actors[type]
  new actors[type] properties

newAdapter = (type, properties) ->
  # Controls and warning about builtinAdapters override
  throw new Error "Adapter's type undefined" if not type
  if not adapters[type] 
    adapters[type] = require type
  else if typeof adapters[type] is "string" 
    adapters[type] = require adapters[type]
  new adapters[type] properties


loadDirectory = (path, callback) ->
  return unless fs.existsSync path
  stats =  fs.statSync path
  return unless stats.isDirectory()
  logger.info "Scanning #{path}..."
  files = fs.readdirSync path
  files.forEach (file) ->
    # TODO Recursive file loading 
    if (file.indexOf ".coffee" isnt -1) and (fs.statSync("#{path}/#{file}").isFile())
      callback file.substr(0, pos), "#{path}/#{file}"  

loadActors = () ->
  builtinActorsName.forEach(name) -> 
    require "./actor/#{name}"
  loadDirectory "#{process.cwd()}/actors", withActor  

loadAdapters = () -> 
  builtinAdaptersName.forEach(name) -> 
    require "./adapters/#{name}"
  loadDirectory "#{process.cwd()}/adapters", withAdapter


loadAdapters
loadActors


exports.withActor = withActor
