var basicScraper = require('./controllers/basicScraperController');
var getExternalUrl = require('./controllers/urlController').getExternalUrl;
var CronJob = require('cron').CronJob;
var secret = require('../config.js');
var db = require('./db.js');
var Sequelize = require('sequelize');
var mandrill = require('mandrill-api');
mandrill_client = new mandrill.Mandrill(secret.mandrill.client_id);

// To run the cronjob as it is now: navigate to server dir and type node cronjob
// for every five minutes
// var schedule = '* +' */5 * * * *';


// for faster testing
// var schedule = '*/30 * * * * *';
var schedule = '*/5 * * * * *';

//To run every 3 seconds do */3; every 5 min do * */5 *

var cronjob = new CronJob(schedule, function() {
  console.log('You will see this message every 5 min');
  // check database for jobs assigned for cronjob

  // get urls 
  db.User.findAll()
    .then(function(allUsers) {
      for (var i = 0; i < allUsers.length; i++){
        var currEmail = allUsers[i].email;
        allUsers[i].getUrls()
          .then(function(url) {
            // display html that are changed
            for (var i = 0; i < url.length; i++) {
              getExternalUrl(url[i], function(newHtml, url) {
                newHtml = newHtml.substring(3000, 4000);
                if (url) {
                  var oldHtml = url.UserUrl.html;
                  if (!(oldHtml === newHtml)) {
                    console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
                    console.log('there is a change at', url.url,'!')

                    // send email
                    // sendEmail(currEmail, currEmail);


                    // update html value in database


                  }
                }
              });
              
            }
          })
      }
    })

}, null, true, 'America/Los_Angeles');

var sendEmail = function (email, name){
  var message = {
    "html": "<span>The Scrapinit found a change in the webpage you are following</span>",
    "subject": "We scraped some tubular stuff for you!!",
    "from_email": email,
    "from_name": "The Blank Arrays",
    "to": [{
      "email": email,
      "name":  name,
      "type": "to"
    },
    ],
    "headers": {
      "Reply-To": ""
    },
    "important": true,
  };

var async = false;
//send email // uncomment to send an email
mandrill_client.messages.send({"message": message, "async": async}, function(result) {
  console.log('Sent a message to '+ email+'  '+ result);
}, function(e) {
            // Mandrill returns the error as an object with name and message keys
            console.log('A mandrill error occurred: ' + e.name + ' - ' + e.message);
            // A mandrill error occurred: Unknown_Subaccount - No subaccount exists with the id 'customer-123'
  });
}
@@ -876,18 +876,16 @@
 ndAll()%0A
-  
   .then(
@@ -901,26 +901,24 @@
 allUsers) %7B%0A
-  
     for (var
@@ -952,18 +952,16 @@
 ; i++)%7B%0A
-  
       va
@@ -999,18 +999,16 @@
 ;%0A      
-  
 allUsers
@@ -1021,20 +1021,16 @@
 tUrls()%0A
-    
       .t
@@ -1312,26 +1312,139 @@
     
- var oldHtml
+for (var i=0; i%3Curl.length;i++)%7B%0A                        // get image out of the database%0A                        var img1
  = url
+%5B0%5D
 .Use
@@ -1452,67 +1452,477 @@
 Url.
-html;%0A                  if (!(oldHtml === newHtml)) %7B%0A     
+dataValues.cropImage;%0A                        // access the url from which the picture was taken%0A                        // call a function which takes url of the image and get a pic of the website%0A                        var img2; %0A                        compare(img1,img2);%0A                      %7D%0A                    %7D%0A%0A                    var compare = function (img1, img2)%7B%0A              // using a library compare img1 to img2%0A              if (!(img1===img2)%7B%0A
     
@@ -2027,37 +2027,32 @@
 %0A               
-     
 console.log('the
@@ -2084,17 +2084,16 @@
 rl,'!')%0A
-%0A
         
@@ -2174,18 +2174,16 @@
 Email);%0A
-%0A%0A
         
@@ -2227,18 +2227,16 @@
 atabase%0A
-%0A%0A
         
@@ -2256,32 +2256,33 @@
                %7D
+ 
 %0A              %7D
@@ -2281,18 +2281,16 @@
        %7D
-);
 %0A       
@@ -2294,18 +2294,20 @@
         
-  
+%7D);%0A
 %0A       
@@ -2326,28 +2326,18 @@
      %7D)%0A
-      %7D%0A    
+%7D%0A
 %7D)%0A%0A%7D, n
@@ -2803,16 +2803,18 @@
 ,%0A  %7D;%0A%0A
+  
 var asyn
@@ -3306,15 +3306,23 @@
 er-123'%0A
+        
   %7D);%0A%7D

set up for img comparison