var request = require('request');

module.exports = () => ({
  method: 'GET',
  path: '/articles/{id}',
  config: {
    handler: function (req, reply) {
      var articles = [];
      var called = 0;

      // NMeM Bradford
      request('https://www.scienceandmediamuseum.org.uk/collection-media/collection-usage/objects', (err, res, body) => {
        called++;
        if (err) {
          console.error(err);
        }
        var data = JSON.parse(body).filter(e => e.collection_objects.indexOf(req.params.id) > -1);
        if (data.length) {
          articles.push({
            museum: 'National Science and Media Museum',
            data: data
          });
        }
        complete();
      });

      // MSI Manchester
      request('https://www.msimanchester.org.uk/collection-media/collection-usage/objects', (err, res, body) => {
        called++;
        if (err) {
          console.error(err);
        }
        var data = JSON.parse(body).filter(e => e.collection_objects.indexOf(req.params.id) > -1);
        if (data.length) {
          articles.push({
            museum: 'Museum of Science and Industry',
            data: data
          });
        }
        complete();
      });

      function complete () {
        if (called === 2) {
          return reply({data: articles});
        }
      }
    }
  }
});

var request = require('request');

module.exports = () => ({
  method: 'GET',
  path: '/articles/{id}',
  config: {
    handler: function (req, reply) {
      var articles = [];
      var called = 0;

      // NMeM Bradford
      request('http://www.scienceandmediamuseum.org.uk/collection-media/collection-usage/objects', (err, res, body) => {
        called++;
        if (err) {
          console.error(err);
        }
        var data = JSON.parse(body).filter(e => e.collection_objects.indexOf(req.params.id) > -1);
        if (data.length) {
          articles.push({
            museum: 'National Science and Media Museum',
            data: data
          });
        }
        complete();
      });

      // MSI Manchester
      request('http://www.msimanchester.org.uk/collection-media/collection-usage/objects', (err, res, body) => {
        called++;
        if (err) {
          console.error(err);
        }
        var data = JSON.parse(body).filter(e => e.collection_objects.indexOf(req.params.id) > -1);
        if (data.length) {
          articles.push({
            museum: 'Museum of Science and Industry',
            data: data
          });
        }
        complete();
      });

      function complete () {
        if (called === 2) {
          return reply({data: articles});
        }
      }
    }
  }
});

change article feed to http over https