var http = require('http'), request = require('request'), cheerio = require('cheerio');

var eventTimer = 0;
var eventCache = {
    last: 0,
    data: {},
    expire: ((60 * 1000) * 60) * 12,
    cinema: {}
};
// 'cwr':{data: {}, last:0};

var cinemas = [{id: 'cwr', url: 'https://film.list.co.uk/cinema/14982-cineworld-renfrew-street/'},
    {id: 'gsc', url: 'https://film.list.co.uk/cinema/13590-cineworld-glasgow-science-centre/'},
    {id: 'pho', url: 'https://film.list.co.uk/cinema/12500-showcase-cinema-paisley/'}];

module.exports = {
    getEvents: function (req, res) {

        console.log('Getting events...');
        var j = [], url = 'https://www.list.co.uk/events/days-out/when:this%20weekend/location:Dumbarton(55.9460,-4.5556)/distance:20/';

        var now = new Date();

        if ((now - eventCache.last) > eventCache.expire) {
            request(url, function (err, resp, body) {
                if (err)
                    throw err;
                $ = cheerio.load(body);
                // console.log($);
                // TODO: scraping goes here!

                $('.resultsRow').each(function (div) {
                    var item = {};
                    var eventSummary = $(this).find('.eventSummary').first();
                    var byDate = $(this).find('.byDate').first();

                    var title = eventSummary.find('.head').first();
                    var description = eventSummary.find('P').first();
                    var link = ' https://www.list.co.uk' + eventSummary.find('A').first().attr('href');

                    var price = byDate.find('.price').first();
                    var dt = byDate.find('.dtstart').first().attr('title');

                    item.title = title.text();
                    item.description = description.text();
                    item.link = link;
                    item.price = price.text();
                    item.date = dt;

                    j.push(item);
                });

                eventCache.last = now;
                eventCache.data = j;

                res.render('pages/events', eventCache);

            }, function(error, response, body) {
                                if(response.statusCode !== 200) {
                                    logger.error(response.statusCode);
                                    logger.error(body);
                                }
                            });
        } else {
            console.log('Using event cache...');

            res.render('pages/events', eventCache);
        }

    },
    doGetCinema: function (id) {
        var cinemaID = cinemas[id].id;
        var url = cinemas[id].url;
        var thisCinema = eventCache[cinemaID] || {data: {}, last: 0};
        console.log(cinemaID);
        console.log(url);
        var j = [];

        var now = new Date();

        if ((now - thisCinema.last) > eventCache.expire) {
            request(url, function (err, resp, body) {
                console.log('Working');
                if (err)
                    throw err;
                $ = cheerio.load(body);

                $('.byEvent').each(function (div) {
                    var item = {};
                    var title = $(this).find('H4').first();
                    var eventSummary = $(this).find('.eventSummary').first();

                    var description = eventSummary.find('P').first();
                    var link = ' https://www.list.co.uk' + eventSummary.find('A').first().attr('href');

                    item.title = title.text();
                    item.description = description.text();
                    item.link = link;

                    j.push(item);
                });

                thisCinema.last = now;
                thisCinema.data = j;

                eventCache[cinemaID] = thisCinema;
                console.log('returning');

                return thisCinema;

            }, function(error, response, body) {
                                if(response.statusCode !== 200) {
                                    console.error(response.statusCode);
                                    console.error(body);
                                }
                            });
        } else {
            console.log('Using event cache...');
            return thisCinema;
        }
    },
    getCinema: function (req, res) {
        var id = parseInt(req.params.id);
        console.log('Getting cinema: ' +id);

        var output = module.exports.doGetCinema(id);
        res.render('pages/cinema', output);
    },
    preLoad: function () {
        var output = module.exports.doGetCinema(0);
        output = module.exports.doGetCinema(1);
        output = module.exports.doGetCinema(2);

        setTimeout(function () {
            module.exports.preLoad();
        }, eventCache.expire);
    }
};

setTimeout(function () {
    console.log('Pre loading cinemas...');
    module.exports.preLoad();
}, 10000);


