var _ = require('lodash');
var util = require('../../core/util.js');
var log = require(`${util.dirs().core}log`);

var handle = require('./handle');
var mongoUtil = require('./util');

var Reader = function Reader () {
  _.bindAll(this);
  this.db = handle;
  this.collection = this.db.collection(mongoUtil.settings.historyCollection);
  this.pair = mongoUtil.settings.pair.join('_');
}

// returns the furtherst point (up to `from`) in time we have valid data from
Reader.prototype.mostRecentWindow = function mostRecentWindow (from, to, next) {
  var mFrom = from.unix();
  var mTo = to.unix();

  var maxAmount = to - from + 1;

  // Find some documents
  this.collection.find({ pair: this.pair, start: { $gte: mFrom, $lte: mTo } }).sort({ start: 1 }).toArray((err, docs) => {
    if (err) {
      return util.die('DB error at `mostRecentWindow`');
    }
    // no candles are available
    if (docs.length === 0) {
      log.debug('no candles are available');
      return next(false);
    }

    if (docs.length === maxAmount) {
      // full history is available!
      log.debug('full history is available!');
      return next({
        mFrom,
        mTo
      });
    }

    // we have at least one gap, figure out where
    var mostRecent = _.first(docs).start;

    var gapIndex = _.findIndex(docs, (r, i) => r.start !== mostRecent - i * 60);

    // if there was no gap in the records, but
    // there were not enough records.
    if (gapIndex === -1) {
      var leastRecent = _.last(docs).start;
      return next({
        from: leastRecent,
        to: mostRecent
      });
    }

    // else return mostRecent and the
    // the minute before the gap
    return next({
      from: docs[gapIndex - 1].start,
      to: mostRecent
    });
  })
}

Reader.prototype.get = function get (from, to, what, next) { // returns all fields in general
  // Find some documents
  this.collection.find({ pair: this.pair, start: { $gte: from, $lte: to } }).sort({ start: 1 }).toArray((err, docs) => {
    if (err) {
      return util.die('DB error at `get`');
    }
    return next(null, docs);
  });
}

Reader.prototype.count = function fCount (from, to, next) {
  this.collection.count({ pair: this.pair, start: { $gte: from, $lte: to } }, (err, count) => {
    if (err) {
      return util.die('DB error at `count`');
    }
    return next(null, count);
  })
}

Reader.prototype.countTotal = function countTotal (next) {
  this.collection.count({ pair: this.pair }, (err, count) => {
    if (err) {
      return util.die('DB error at `countTotal`');
    }
    return next(null, count);
  })
}

Reader.prototype.getBoundry = function getBoundry (next) {
  this.collection.find({ pair: this.pair }, { start: 1 }).sort({ start: 1 }).limit(1).toArray((err, docs) => {
    if (err) {
      return util.die('DB error at `getBoundry`');
    }
    var start = _.first(docs).start;

    collection.find({ pair: this.pair }, { start: 1 }).sort({ start: -1 }).limit(1).toArray((err2, docs2) => {
      if (err2) {
        return util.die('DB error at `getBoundry`');
      }
      var end = _.first(docs2).start;
      return next(null, { first: start, last: end });
    });
    return null;
  });
}

Reader.prototype.close = function close () {
  this.db = null;
}

module.exports = Reader;

@@ -764,34 +764,26 @@
 %7B start: 1 %7D
-).toArray(
+, 
 (err, docs) 
@@ -2026,26 +2026,18 @@
 art: 1 %7D
-).toArray(
+, 
 (err, do
@@ -2807,34 +2807,26 @@
 1 %7D).limit(1
-).toArray(
+, 
 (err, docs) 
@@ -3032,18 +3032,10 @@
 it(1
-).toArray(
+, 
 (err
@@ -3234,16 +3234,16 @@
     %7D);%0A
-
     retu
@@ -3255,32 +3255,289 @@
 ll;%0A  %7D);%0D%0A%7D%0D%0A%0D%0A
+Reader.prototype.tableExists = function(name, next) %7B%0D%0A  this.db.getCollectionNames((err, names) =%3E %7B%0D%0A    if (err) %7B%0D%0A      log.debug(err);%0D%0A      return util.die('DB error at %60tableExists%60');%0D%0A    %7D%0D%0A    next(null, names.indexOf(name) %3E -1);%0D%0A  %7D);%0D%0A%7D%0D%0A%0D%0A
 Reader.prototype

Add tableExists for mongodb