'use strict';

function storage (collection, storage, pushover) {

  function create (obj, fn) {

    //NOTE: the eventTime is sent by the client, but deleted, we only store created_at right now
    var created_at = new Date();
    var eventTime;

    if (obj.eventTime) {
      eventTime = new Date(obj.eventTime);
      created_at = eventTime;
    }

    obj.created_at = created_at.toISOString();

    var preBolusCarbs = '';
    if (obj.preBolus > 0 && obj.carbs) {
      preBolusCarbs = obj.carbs;
      delete obj.carbs;
    }

    // clean data
    delete obj.eventTime;
    if (!obj.carbs) delete obj.carbs;
    if (!obj.insulin) delete obj.insulin;
    if (!obj.notes) delete obj.notes;
    if (!obj.preBolus || obj.preBolus == 0) delete obj.preBolus;
    if (!obj.glucose) {
      delete obj.glucose;
      delete obj.glucoseType;
    }

    api( ).insert(obj, function (err, doc) {
      fn(null, doc);

      if (obj.preBolus > 0) {
        //create a new object to insert copying only the needed fields
        var pbTreat = {
          created_at: (new Date(created_at.getTime() + (obj.preBolus * 60000))).toISOString(),
          eventType: obj.eventType,
          carbs: preBolusCarbs
        };

        if (obj.notes) pbTreat.notes = obj.notes;

        api( ).insert(pbTreat, function() {
            //nothing to do here
        });
      }

      if (pushover) {

        var text = (obj.glucose ? 'BG: ' + obj.glucose + ' (' + obj.glucoseType + ')' : '') +
          (obj.carbs ? '\nCarbs: ' + obj.carbs : '') +
          (obj.insulin ? '\nInsulin: ' + obj.insulin : '')+
          (obj.preBolus > 0 ? '\nPre-Bolus: ' + obj.preBolus + "  minutes" : '')+
          (obj.enteredBy ? '\nEntered By: ' + obj.enteredBy : '') +
          (eventTime ? '\nEvent Time: adjusted' : '') +
          (obj.notes ? '\nNotes: ' + obj.notes : '');

        var msg = {
          expire: 14400, // 4 hours
          message: text,
          title: obj.eventType,
          sound: 'gamelan',
          timestamp: new Date( ),
          priority: 0,
          retry: 30
        };

        pushover.send( msg, function( err, result ) {
          console.log(result);
        });
      }

    });
  }

  function list (opts, fn) {
    function find ( ) {
      var q = opts && opts.find ? opts.find : { };
      return q;
    }

    return api( ).find(find()).sort({created_at: -1}).toArray(fn);
  }

  function api ( ) {
    return storage.pool.db.collection(collection);
  }

  api.list = list;
  api.create = create;
  return api;
}

function ensureIndexes(name, storage) {
  storage.with_collection(name)(function (err, collection) {
    if (err) {
      console.error("ensureIndexes, unable to get collection for: " + name + " - " + err);
    } else {
      storage.ensureIndexes(collection, ['created_at', 'eventType']);
    }
  });
}

module.exports = {
  storage: storage,
  ensureIndexes: ensureIndexes
};

@@ -1375,24 +1375,221 @@
 ushover) %7B%0A%0A
+        //since we don't know the time zone on the device viewing the push messeage%0A        //we can only show the amount of adjustment%0A        var timeAdjustment = calcTimeAdjustment(eventTime);%0A%0A
         var 
@@ -1662,32 +1662,32 @@
 e + ')' : '') +%0A
-
           (obj.c
@@ -1946,25 +1946,30 @@
        (
-eventTime
+timeAdjustment
  ? '%5CnEv
@@ -1982,17 +1982,26 @@
 me: 
-a
+' + timeA
 djust
-ed'
+ment
  : '
@@ -2255,17 +2255,50 @@
 iority: 
-0
+(obj.eventType == 'Note' ? -1 : 0)
 ,%0A      
@@ -2425,24 +2425,24 @@
 );%0A      %7D%0A%0A
-
     %7D);%0A  %7D%0A
@@ -2434,32 +2434,706 @@
 %7D%0A%0A    %7D);%0A  %7D%0A%0A
+  function calcTimeAdjustment(eventTime) %7B%0A%0A    if (!eventTime) return null;%0A%0A    var now = (new Date()).getTime(),%0A      other = eventTime.getTime(),%0A      past = other %3C now,%0A      offset = Math.abs(now - other);%0A%0A    var MINUTE = 60 * 1000,%0A      HOUR = 3600 * 1000;%0A%0A    var parts = %7B%7D;%0A%0A    if (offset %3C= MINUTE)%0A      return 'now';%0A    else if (offset %3C (HOUR * 2))%0A      parts = %7B value: Math.round(Math.abs(offset / MINUTE)), label: 'mins' %7D;%0A    else%0A      parts = %7B value: Math.round(Math.abs(offset / HOUR)), label: 'hrs' %7D;%0A%0A    if (past)%0A      return parts.value + ' ' + parts.label + ' ago';%0A    else%0A      return 'in ' + parts.value + ' ' + parts.label;%0A  %7D%0A%0A
   function list 

add time adjustment time pushover message, if type is note set priority to -1