const outputFile = 'news';
const RssBraider = require('rss-braider');
const fs = require('fs');
const ejs = require(
  'ejs');
const read = require('fs').readFileSync;
const join = require('path').join;
const str = read(
  join(__dirname, '/templates/rss.ejs'),
  'utf8');
const feeds = {};

const argv = require('yargs').argv;

const log4js = require('log4js');
const logger = log4js.getLogger();

logger.level = 'trace';

// Pull feeds from config files: ,
//      feeds.simple_test_feed = require("./config/feed").feed;
// Or define in-line

feeds.news = require('./news.json');

logger.debug('news:', feeds.news);


const braider_options = {
  feeds: feeds,
  indent: '    ',
  date_sort_order: 'desc', // Newest first
  log_level: 'warn',
  dedupe_fields: ['link', 'guid'],
  plugins_directories: [__dirname + '/plugins/']
};
const rss_braider = RssBraider.createClient(braider_options);

// Override logging level (debug, info, warn, err, off)
//rss_braider.logger.level('off');

function gatherNews() {
  logger.info('Gathering news...');
  rss_braider.processFeed('news', 'json', function (err, data) {
    console.log('A');
    if (err) {
      logger.error(err);
      return err;
    }

    console.log('B');

    const j = JSON.parse(data);

    console.log('C');
    const ejsOutput = ejs.compile(str)(j);

    console.log('D');
    fs.writeFile(__dirname + '/dist/' + outputFile + '.html',
      ejsOutput,
      function (err) {

        if (err) {
          logger.error(err);
          return console.error(err);
        }

        console.log('The file was saved!');
      });

    fs.writeFile(__dirname + '/dist/' + outputFile + '.json',
      data,
      function (err) {
        if (err) {
          logger.error(err);
          return console.error(err);
        }

        console.log('The file was saved!');
      });
  });

}



gatherNews();
