"use strict";

var redis   = require("redis"),
    client  = redis.createClient(),
    _       = require("underscore"),
    Fetcher = require("l2b-price-fetchers");


var vendorCodes = _.keys(Fetcher.prototype.scrapers);


function bookDetailsCacheKey (isbn) {
  return "bookDetails-" + isbn;
}

function getBookDetails (isbn, cb) {

  var cacheKey = bookDetailsCacheKey(isbn);

  client.get(cacheKey, function (err, reply) {
    if ( reply ) {
      cb( null, JSON.parse(reply) );
    } else {
      fetchFromScrapers(
        {vendor: "foyles", isbn: isbn },
        function (err, results) {
          if (err) { return cb(err); }
          var bookDetails = extractBookDetails(results);
          cb(null, bookDetails);
        }
      );
    }
  });

}

function extractBookDetails (results) {
  return _.pick(results, "isbn", "authors", "title");
}


function cacheBookDetails (data) {
  var isbn = data.isbn;
  var cacheKey = bookDetailsCacheKey(isbn);

  client.exists(cacheKey, function (err, exists) {
    if (!err && !exists) {
      // TODO perhaps put an expiry in here...
      var bookDetails = extractBookDetails(data);
      client.set(cacheKey, JSON.stringify(bookDetails));
    }
  });

}


function fetchFromScrapers (options, cb) {
  var f = new Fetcher();
  f.fetch(
    options,
    function (err, data) {
      if (err) { return cb(err); }
      cacheBookDetails(data);
      cacheBookPrices(data);
      cb(null, data);
    }
  );
}




function bookPricesCacheKey (opt) {
  return ["bookPrice", opt.isbn, opt.country, opt.currency, opt.vendor].join("-");
}

function cacheBookPrices (data) {
  // console.log(data);

  _.each(data.prices, function (price) {
    _.each( price.countries, function (country) {

      var entry = _.chain(price)
        .omit("countries")
        .defaults({country: country})
        .value();

      var cacheKey = bookPricesCacheKey(entry);

      var ttl = Math.floor(entry.validUntil - new Date().valueOf()/1000);

      client.setex(
        cacheKey,
        ttl,
        JSON.stringify(entry)
      );
    });
  });
}


module.exports = {
  getBookDetails: getBookDetails,
  vendorCodes: vendorCodes,
};

@@ -160,16 +160,95 @@
 rs%22);%0A%0A%0A
+client.on(%22error%22, function (err) %7B%0A  console.log(%22Redis error %22 + err);%0A%7D);%0A%0A%0A
 var vend
@@ -629,17 +629,48 @@
 bn: isbn
- 
+, country: %22GB%22, currency: %22GBP%22
 %7D,%0A     
@@ -908,58 +908,129 @@
 %7B%0A  
-return _.pick(results, %22isbn%22, %22authors%22, %22title%22)
+var data = %7B%0A    isbn:    results.args.isbn,%0A    authors: results.authors,%0A    title:   results.title,%0A  %7D;%0A  return data
 ;%0A%7D%0A
@@ -1569,24 +1569,27 @@
 data);%0A     
+ //
  cacheBookPr
@@ -1639,16 +1639,19 @@
 ;%0A%7D%0A%0A%0A%0A%0A
+// 
 function
@@ -1674,24 +1674,27 @@
 Key (opt) %7B%0A
+// 
   return %5B%22b
@@ -1764,19 +1764,25 @@
 n(%22-%22);%0A
-%7D%0A%0A
+// %7D%0A%0A// 
 function
@@ -1807,16 +1807,19 @@
 data) %7B%0A
+// 
   // con
@@ -1834,19 +1834,24 @@
 (data);%0A
-%0A
+//%0A//
   
+ 
 _.each(d
@@ -1877,24 +1877,27 @@
 n (price) %7B%0A
+// 
     _.each( 
@@ -1934,17 +1934,22 @@
 ntry) %7B%0A
-%0A
+//%0A// 
       va
@@ -1973,16 +1973,19 @@
 (price)%0A
+// 
         
@@ -2003,16 +2003,19 @@
 tries%22)%0A
+// 
         
@@ -2044,16 +2044,19 @@
 untry%7D)%0A
+// 
         
@@ -2065,17 +2065,22 @@
 alue();%0A
-%0A
+//%0A// 
       va
@@ -2119,23 +2119,28 @@
 entry);%0A
-%0A
+//%0A//
       
+ 
 var ttl 
@@ -2199,17 +2199,22 @@
 /1000);%0A
-%0A
+//%0A// 
       cl
@@ -2225,16 +2225,19 @@
 .setex(%0A
+// 
         
@@ -2246,16 +2246,19 @@
 cheKey,%0A
+// 
         
@@ -2262,16 +2262,19 @@
    ttl,%0A
+// 
         
@@ -2295,16 +2295,19 @@
 (entry)%0A
+// 
       );
@@ -2311,16 +2311,19 @@
  );%0A
+// 
     %7D);%0A
   %7D)
@@ -2318,22 +2318,28 @@
     %7D);%0A
+//
   
+ 
 %7D);%0A
+// 
 %7D%0A%0A%0Amodu

Add country and currency to the book detail getter, comment out price caching