'use strict';

const request = require('request');
const cheerio = require('cheerio');

let url = 'http://tvschedule.zap2it.com/tvlistings/ZCGrid.do?method=decideFwdForLineup&zipcode=02115&setMyPreference=false&lineupId=MA20483:-&aid=tvschedule';
let $;

module.exports = {
    testReturn: function (callback) {
        callback('Test successful.');
    },

    requestListings: function (callback) {
        request(url, (err, response, body) => {
            if (err && response.statusCode !== 200) {
                throw 'Error with retrieving listings: ' + err;
            }

            callback(body);
        });
    },

    parseCurrentListingsTimes: function (data, callback) {
        $ = cheerio.load(data);
        let times = [];

        // gets the elements with the current times shown and iterates through each - this is a 3-hr range for shows
        $('.zc-tn-c').first().find('.zc-tn-t').each(function (i, elem) {
            // adds the time in the element to the times array
            times[i] = $(this).text();
        });

        callback(times);
    },

    parseCurrentShows: function (data, callback) {
        $ = cheerio.load(data);
        let shows = {};
        let channel, show;

        $('.zc-row').each(function (i, elem) {
            channel = $(this).find('.zc-st').find('.zc-st-c').find('.zc-st-a').text();
            shows[channel] = [];
            $(this).find('.zc-pg').each(function (j, e) {
                show = $(this).find('.zc-pg-t').text();
                shows[channel].push({
                    'show': show,
                    'time': ''
                });
            });
        });

        callback(shows);
    }
};

@@ -734,18 +734,47 @@
 times = 
-%5B%5D
+%7B%7D;%0A        let timeCounter = 0
 ;%0A%0A     
@@ -950,32 +950,327 @@
 ion (i, elem) %7B%0A
+            // splits the text of the time (originally in the form 'X:XX XM', e.g. '4:30 PM')%0A            let timeSplit = $(this).text().split(' ');%0A            let hour = timeSplit%5B0%5D.split(':')%5B0%5D;%0A            let minute = timeSplit%5B0%5D.split(':')%5B1%5D;%0A            let meridiem = timeSplit%5B1%5D;%0A%0A
             // a
@@ -1314,13 +1314,14 @@
 mes 
-array
+object
 %0A   
@@ -1339,27 +1339,249 @@
 mes%5B
-i%5D = $(this).text()
+timeCounter%5D = %7B%0A                'hour': hour,%0A                'minute': minute,%0A                'meridiem': meridiem%0A            %7D;%0A%0A            // increments time counter to assign next value in the time object%0A            timeCounter += 1
 ;%0A  
@@ -1699,32 +1699,94 @@
 rio.load(data);%0A
+        this.parseCurrentListingsTimes(data, (times) =%3E %7B%0A    
         let show
@@ -1785,32 +1785,36 @@
 let shows = %7B%7D;%0A
+    
         let chan
@@ -1822,19 +1822,40 @@
 el, show
-;%0A%0A
+, time, timeIndex;%0A%0A    
         
@@ -1885,32 +1885,36 @@
 ion (i, elem) %7B%0A
+    
             chan
@@ -1988,32 +1988,36 @@
 ();%0A            
+    
 shows%5Bchannel%5D =
@@ -2033,16 +2033,20 @@
         
+   
+ 
 $(this).
@@ -2095,24 +2095,28 @@
             
+    
 show = $(thi
@@ -2135,32 +2135,99 @@
 -pg-t').text();%0A
+%0A
+                    time = determineShowTime($(this), times);%0A    
                 
@@ -2248,16 +2248,20 @@
 .push(%7B%0A
+    
         
@@ -2310,16 +2310,20 @@
     
+    
 'time': 
 ''%0A 
@@ -2322,11 +2322,17 @@
 e': 
-''%0A
+time%0A    
     
@@ -2351,32 +2351,36 @@
 %7D);%0A            
+    
 %7D);%0A        %7D);%0A
@@ -2367,37 +2367,45 @@
     %7D);%0A        
+  
+  
 %7D);%0A%0A
+    
         callback
@@ -2421,9 +2421,630 @@
     
-%7D%0A%7D;
+    %7D);%0A    %7D%0A%7D;%0A%0Afunction determineShowTime(show, times) %7B%0A    let showStyle = show.attr('style');%0A    let blockSize = 150;%0A    let totalSize = blockSize * 6;%0A    let showTime = '';%0A%0A    if (showStyle !== undefined) %7B%0A        let widthIndex = showStyle.indexOf('width:');%0A        let pxIndex = showStyle.indexOf('px');%0A        // value of the width is between 'width:' and 'px' in the style string%0A        let widthValue = showStyle.substring(widthIndex + 'width:'.length, pxIndex);%0A        showTime = times%5BMath.floor(parseInt(widthValue) / blockSize)%5D;%0A    %7D else %7B%0A        showTime = 'null';%0A    %7D%0A%0A    return showTime;%0A%7D
 %0A

Change parsed times data object