/**
 * Created by Martin on 31/03/2016.
 */
var Forecast = require('forecast.io');
var STRING = require('string');
var logger = require('log4js').getLogger();


var forecastOptions = {
    APIKey: '0657dc0d81c037cbc89ca88e383b6bbf', units: 'uk2'
};

module.exports = {
    newDoGetWeather: function() {
        'use strict';
        return new Promise(function(resolve, reject) {
            logger.info('New Retrieving weather..');
            var j = {};
            var forecast = new Forecast(forecastOptions);
            forecast.get(55.8582846,
                -4.2593033,
                {units: 'uk2'},
                function(err, res, data) {
                    if (err) {
                      //  logger.error(err);
                        return reject(err);
                       // throw err;
                    }

                    var tempMin = parseInt(data.daily.data[0].temperatureMin);
                    var tempMax = parseInt(data.daily.data[0].temperatureMax);

                    j.currently = data.currently.summary;
                    j.today = data.daily.data[0].summary;
                    j.later = data.daily.summary;
                    j.alerts = data.alerts || {};
                    j.data = data;

                    var fs = STRING(j.currently).endsWith('.') ? '' : '.';
                    if (tempMax == tempMin) {
                        j.currently += fs + ' Around ' + tempMin.toString() + ' degrees.';
                    }
                    else {
                        j.currently += fs + ' Around ' + tempMin.toString() + ' to ' + tempMax.toString() + ' degrees.';
                    }
                   // logger.debug(j);
                    return resolve(j);
                });
        });
    }


};