#!/usr/bin/env node
'use strict';

var express = require('express'),
    path = require('path'),
    weatherServiceForecast = require('./backend/weather-service-forecast'),
    config = require('config'),
    logger = require('./backend/logger')(),
    cachedWeatherForecasts = null;

var app = express();

app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, config.get('Deployment.distributionDir'), 'app')));

express.static.mime.define({'application/x-font-ttf': ['ttf']});

app.listen(app.get('port'));
logger.info('Express server listening on port ' + app.get('port'));


updateCachedWeatherForecasts();
setInterval(updateCachedWeatherForecasts, 1000 * 60 * 10);

function updateCachedWeatherForecasts() {
    weatherServiceForecast.getAll().then(function (forecasts) {
        cachedWeatherForecasts = forecasts;
        logger.info('Forecasts retrieved from weather service.');
        logger.debug(cachedWeatherForecasts);
    });
}

function getForecastsAsArray(forecastsObj) {
    var cityIds = Object.keys(forecastsObj);

    return cityIds.map(function (cityId) {
        return {
            location: forecastsObj[cityId].location,
            currentDayForecast: forecastsObj[cityId].currentDayForecast,
            futureForecasts: forecastsObj[cityId].futureForecasts
        };
    });
}


app.get('/api/forecast/location/:cityId', function(request, response) {
    var cityId = request.params.cityId;

    weatherServiceForecast.getForecastForLocation(cityId).then(function (forecast) {
        if (forecast) {
            response.send(forecast);
        } else {
            logger.warn('Lookup of /api/forecast/location/' + cityId + ' failed.');
            response.sendStatus(404);
        }

    }, function (error) {
        logger.error(error);
    });
});

app.get('/api/forecast/predefined/location/:cityId', function(request, response, next) {
    var cityId = request.params.cityId.toLowerCase();   // In case of non-numeric 'cityId' param.

    if (cityId === 'all') {
        response.send(getForecastsAsArray(cachedWeatherForecasts));
        next();

    } else {
        if (!cachedWeatherForecasts[cityId]) {
            logger.warn('Lookup of /api/forecast/predefined/location/' + cityId + ' failed.');
            response.sendStatus(404);
        } else {
            response.send(cachedWeatherForecasts[cityId]);
            next();
        }
    }
});

app.get('/api/forecast/predefined/location/:cityId/:section', function(request, response, next) {
    var cityId = request.params.cityId.toLowerCase(),
        section = request.params.section;

    if (cachedWeatherForecasts[cityId] && cachedWeatherForecasts[cityId][section]) {
        response.send(cachedWeatherForecasts[cityId][section]);
        next();
    } else {
        logger.warn('Lookup of /api/forecast/predefined/location/' + cityId + '/' + section + ' failed.');
        response.sendStatus(404);
    }
});

@@ -1888,34 +1888,28 @@
 st, response
-, next
 ) %7B%0A
+
     var city
@@ -2090,32 +2090,16 @@
 casts));
-%0A        next();
 %0A%0A    %7D 
@@ -2365,28 +2365,8 @@
 %5D);%0A
-            next();%0A
     
@@ -2474,14 +2474,8 @@
 onse
-, next
 ) %7B%0A
@@ -2652,24 +2652,24 @@
 section%5D) %7B%0A
+
         resp
@@ -2724,24 +2724,8 @@
 %5D);%0A
-        next();%0A
     

Remove next() calls in server.js middleware:s not needed.