window.onload = async function(){
  // Fix button follow
  let follow = document.querySelector('#follow');
  //let feedUrl = location.href.replace(/^https?:/, 'feed:');
  let locationHref = new URL(location.href);
  let node = locationHref.searchParams.get('node')
  let pubsub = locationHref.searchParams.get('pubsub')
  let feedUrl = `feed://${location.host}/atom?pubsub=${pubsub}&node=${node}`;
  follow.href = feedUrl;
  follow.addEventListener ('click', function() {
      window.open(feedUrl, "_self");
  });
  // Fix button subtome
  document.querySelector('#subtome').href='https://www.subtome.com/#/subscribe?feeds=' + feedUrl;
  // Convert ISO8601 To UTC
  for (let element of document.querySelectorAll('#articles > ul > li > div > h4, #feed > #header > h2#subtitle.date')) {
    let timeStamp = new Date(element.textContent);
    element.textContent = timeStamp.toUTCString();
  }
  // Parse Markdown
  for (let element of document.querySelectorAll('#articles > ul > li > div > p')) {
    let markDown = element.textContent
    element.innerHTML = marked.parse(markDown);
  }
  // Build a journal list
  itemsList = await openJson(node)
  if (itemsList && locationHref.searchParams.get('item')) {
    node = locationHref.searchParams.get('node')
    pubsub = locationHref.searchParams.get('pubsub')
    let elementDiv = document.createElement('div');
    elementDiv.id = 'journal';
    let elementH3 = document.createElement('h3');
    elementH3.textContent = 'Journal';
    elementDiv.appendChild(elementH3);
    let elementH4 = document.createElement('h4');
    elementH4.textContent = node;
    elementDiv.appendChild(elementH4);
    let elementUl = document.createElement('ul');
    elementDiv.appendChild(elementUl);
    for (let item of itemsList) {
      let elementLi = document.createElement('li');
      let elementA = document.createElement('a');
      elementA.textContent = item.title;
      elementA.href = item.link;
      elementLi.appendChild(elementA);
      elementUl.appendChild(elementLi);
    }
    let elementB = document.createElement('b');
    elementB.textContent = 'Actions';
    elementDiv.appendChild(elementB);
    let elementUl2 = document.createElement('ul');
    elementDiv.appendChild(elementUl2);
    links = [
      {'text' : 'Subscribe from an XMPP client.',
       'href' : `xmpp:${pubsub}?pubsub;action=subscribe;node=${node}`},
      {'text' : 'Subscribe with a News Reader.',
       'href' : `feed://${location.host}/atom?pubsub=${pubsub}&node=${node}`},
      {'text' : 'Browse the journal.',
       'href' : `atom?pubsub=${pubsub}&node=${node}`}
       ]
    for (let link of links) {
      let elementLi = document.createElement('li');
      let elementA = document.createElement('a');
      elementA.textContent = link.text;
      elementA.href = link.href;
      elementLi.appendChild(elementA);
      elementUl2.appendChild(elementLi);
    }
    elementDiv.appendChild(elementUl2);
    // document.querySelector('#feed').appendChild(elementDiv); // This would result in a combination of Title, Article, and Journal
    document.querySelector('#articles').appendChild(elementDiv);
  }
  // Convert URI xmpp: to URI http: links.
  for (let xmppLink of document.querySelectorAll('a[href^="xmpp:"]')) {
    xmppUri = new URL(xmppLink);
    let parameters = xmppUri.search.split(';');
    try {
      let node = parameters.find(parameter => parameter.startsWith('node=')).split('=')[1];
      let item = parameters.find(parameter => parameter.startsWith('item=')).split('=')[1];
      let pubsub = xmppUri.pathname;
      xmppLink.href = `atom?pubsub=${pubsub}&node=${node}&item=${item}`
    } catch (err) {
      console.warn(err)
    }
  }
  // Display a selection of suggested software.
  const selection = {
    'akregator' : {
      'name'  : 'Akregator',
      'image' : 'akregator.svg',
      'url'   : 'https://apps.kde.org/akregator/'
    },
    'leechcraft' : {
      'name'  : 'LeechCraft',
      'image' : 'leechcraft.png',
      'url'   : 'https://leechcraft.org/'
    },
    'liferea' : {
      'name'  : 'Liferea',
      'image' : 'liferea.svg',
      'url'   : 'https://lzone.de/liferea/'
    },
    'raven' : {
      'name'  : 'Raven Reader',
      'image' : 'raven.svg',
      'url'   : 'https://ravenreader.app/'
    },
    'rssguard' : {
      'name'  : 'RSS Guard',
      'image' : 'rssguard.png',
      'url'   : 'https://github.com/martinrotter/rssguard'
    },
    'rssowl' : {
      'name'  : 'RSSOwl',
      'image' : 'rssowl.svg',
      'url'   : 'http://www.rssowl.org/'
    },
    'tickr' : {
      'name'  : 'TICKR',
      'image' : 'tickr.png',
      'url'   : 'https://www.open-tickr.net/'
    }
  }
  let selectionLink = document.querySelector('#selection-link');
  selectionLink.addEventListener ('click', function() {
    let elementDiv = document.createElement('div');
    elementDiv.id = 'selection-page';
    let elementH1 = document.createElement('h1');
    elementH1.textContent = 'Get A News Reader';
    elementDiv.appendChild(elementH1);
    let elementH2 = document.createElement('h2');
    elementH2.textContent = 'Install Feed Reader Apps For Desktop And Mobile';
    elementDiv.appendChild(elementH2);
    const brands = Object.keys(selection);
    let elementDivSel = document.createElement('div');
    elementDivSel.id = 'selection';
    for (let i = 0; i < brands.length; i++) {
      let brand = brands[i];
      elementSpan = document.createElement('span');
      let elementA = document.createElement('a');
      elementA.href = selection[brand].url;
      elementA.textContent = selection[brand].name;
      let elementImg = document.createElement('img');
      elementImg.src = 'graphic/' + selection[brand].image;
      elementSpan.appendChild(elementImg);
      elementSpan.appendChild(elementA);
      elementDivSel.appendChild(elementSpan);
      elementDiv.appendChild(elementDivSel);
    }
    let elementP = document.createElement('p');
    elementP.textContent = '' +
      'This is a selection of desktop applications, mobile apps and online ' +
      'services for you to choose from. This selection includes news ' +
      'readers, podcast managers, torrent clients, chat bots, HTML browsers ' +
      'and plugins which support syndication feeds.';
    elementDiv.appendChild(elementP);
    let elementDivReturn = document.createElement('div');
    elementDivReturn.id = 'return';
    elementDivReturn.textContent = 'Return To PubSub...';
    elementDivReturn.addEventListener ('click', function() {
      document.querySelector('#selection-page').remove();
    });
    elementDiv.appendChild(elementDivReturn);
    document.body.appendChild(elementDiv);
  });
}

async function openJson(nodeId) {
  return fetch(`/data/${nodeId}.json`)
  .then(response => {
     if (!response.ok) {
       throw new Error('HTTP Error: ' + response.status);
     }
     return response.json();
  })
  .then(json => {
    return json;
  })
  .catch(err => {
    console.warn(err);
  })
}
