/* Magic Mirror
 * Node Helper: Newsfeed
 *
 * By Michael Teeuw http://michaelteeuw.nl
 * MIT Licensed.
 */

var NodeHelper = require('node_helper');
var validUrl = require('valid-url');
var Fetcher = require('./fetcher.js');

module.exports = NodeHelper.create({
	// Subclass start method.
	start: function() {
		console.log('Starting module: ' + this.name);

		this.fetchers = [];
	},

	// Subclass socketNotificationReceived received.
	socketNotificationReceived: function(notification, payload) {
		if(notification === 'ADD_FEED') {
			this.createFetcher(payload.url, payload.reloadInterval);
		}
	},

	/* createFetcher(url, reloadInterval)
	 * Creates a fetcher for a new url if it doesn't exsist yet.
	 * Otherwise it reoses the exsisting one.
	 *
	 * attribute url string - URL of the news feed.
	 * attribute reloadInterval number - Reload interval in milliseconds.
	 */

	createFetcher: function(url, reloadInterval) {
		var self = this;

		if (!validUrl.isUri(url)){
			self.sendSocketNotification('INCORRECT_URL', url);
			return;
		}

		var fetcher;
		if (typeof self.fetchers[url] === 'undefined') {
			console.log('Create new news fetcher for url: ' + url + ' - Interval: ' + reloadInterval);
			fetcher = new Fetcher(url, reloadInterval);

			fetcher.onReceive(function(fetcher) {
				self.sendSocketNotification('NEWS_ITEMS', {
					url: fetcher.url(),
					items: fetcher.items()
				});
			});

			fetcher.onError(function(fetcher, error) {
				self.sendSocketNotification('FETCH_ERROR', {
					url: fetcher.url(),
					error: error
				});
			});

			self.fetchers[url] = fetcher;
		} else {
			console.log('Use exsisting news fetcher for url: ' + url);
			fetcher = self.fetchers[url];
			fetcher.setReloadInterval(reloadInterval);
			fetcher.broadcastItems();
		}

		fetcher.startFetch();
	}
});
