import moment from 'moment-timezone';
const CalendarUtils = {
  // default timezone is İstanbul
  timeZone: 'Europe/Istanbul',
  // default language is English
  locale: 'tr',
  timeFormat: '24-hour',
  dateFormat: 'DD/MM/YYYY',
  dayNamesOrdered: [],
  setLocale(locale, strings, weekStartDay = 'monday') {
    moment.locale(locale);
    CalendarUtils.locale = locale;
    if (strings) {
      setMomentI18n(locale, strings);
      const daysNames = [
        strings.sunday3, strings.monday3, strings.tuesday3, strings.wednesday3,
        strings.thursday3, strings.friday3, strings.saturday3
      ];
      const dayNamesOrdered = [];
      for (let i = 0; i < 7; i++) {
        dayNamesOrdered.push(daysNames[(i + CalendarUtils.getWeekStartIndex(weekStartDay)) % 7]);
      }
      CalendarUtils.dayNamesOrdered = dayNamesOrdered;
    }
  },
  getWeekStartIndex(weekStartDay) {
    switch (weekStartDay) {
      case 'monday':
        return 1;
      case 'saturday':
        return 6;
      case 'sunday':
      default:
        return 0;
    }
  },
  createDateWithHours(date, hour, minutes) {
    const day = date.getDate();
    const month = date.getMonth() + 1;
    const year = date.getFullYear();
    return moment.tz(`${day}/${month}/${year} ${hour}:${minutes}`, "D/M/YYYY h:mm", CalendarUtils.timeZone).toDate();
  },
  setTimeZone(timeZone) {
    CalendarUtils.timeZone = timeZone;
  },
  setTimeFormat(timeFormat) {
    CalendarUtils.timeFormat = timeFormat;
  },
  setDateFormat(dateFormat) {
    CalendarUtils.dateFormat = dateFormat;
  },
  fixHourDifference(date) {
    const browserOffset = new Date().getTimezoneOffset();
    const timeZoneOffset = moment.tz.zone(CalendarUtils.timeZone).utcOffset(new Date());
    const diff = (browserOffset - timeZoneOffset) / 60;
    date.setHours(date.getHours() - diff);
    return date;
  },
  format(date, formatString) {
    if (formatString === 'HH:mm' && CalendarUtils.timeFormat === '12-hour') {
      return moment(date).tz(CalendarUtils.timeZone).format('hh:mm A');
    }
    return moment(date).tz(CalendarUtils.timeZone).format(formatString);
  },
  // Returns the GMT format of the time zone.
  // See: https://momentjs.com/timezone/docs/#/using-timezones/formatting/
  getZoneAbbr(timeZone = moment.tz.guess()) {
    return moment.tz(timeZone).zoneAbbr();
  },
  /**
   * Return startDate and endDate Unix time stamp values from the date objects in parameters.
   *
   * @param date (JS date object) of the selected day.
   * @param startTime (JS date object) of the start hour.
   * @param endTime (JS date object) of the end hour.
   * @returns {{startDate: number, endDate: number}}
   */
  getStartAndEndDateFromInputs(date, startTime, endTime) {
    let startDate = new Date(date.getTime());
    startDate.setHours(startTime.getHours(), startTime.getMinutes(), 0);
    let endDate = new Date(date.getTime());
    endDate.setHours(endTime.getHours(), endTime.getMinutes(), 0);
    startDate = CalendarUtils.fixHourDifference(startDate);
    endDate = CalendarUtils.fixHourDifference(endDate);
    // convert dates to unix time
    return {
      startDate: startDate.getTime() / 1000,
      endDate: endDate.getTime() / 1000,
    };
  },
  elapsedTime(date) {
    return moment(date).tz(CalendarUtils.timeZone).fromNow();
  },
  elapsedTimeBetweenDates(date1, date2) {
    return moment(date1).from(moment(date2));
  },
  getMonday() {
    return moment().startOf('isoWeek').toDate();
  },
  getStartEndOfWeek(relativeWeekNumber = 0) {
    // const relativeDate = new Date();
    // relativeDate.setDate(relativeDate.getDate() + (relativeWeekNumber * 7));
    const addition = relativeWeekNumber * (7 * 24 * 60 * 60 * 1000);
    const monday = new Date(new Date(CalendarUtils.getMonday()).getTime() + addition);
    monday.setHours(0, 0, 0, 0);
    const sunday = new Date(monday.getTime());
    sunday.setDate(monday.getDate() + 6);
    sunday.setHours(23, 59, 59, 99);
    return { relativeWeekNumber, firstDayOfWeek: monday, lastDayOfWeek: sunday };
  },
};
export function setMomentI18n(language, strings) {
  moment.locale(language, {
    months: [
      strings.january,
      strings.february,
      strings.march,
      strings.april,
      strings.may,
      strings.june,
      strings.july,
      strings.august,
      strings.september,
      strings.october,
      strings.november,
      strings.december,
    ],
    monthsShort: [
      strings.january3,
      strings.february3,
      strings.march3,
      strings.april3,
      strings.may3,
      strings.june3,
      strings.july3,
      strings.august3,
      strings.september3,
      strings.october3,
      strings.november3,
      strings.december3,
    ],
    monthsParseExact: true,
    weekdays: [
      strings.sunday,
      strings.monday,
      strings.tuesday,
      strings.wednesday,
      strings.thursday,
      strings.friday,
      strings.saturday,
    ],
    weekdaysShort: [
      strings.sunday3,
      strings.monday3,
      strings.tuesday3,
      strings.wednesday3,
      strings.thursday3,
      strings.friday3,
      strings.saturday3,
    ],
    weekdaysMin: [
      strings.sunday2,
      strings.monday2,
      strings.tuesday2,
      strings.wednesday2,
      strings.thursday2,
      strings.friday2,
      strings.saturday2,
    ],
    // weekdaysParseExact : true,
    // longDateFormat : {
    //   LT : 'HH:mm',
    //   LTS : 'HH:mm:ss',
    //   L : 'DD/MM/YYYY',
    //   LL : 'D MMMM YYYY',
    //   LLL : 'D MMMM YYYY HH:mm',
    //   LLLL : 'dddd D MMMM YYYY HH:mm'
    // },
    // calendar : {
    //   sameDay : '[Aujourd’hui à] LT',
    //   nextDay : '[Demain à] LT',
    //   nextWeek : 'dddd [à] LT',
    //   lastDay : '[Hier à] LT',
    //   lastWeek : 'dddd [dernier à] LT',
    //   sameElse : 'L'
    // },
    // relativeTime : {
    //   future : 'dans %s',
    //   past : 'il y a %s',
    //   s : 'quelques secondes',
    //   m : 'une minute',
    //   mm : '%d minutes',
    //   h : 'une heure',
    //   hh : '%d heures',
    //   d : 'un jour',
    //   dd : '%d jours',
    //   M : 'un mois',
    //   MM : '%d mois',
    //   y : 'un an',
    //   yy : '%d ans'
    // },
    // dayOfMonthOrdinalParse : /\d{1,2}(er|e)/,
    // ordinal : function (number) {
    //   return number + (number === 1 ? 'er' : 'e');
    // },
    // meridiemParse : /PD|MD/,
    // isPM : function (input) {
    //   return input.charAt(0) === 'M';
    // },
    // // In case the meridiem units are not separated around 12, then implement
    // // this function (look at locale/id.js for an example).
    // // meridiemHour : function (hour, meridiem) {
    // //     return /* 0-23 hour, given meridiem token and hour 1-12 */ ;
    // // },
    // meridiem : function (hours, minutes, isLower) {
    //   return hours < 12 ? 'PD' : 'MD';
    // },
    // week : {
    //   dow : 1, // Monday is the first day of the week.
    //   doy : 4  // Used to determine first week of the year.
    // }
  });
}
export default CalendarUtils;
