//check for alert every minnute
var CronJob = require('cron').CronJob;
new CronJob('* * * * *', function() {

  console.log("Checking for alert");

  //clear the cache to get the latest user_list
  delete require.cache[require.resolve('../user_list.json')];
  var list = require('../user_list.json');

  var credential = require('../credential.json')

  var accountSid = credential.accountSid; // Your Account SID from www.twilio.com/console
  var authToken = credential.authToken;   // Your Auth Token from www.twilio.com/console

  var twilio = require('twilio');
  var client = new twilio(accountSid, authToken);

  /*const Client = require('twilio').RestClient;



          //verify the phone number 
          client.validationRequests.create(
          {
              friendlyName: 'tetst',
              phoneNumber: '',
          }, (err, callerId) => {
              if (err) {
                console.error(err);
              } else {
                console.log(callerId.sid);
              }
            }
          );*/


  //get current time and parse it to the database time string
  var currTime;

  /** convert UTC time to PST time **/
  var date = new Date();
  var dateObj = new Date(date.toUTCString());
  dateObj.setHours(dateObj.getUTCHours()-7);

  if (dateObj.getHours() && dateObj.getMinutes()) {

      var minuteStr = dateObj.getMinutes().toString();
      if (dateObj.getMinutes().toString().length < 2) {
        minuteStr = "0" + dateObj.getMinutes();
      }

      var hourStr = dateObj.getHours().toString();
      var M; 
      if (dateObj.getHours().toString().length < 2) {
        hourStr = "0" + dateObj.getHours();
      }
      if (dateObj.getHours() > 12) {
        hourStr = (dateObj.getHours() - 12).toString(); 
        M = ' PM'; 
      } else if (dateObj.getHours() == 12) {
        M = ' PM'; 
      } else {
        M = ' AM'; 
      }

      currTime = hourStr + ":" + minuteStr + M;
    }

    //loop through each user in user_list
    for(var user in list) {
      var routines = list[user].routine;
      //loop through each routine to get time
      for(var id in routines) {
        var alertTime = routines[id].time;

        console.log("current time: " + currTime);
        console.log("alert time: " + alertTime);

        if(alertTime == currTime && routines[id].on && list[user].phone != "") {

          /**send the text msg **/
          client.messages.create({
            body: 'Alert from Telebabies: ' + alertTime,
            to: list[user].phone,  
            from: credential.phone // From a valid Twilio number
          })
          .then((message) => console.log(message.sid));
        }
      }
    }
}, null, true, 'America/Los_Angeles');
