var https = require('https');
var querystring = require('query-string');
var striptags = require('striptags');

// API Keys
var secret = require('../../secret.json');

var parser = {
  fetchText: function(inputurl, callback) {
    // get API key and inputurl
    var tokens = querystring.stringify({
      token: secret.readability,
      url: inputurl
    });

    var config = {
      host: 'readability.com',
      path: '/api/content/v1/parser?' + tokens,
      method: 'GET',
    };

    // request to Readability
    var req = https.request(config, function(res) {
      var responseJSON = '';
      res.setEncoding('utf8');
      // add buffers to container
      res.on('data', function(chunk) {
        responseJSON += chunk;
      });

      res.on('end', function() {
        responseJSON = JSON.parse(responseJSON);
        // strip all HTML tags in the content
        var content = striptags(responseJSON.content);
        // replace all common Unicode characters
        content = content.replace(/&#x2019;/g, "'");
        content = content.replace(/&#x2014;/g, "-");
        content = content.replace(/&#x201C;/g, '"');
        content = content.replace(/&#x201D;/g, '"');
        content = content.replace(/&#xA0;/g, ' ');
        // complete, return out of the function
        callback(null, content);
      });
    });

    // error handling
    req.on('error', function(err) {
      callback(err);
    });

    req.end();
  }
};

module.exports = parser;
