/*
 * == BSD2 LICENSE ==
 * Copyright (c) 2014, Tidepool Project
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the associated License, which is identical to the BSD 2-Clause
 * License as published by the Open Source Initiative at opensource.org.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the License for more details.
 *
 * You should have received a copy of the License along with this program; if
 * not, you can obtain one from Tidepool Project at tidepool.org.
 * == BSD2 LICENSE ==
 */
'use strict';

var common = require('common');
var moment = require('moment');
var pre = require('amoeba').pre;
var request = require('request');

var CARELINK_URLS = {
  security: 'https://carelink.minimed.com/patient/j_security_check',
  login: 'https://carelink.minimed.com/patient/main/login.do',
  csv: 'https://carelink.minimed.com/patient/main/selectCSV.do'
};

function defaultOptions(jar) {
  return  {
    jar: jar,
    headers: {
      Host: 'carelink.minimed.com',
      Connection: 'keep-alive',
      Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
      'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36',
      'Accept-Encoding': 'gzip,deflate,sdch',
      'Accept-Language': 'en-US,en;q=0.8'
    }
  };
}

module.exports = function (opts, cb) {
  var user = pre.hasProperty(opts, 'username');
  var password = pre.hasProperty(opts, 'password');
  var daysAgo = pre.hasProperty(opts, 'daysAgo');

  var jar = request.jar();

  common.step(
    [
      function (next) {
        var reqOptions = defaultOptions(jar);
        reqOptions.qs = {j_username: user, j_password: password};
        request.post(CARELINK_URLS.security, reqOptions, next);
      },
      function (response, next) {
        if (response.statusCode === 403) {
          cb({error: 'badlogin',message: 'Incorrect username or password', code: 403});
          return;
        }

        request.get(CARELINK_URLS.login, defaultOptions(jar), next);
      },
      function (response, next) {
        var m = moment();

        var reqOptions = defaultOptions(jar);
        reqOptions.qs = { t: '11' };
        reqOptions.form = {
          report: 11,
          listSeparator: ',',
          datePicker1: m.format('MM/DD/YYYY'),
          datePicker2: m.subtract(daysAgo || 14, 'days').format('MM/DD/YYYY')
        };

        request.post(CARELINK_URLS.csv, reqOptions, next);
      },
      function(response, next) {
        var body = response.body;
        var regex = /DEVICE DATA \((\d+?) records\)/i;
        var match = regex.exec(body);

        if(!match) {
          cb({error: 'badrequest', message: 'We were unable to fetch data', code: 500});
        }

        if(match && match.length) {
          var records = parseInt(match[1]);

          if(records === 0) {
            cb({error: 'norecords', message: 'No records where found for the last ' + daysAgo + 'days', code: 204});
            return;
          }
        }

        cb(null, response);
      }
    ],
    cb
  );
};

@@ -2606,32 +2606,41 @@
      %7D;%0A%0A       
+ cb(null,
  request.post(CA
@@ -2670,604 +2670,9 @@
 ions
-, next);%0A      %7D,%0A      function(response, next) %7B%0A        var body = response.body;%0A        var regex = /DEVICE DATA %5C((%5Cd+?) records%5C)/i;%0A        var match = regex.exec(body);%0A%0A        if(!match) %7B%0A          cb(%7Berror: 'badrequest', message: 'We were unable to fetch data', code: 500%7D);%0A        %7D%0A%0A        if(match && match.length) %7B%0A          var records = parseInt(match%5B1%5D);%0A%0A          if(records === 0) %7B%0A            cb(%7Berror: 'norecords', message: 'No records where found for the last ' + daysAgo + 'days', code: 204%7D);%0A            return;%0A          %7D%0A        %7D%0A%0A        cb(null, response
+)
 );%0A 

remove check for empty file in fetch