module.exports = function(nodecg, Twitch){
	var lastFollowRequest = nodecg.Replicant('lastFollowRequests', {defaultValue: "1970-01-01T00:00:00.000Z"});
	var followOffset = nodecg.Replicant('followOffset', {defaultValue: 0});
	var lastFollowRequestDate = new Date(lastFollowRequest.value);
	var trackingEvents = [{type: 0, display: "Followers", eventId: "channel-followed", resourceUrl: "/channels/{{username}}/follows",
	 												offset: nodecg.Replicant('followOffset', {defaultValue: 0}), lastObserved: nodecg.Replicant('lastFollowRequest', {defaultValue: "1970-01-01T00:00:00.000Z"}),
													lastObservedDate: function(){return new Date(this.lastObserved.value)},
													eventText: function(event){ return event.user.display_name },
													getElements: function(twitchResponse){ return twitchResponse.body.follows; },
												  endCycle: function(response){
																											 nodecg.log.info("Response total: " + response._total +" and offset is "+ this.offset.value)
																											 // We lost followers :(
														 													 if(response._total == 0){
																												 // We didn't had any :(
																												 if(this.offset.value == 0){
																													 return;
																												 }
																												 --this.offset.value;
																											 };}}]

	// Twitch API seems to only allow us to poll list of followers no ability to be notified of next new follower seems to be provided

  // Iterate over new events and push notification
	function NewNotification(nodecg, Twitch, trackingEvent){
		FetchEvents(nodecg, Twitch, trackingEvent, function(response){
			trackingEvent.getElements(response).forEach(event => {
				var eventDate = new Date(event.created_at);

				//New observed
				if(trackingEvent.lastObservedDate() < eventDate){
					nodecg.log.info("New event observed")
					nodecg.sendMessage(trackingEvent.eventId, {display_name : trackingEvent.eventText(event), type : trackingEvent.type, showtime: nodecg.Replicant('alertShowtime', {defaultValue: 2000}).value});

					trackingEvent.offset.value += 1;
					trackingEvent.lastObserved.value = eventDate.toISOString();
				}
			})
			nodecg.log.info(JSON.stringify(response))
			trackingEvent.endCycle(response.body);
		});
	}



	// Get the next 25 next followers
	function FetchEvents(nodecg, Twitch, trackingEvent, callback){
		nodecg.log.info("Offset "+trackingEvent.offset.value)
		Twitch.get(trackingEvent.resourceUrl, {
				    limit: 25,
				    direction: 'asc',
				    offset: trackingEvent.offset.value
		}).then(response =>{
			if (response.statusCode !== 200) {
		        return nodecg.log.error(response.body.error, response.body.message);
		    }

		    callback(response);
		}).catch(err =>{
			nodecg.log.error(err);
		});
	}

	// Social links slides
	function PushSocials(){
			var socialImages = nodecg.Replicant('assets:socials').value;

			var socialBanner = {
														images: socialImages.map(image => { return { Url: image.url } }),
														timeperslide: nodecg.Replicant('timeperslide', {defaultValue: 1500}).value,
														transitionsspeed: nodecg.Replicant('transitionsspeed', {defaultValue: 1000}).value
												 }
			nodecg.log.info("pushing socials");

			nodecg.sendMessage("channel-slideshow", socialBanner);

			var periodforslides = nodecg.Replicant('periodforslides', {defaultValue: 30000}).value;

			nodecg.log.info("Socials Period " + periodforslides);

			setTimeout(PushSocials, periodforslides)
	}

	// Poll list of events
	setInterval(function(){
		nodecg.log.info("polling followers");
		// Follows
		NewNotification(nodecg, Twitch, trackingEvents[0]);
	}, 4000);

	var periodforslides = nodecg.Replicant('periodforslides', {defaultValue: 30000}).value;


	nodecg.log.info("periodVal " + periodforslides);

	// Show socials
	setTimeout(PushSocials, periodforslides)
}




/* username = response.body.follows.reverse()[0].user.name;

		    nodecg.sendMessage("channel-followed", { display_name : username}); */

/*response.body.follows.forEach(follow => {
		    	const username = follow.user.name;
		    	console.log("%s followed the channel %s", username, Twitch.channel);
		    });*/

@@ -45,550 +45,132 @@
 var 
-lastFollowRequest = nodecg.Replicant('lastFollowRequests', %7BdefaultValue: %221970-01-01T00:00:00.000Z%22%7D);%0A%09var followOffset = nodecg.Replicant('followOffset', %7BdefaultValue: 0%7D);%0A%09var lastFollowRequestDate = new Date(lastFollowRequest.value);%0A%09var trackingEvents = %5B%7Btype: 0, display: %22Followers%22, eventId: %22channel-followed%22, resourceUrl: %22/channels/%7B%7Busername%7D%7D/follows%22,%0A%09 %09%09%09%09%09%09%09%09%09%09%09%09offset: nodecg.Replicant('followOffset', %7BdefaultValue: 0%7D), lastObserved: nodecg.Replicant('lastFollowRequest', %7BdefaultValue: %221970-01-01T00:00:00.000Z%22%7D)
+trackingEvents = %5B%7Btype: 0, display: %22Followers%22, eventId: %22channel-followed%22, resourceUrl: %22/channels/%7B%7Busername%7D%7D/follows%22
 ,%0A%09%09
@@ -323,24 +323,24 @@
 lay_name %7D,%0A
-
 %09%09%09%09%09%09%09%09%09%09%09%09
@@ -414,24 +414,58 @@
 follows; %7D,%0A
+%09%09%09%09%09%09%09%09%09%09%09%09%09elementsPerPage: 25,%0A
 %09%09%09%09%09%09%09%09%09%09%09%09
@@ -1738,32 +1738,68 @@
 ent.
-offset
+lastObserved
 .value 
-+
 = 
-1;%0A%09%09%09%09%09
+eventDate.toISOString();%0A%09%09%09%09%7D%0A%0A%09%09%09%09if(
 trac
@@ -1812,39 +1812,40 @@
 ent.lastObserved
-.value 
+Date() !
 = eventDate.toIS
@@ -1843,79 +1843,66 @@
 Date
-.toISOString();
+)
 %0A%09%09%09%09
-%7D
+%7B
 %0A%09%09%09
-%7D)%0A%09%09%09nodecg.log.info(JSON.stringify(response)
+%09%09%09trackingEvent.offset.value += 1;%0A%09%09%09%09%7D%0A%0A%09%09%09%7D
 )%0A%09%09
@@ -2165,18 +2165,45 @@
  limit: 
-25
+trackingEvent.elementsPerPage
 ,%0A%09%09%09%09  

Fix loss of followers