async function getCalendar(icalUrl) {
  const response = await fetch(icalUrl);
  const data = await response.text();
  return data;
}

function getDateFromjCal(jCal, event, name) {
  const date = event.getFirstPropertyValue(name);
  const vtimezone = jCal.getFirstSubcomponent('vtimezone');
  if (vtimezone && moment(date.toJSDate()).utcOffset()) {
    date.zone = new ICAL.Timezone(vtimezone);
  }
  var locale = window.navigator.userLanguage || window.navigator.language;
  moment.locale(locale);
  return moment.parseZone(date.toJSDate()).utcOffset(date.utcOffset() / 60)
};

function insertCalendarEvents(dlElement, icalUrl, maxAgeDays = 100) {

  if (!icalUrl) {
    return;
  }

  getCalendar(icalUrl).then(data => {
    // console.log(data);
    var jcalData = ICAL.parse(data);
    var vcalendar = new ICAL.Component(jcalData);
    ICAL.helpers.updateTimezones(vcalendar, ICAL.TimezoneService);
    const element = dlElement;

    var events = vcalendar.getAllSubcomponents('vevent');
    var eventArray = [];
    var eventKeys = [];
    var now = moment();
    events.forEach(event => {
      var start = getDateFromjCal(vcalendar, event, 'dtstart');
      if (now.diff(start, 'days') <= maxAgeDays) {
        var key = event.getFirstPropertyValue("dtstart") + "-" + event.getFirstPropertyValue("dtend");
        eventArray[key] = event;
        eventKeys.push(key);
      }
    });
    dlElement.replaceChildren();
    if (eventKeys.length == 0) {
      const dd = document.createElement('dd');
      dd.appendChild(document.createTextNode("–"));
      element.appendChild(dd);
      return;
    }
    eventKeys.sort().reverse().forEach(key => {
      var event = eventArray[key];
      var summary = event.getFirstPropertyValue('summary');
      var start = getDateFromjCal(vcalendar, event, 'dtstart');
      var end = getDateFromjCal(vcalendar, event, 'dtend');
      const dt = document.createElement('dt');
      const time = document.createElement('time');
      dt.setAttribute('class', 'maintenance');
      const node = document.createTextNode(start.format('dddd, LL'));
      time.appendChild(node);
      time.setAttribute('datetime', start.format('YYYY-MM-DD'));
      dt.appendChild(time);
      element.appendChild(dt);
      const dd = document.createElement('dd');
      const description = document.createTextNode(start.format("HH:mm") + "-" + end.format("HH:mm") + ` ${summary}`);
      dd.appendChild(description);
      element.appendChild(dd);
    });
  }
  ).catch(error => {
    console.log(error);
  });
}
