const Router = require('express').Router;
const Crime = require(__dirname + '/../../model/crime');
const eH = require(__dirname + '/../lib/error_handler');
const request = require('superagent');
var dailyRouter = module.exports = exports = Router();

var yesterday = {
  date: new Date(Date.now() - 8.64e+7),
  day: (new Date(Date.now() - 8.64e+7)).getDate(),
  month: (new Date(Date.now() - 8.64e+7)).getMonth() + 1,
  year: (new Date(Date.now() - 8.64e+7)).getFullYear()
};

var dayBefore = {
  date: new Date(Date.now() - 1.728e+8),
  day: (new Date(Date.now() - 1.728e+8)).getDate(),
  month: (new Date(Date.now() - 1.73e+8)).getMonth() + 1,
  year: (new Date(Date.now() - 1.73e+8)).getFullYear()
};

var dailyData = function(callback) {
  request.get('https://data.seattle.gov/resource/tn4m-tpqu.json?month=' + '5' + '&year=' + '2016')
    .end((err, res) => {
      if (err) return eH(err);

      var dailyUpdateArray = [];

      console.log('body length ' + res.body.length);

      for (var i = 0; i < res.body.length; i++) {
        var newCrime = new Crime({
          offense: res.body[i].offense_type,
          date: res.body[i].occurred_date_or_date_range_start.slice(0, 10),
          day: res.body[i].occurred_date_or_date_range_start.slice(8, 10),
          month: res.body[i].month,
          year: res.body[i].year,
          time: res.body[i].occurred_date_or_date_range_start.slice(11),
          longitude: res.body[i].longitude,
          latitude: res.body[i].latitude,
          zone: res.body[i].zone_beat,
          rms_cdw_id: res.body[i].rms_cdw_id
        });

        dailyUpdateArray.push(newCrime);
      };
      for (var j = 0; j < dailyUpdateArray.length; j++) {
        console.log("gonna get " , dailyUpdateArray[j].rms_cdw_id );
        Crime.find({
          rms_cdw_id: dailyUpdateArray[j].rms_cdw_id
        }, (err, data) => {
          console.log("data = ", data);
          console.log("about to save " , dailyUpdateArray[j] );
          if (!data) {
            dailyUpdateArray[j].save((err, data) => {
              if (err) {
                return eH(err);
              }
            });
          }
        });
      }
        console.log('daily' + dailyUpdateArray);
        callback(dailyUpdateArray);
    });
};

var saveNewCrimes = function(newArray) {
    newArray.map( (crime) => {
      Crime.find({ rms_cdw_id: crime.rms_cdw_id }, (err, data) => {
        if (err) return eH(err);
        console.log('no of data from db based on id' + data.length);
        if (data.length === 0) {
          crime.save( (err, data) => {
            if (err) return eH(err);
            console.log('new crime has been added to db' + crime.rms_cdw_id);
          });
        }
      });
    });
};
// dailyData(saveNewCrimes);

setInterval(() => {
  dailyData(saveNewCrimes);
  console.log('getting data');
}, 4000);

@@ -1631,540 +1631,8 @@
 e);%0A
-      %7D;%0A      for (var j = 0; j %3C dailyUpdateArray.length; j++) %7B%0A        console.log(%22gonna get %22 , dailyUpdateArray%5Bj%5D.rms_cdw_id );%0A        Crime.find(%7B%0A          rms_cdw_id: dailyUpdateArray%5Bj%5D.rms_cdw_id%0A        %7D, (err, data) =%3E %7B%0A          console.log(%22data = %22, data);%0A          console.log(%22about to save %22 , dailyUpdateArray%5Bj%5D );%0A          if (!data) %7B%0A            dailyUpdateArray%5Bj%5D.save((err, data) =%3E %7B%0A              if (err) %7B%0A                return eH(err);%0A              %7D%0A            %7D);%0A          %7D%0A        %7D);%0A
     

update daily router