// Edit these variables to match the maintenance window

// RESOURCES
// https://stackoverflow.com/questions/27416750/how-to-check-if-the-current-time-is-in-between-two-given-times

// ============= ACTIVATE THE NOTICE HERE ==============
var NoticeIsActive = false;      // Set this variable to "true" if you want the notice to be active.
// ===================================================

var MaintYear = "2022";             // Enter the year in which the maintenance begins (Ex.: 2020)
var MainWeekday = "Tuesday";        // Enter the day of the week maintenance is to begin (Ex.: Friday)
var MaintMonth = "May";           // Enter the Month in which the maintenance begins (Ex.: April)
var MaintMonthNum = "5";            // Enter the Month number in which the maintenance begins (Ex.: 4)
var MaintDate = "3";               // Enter the day of the month in which the maintenance begins (Ex.: 17)
var EndMaintYear = "2022";          // Enter the year in which the maintenance ends (Ex.: 2020)
var EndMaintWeekday = "Sunday"   // Enter the day in which the maintenance ends (Ex.: Saturday)
var EndMaintMonth = "May";        // Enter the Month in which the maintenance begins (Ex.: April)
var EndMaintDate = "22";            // Enter the date in which the maintenance ends (Ex.: 5)

var startTime = '10:00 PM';         // Enter the time at which the maintenance begins (Ex.: 10:00 PM)
var endTime = '2:00 AM';            // Enter the time at which the maintenance ends (Ex.: 2:00 AM)

var DateMatches = false;
var EverythingMatches = false;
var TimesMatch = false;
var date=new Date();
var year=date.getFullYear();
var month=date.getMonth() + 1;
var day=date.getDate();
var time=date.getHours();
var minutes=date.getMinutes();

if (NoticeIsActive) {
    var curr_time = getval();
    // if (get24Hr(curr_time) > get24Hr(startTime) && get24Hr(curr_time) < get24Hr(endTime)) {
    //     //in between these two times
    //     TimesMatch = true;
    // }
    if (MaintDate <= day){
        DateMatches = true;
    }

    if (MaintYear == year.toString() && 
          MaintMonthNum == month.toString() &&
          DateMatches){
            EverythingMatches = true;
    }

    console.log(EverythingMatches);
    if (EverythingMatches){
        // Create the notification
        $('<ServerMaintenanceNotice>' + 
        '<div class="ServerMaintenanceContent">SCHEDULED MAINTENANCE: <br/>On <span id="StartDateTime">' +
        MainWeekday + ', '  + MaintMonth + " " + MaintDate + " starting at " + startTime + ' CST</span>' +
        ' the documentation servers will undergo scheduled maintenance and will be unavailable ' +
        'during this time.<br/>' + 
        'Regular service is scheduled to resume at ' +
        endTime + ' CST, ' + EndMaintWeekday + ', ' + EndMaintMonth + ' ' + EndMaintDate + '.<br/>' +
        'NOTE: This does not impact Wolters Kluwer applications. Only documentation is affected.</div></ServerMaintenanceNotice>').insertBefore('header');
    }
}







function get24Hr(time){
      var hours = Number(time.match(/^(\d+)/)[1]);
      var AMPM = time.match(/\s(.*)$/)[1];
      if(AMPM == "PM" && hours<12) hours = hours+12;
      if(AMPM == "AM" && hours==12) hours = hours-12;

      var minutes = Number(time.match(/:(\d+)/)[1]);
      hours = hours*100+minutes;
      console.log(time +" - "+hours);
      return hours;
 }

function getval() {
     var currentTime = new Date()
     var hours = currentTime.getHours()
     var minutes = currentTime.getMinutes()

     if (minutes < 10) minutes = "0" + minutes;

     var suffix = "AM";
     if (hours >= 12) {
        suffix = "PM";
        hours = hours - 12;
     }
     if (hours == 0) {
        hours = 12;
     }
     var current_time = hours + ":" + minutes + " " + suffix;

     return current_time;

}











