
const bruTimezone = 'Europe/Brussels';
const workDays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
export default class DateFormat{
  
  static hourByTimezone(timeZoneName = bruTimezone){
    let date = new Date;
    let hour = date.toLocaleString('de-DE', {hour: '2-digit', hour12: false, timeZone: timeZoneName });
    return parseInt(hour);
  }
  static hourInsideIntervalByTimezone(minH = 8, maxH = 18, timeZone = bruTimezone){
    let currentHour = this.hourByTimezone(timeZone);
    if( (minH <= currentHour) &&( currentHour< maxH)){
      return true;
    }
    else {
      return false;
    }
  }
  static timeInsideIntervalByTimezone(intervalList = ['8:50-9:20', '13:20-13:50', '15:20-15:50'], timeZone = bruTimezone){
    if(this.hourInsideIntervalByTimezone()){
      for(let index = 0; index<intervalList.length; index++){
        let dateStart = new Date();
        let dateStop = new Date();
        let currentDate = new Date();
        let intervalSplit = intervalList[index].split('-');
        let start = intervalSplit[0].split(':');
        let stop = intervalSplit[1].split(':')
        dateStart.setHours(parseInt(start[0]));
        dateStart.setMinutes(parseInt(start[1]));
        dateStop.setHours(parseInt(stop[0]));
        dateStop.setMinutes(parseInt(stop[1]));
        currentDate.setHours(this.hourByTimezone(timeZone))
        if( dateStart <= currentDate && currentDate <= dateStop){
          console.log('Running on interval: ', intervalList[index])
          return true
        }
      }
    }
    return false;
  }
  static timeInsideIntervalAndTimeZoneAndBusinessDay(intervalList = ['8:50-9:20', '13:20-13:50', '15:20-15:50'], timeZone = bruTimezone){
    if(this.isWorkWeekDay()){
      return this.timeInsideIntervalByTimezone(intervalList, timeZone);
    }else{
      return false;
    }
  }

  static isWorkWeekDay(){
    let currentDay = new Date().toLocaleString('en-us', { weekday: 'short' })
    console.log('DateUtils: '+ currentDay + ' is a working day: ' + workDays.includes(currentDay))
    return workDays.includes(currentDay);
  }
}