/*---------------------------------------------------------------
  :: sails-redis
  -> adapter
---------------------------------------------------------------*/

var async = require('async')
, _       = require('underscore')
, _str    = require('underscore.string')
, redis   = require('redis');

module.exports = (function(){

  // Keep track of Redis clients
  var clients = {};

  var adapter = {

    syncable: false,

    defaults: {
      port: 6379,
      host: 'localhost',
      options: {
        parser: 'hiredis',
        return_buffers: false,
        detect_buffers: false,
        socket_nodelay: true,
        no_ready_check: false,
        enable_offline_queue: true
      },
      password: null
    },

    registerCollection: function(collection, cb) {

      // Reuse existing connection(s)...
      clients[collection.identity] =  _.find(clients, function(client) {
        return collection.host === client.host;
      });

      // ...or create a new one
      if (!clients[collection.identity]) {
        clients[collection.identity] = marshalConfig(collection);
      }

      return cb();
    },

    // TODO: Add optional support for AOF before the adapter shuts down via teardown method

    create: function(collectionName, data, cb) {},

    find: function(collectionName, options, cb) {},

    stream: function(collectionName, options, stream) {},

    update: function(collectionName, options, values, cb) {},

    destroy: function(collectionName, options, cb) {},

    identity: 'sails-redis'

  };

  //////////////                 //////////////////////////////////////////
  ////////////// Private Methods //////////////////////////////////////////
  //////////////                 //////////////////////////////////////////

  // Convert standard adapter config 
  // into a custom configuration object for redis
  function marshalConfig(config) {
    return _.extend(config, {
      port: config.port,
      host: config.host,
      options: config.options
    });
  }

  function connect(collection, cb) {
    try {
      redisClient = redis.createClient(collection.port, collection.host, collection.options);
      if(collection.password) redisClient.auth(collection.password, function(){
        return true;
      });
      return redisClient;
    } catch (e) {
      return cb(e);
    }
  }
  
  return adapter;

})();
@@ -1090,16 +1090,22 @@
       %7D%0A
+      
 %0A      r
@@ -1996,17 +1996,50 @@
 .options
+,%0A      password: config.password
 %0A
-
     %7D);%0A
@@ -2088,33 +2088,54 @@
     
-try %7B%0A      redisClient =
+if(collection.password != null) %7B%0A      return
  red
@@ -2207,18 +2207,13 @@
 ons)
-;%0A      if
+.auth
 (col
@@ -2233,25 +2233,54 @@
 ord)
- redis
+;%0A    %7D else %7B%0A      return redis.create
 Client
-.auth
 (col
@@ -2292,106 +2292,57 @@
 on.p
-assw
 or
-d, function()%7B%0A        return true;%0A      %7D);%0A      return redisClient;%0A    %7D catch (e) %7B%0A  
+t, collection.host, collection.options);%0A    %7D%0A
     
@@ -2355,18 +2355,11 @@
  cb(
-e
 );%0A
-    %7D%0A
   %7D%0A

Refactor connection code