var q = require("q");
var request = require('request');

var config = require('../config').config;

var BASE_URI =  config.DB_URL;

this.FACILITY = "facilities";
this.PRODUCT_TYPES = "product_types";
this.CONTACTS = "contacts";
this.STOCK_OUT = "stock_out";
this.CCU_BREAKDOWN = "ccu_breakdown";
this.OFFLINE_SMS_ALERTS = "offline_sms_alerts";

this.getRecord = function(dbName, uuid){
  var deferred = q.defer();
  var URI = BASE_URI + dbName + "/" + uuid;
  var opts = {
    "uri": URI,
    "method": "GET"
  };
  request(opts, function (err, res, body) {
    if (res) {
      deferred.resolve(JSON.parse(res.body));
    } else {
      deferred.reject(err);
    }
  });
  return deferred.promise;
};

this.createOrUpdate = function(dbName, record){
  var deferred  = q.defer();
  var URI = BASE_URI + dbName + "/";

  if (typeof record._id === 'undefined') {
    record._id = record.uuid;
  }

  var requestSettings = {
    "uri": URI + record._id,
    "method": "GET"
  };

  //try to get remote copy of doc.
  request(requestSettings, function (err, res, body) {

    //prepare request settings for PUT request.
    requestSettings.method = "POST";
    requestSettings.json = record;
    requestSettings.uri = URI;

    if (res) {
      var couchResponse = JSON.parse(res.body);
      if (!couchResponse.error) {
        //update couchResponse document with record properties
        var recordProperties = Object.keys(record);
        for (var index in recordProperties) {
          var key = recordProperties[index];
          couchResponse[key] = record[key];
        }

        //set updated couchResponse as json doc to post to server.
        requestSettings.json = couchResponse;

        //POST updated copy
        request(requestSettings, function (err, res, body) {
          if(!err){
            deferred.resolve(couchResponse);
          }else{
            deferred.reject(err);
          }
        });

      } else {
        if (couchResponse.error === 'not_found' && couchResponse.reason === 'missing') {
          //save record as a new doc.
          request(requestSettings, function (err, res, body) {
            if (!err) {
              record._id = res.body.id;
              record._rev = res.body.rev;
              deferred.resolve(record);
            } else {
              deferred.reject(err);
            }
          });
        }else{
          deferred.reject(couchResponse);
        }
      }
    }
  });
  return deferred.promise;
};

//expose storage as a module.
module.storage = this;
@@ -19,23 +19,23 @@
 %22);%0Avar 
-request
+PouchDB
  = requi
@@ -38,23 +38,23 @@
 equire('
-request
+pouchdb
 ');%0A%0Avar
@@ -197,36 +197,8 @@
 s%22;%0A
-this.CONTACTS = %22contacts%22;%0A
 this
@@ -261,16 +261,36 @@
 kdown%22;%0A
+this.CCEI = 'ccei';%0A
 this.OFF
@@ -334,22 +334,17 @@
 %22;%0A%0A
+%0A
 this.
-getRecord
+all
  = f
@@ -354,2114 +354,623 @@
 tion
+ 
 (dbName
-, uuid)%7B%0A  var deferred = q.defer();%0A  var URI = BASE_URI + dbName + %22/%22 + uuid;%0A  var opts = %7B%0A    %22uri%22: URI,%0A    %22method%22: %22GET%22%0A  %7D;%0A  request(opts, function (err, res, body) %7B%0A    if (res) %7B%0A      deferred.resolve(JSON.parse(res.body));%0A    %7D else %7B%0A      deferred.reject(err);%0A    %7D%0A  %7D);%0A  return deferred.promise;%0A%7D;%0A%0Athis.createOrUpdate = function(dbName, record)%7B%0A  var deferred  = q.defer();%0A  var URI = BASE_URI + dbName + %22/%22;%0A%0A  if (typeof record._id === 'undefined') %7B%0A    record._id = record.uuid;%0A  %7D%0A%0A  var requestSettings = %7B%0A    %22uri%22: URI + record._id,%0A    %22method%22: %22GET%22%0A  %7D;%0A%0A  //try to get remote copy of doc.%0A  request(requestSettings, function (err, res, body
+) %7B%0A  var URI = BASE_URI + dbName;%0A  var db = new PouchDB(URI);%0A  return db.allDocs(%7Binclude_docs: true%7D)%0A      .then(function (res) %7B%0A        return res.rows%0A            .map(function (row
 ) %7B%0A
-%0A
     
-//prepare request settings for PUT request.%0A    requestSettings.method = %22POST%22;%0A    requestSettings.json = record;%0A    requestSettings.uri = URI;%0A%0A    if (res) %7B%0A      var couchResponse = JSON.parse(res.body);%0A      if (!couchResponse.error) %7B%0A        //update couchResponse document with record properties%0A        var recordProperties = Object.keys(record);%0A      
+          return row.doc;%0A            %7D);%0A      %7D);%0A%7D;%0A%0Athis.loadDBS = function(dbNames) %7B%0A  var promises = %5B%5D;%0A  var db;%0A
   for
- 
 (var i
-ndex in recordProperti
+ in dbNam
 es)
- 
 %7B%0A    
-  
-    var key = recordProperties%5Bindex%5D;%0A          couchResponse%5Bkey%5D = record%5Bkey%5D;%0A        %7D%0A%0A        //set updated couchResponse as json doc to post to server.%0A        requestSettings.json = couchResponse;%0A%0A        //POST updated copy%0A        request(requestSettings, function (err, res, body) %7B%0A          if(!err)%7B%0A            deferred.resolve(couchResponse);%0A          %7Delse%7B%0A            deferred.reject(err);%0A          %7D%0A        %7D);%0A%0A      %7D else %7B%0A        if (couchResponse.error === 'not_found' && couchResponse.reason === 'missing') %7B%0A          //save record as a new doc.%0A          request(requestSettings, function (err, res, body) %7B%0A            if (!err) %7B%0A              record._id = res.body.id;%0A              record._rev = res.body.rev;%0A              deferred.resolve(record);%0A            %7D else %7B%0A              deferred.reject(err);%0A            %7D%0A          %7D);%0A        %7Delse%7B%0A          deferred.reject(couchResponse);%0A        %7D%0A      %7D%0A    %7D%0A  %7D);%0A  return deferred.promise
+db = dbNames%5Bi%5D;%0A    promises.push(this.all(db));%0A  %7D%0A  return q.all(promises);%0A%7D;%0A%0Athis.bulkUpdate = function(dbName, docs, opts)%7B%0A  var params = opts %7C %7B%7D;%0A  var URI = BASE_URI + dbName;%0A  var db = new PouchDB(URI);%0A  return db.bulkDocs(docs, params)
 ;%0A%7D;%0A%0A
+%0A
 //ex

rewrite storage module to use pouchdb and deprecate direct http requests.