/**
 * Created by apizzimenti on 12/29/15.
 */

var http = require('http'),
    https = require('https'),
    chalk = require('chalk'),
    Format = require('./format');

function weatherRequest(location, units, args) {
    var weather_options = {
        host: 'api.forecast.io',
        path: '/forecast/d399f7331297381cd6b95106add0d22d/' + location.lat + ',' + location.long + '?units=' + units.type,
        method: 'GET'
    };

    https.get(weather_options, function(res) {
        var json  = '';
        res
            .on('data', function (chunk) {
                json += chunk;
            })
            .on('end', function() {
                if (args.v || args.verbose) {
                    console.log(chalk.green('✓ got data from weather server'));
                }
                var weather = Format(json, units);
                console.log(weather.currentWeather);
                console.log(weather.tabled());
                process.exit();
            });
    });
}

function address(addr, callback, units, args) {

    var location_options = {
        host: 'maps.googleapis.com',
        path: '/maps/api/geocode/json?address=' + encodeURIComponent(addr.toString()),
        method: 'GET'
    };

    http.get(location_options, function (res) {
        var text = '',
            position = {};
        res
            .on('data', function (chunk) {
                text +=  chunk.toString();
            })
            .on('end', function () {
                var json = JSON.parse(text);
                if (json.status == "OK" && json.results[0]) {
                    if (args.v || args.verbose) {
                        console.log(chalk.green('✓ got geo location server response'));
                        console.log(chalk.green('✓ got location: ') + chalk.bgBlack.white(json.results[0].formatted_address));
                    }
                    position.lat = json.results[0].geometry.location.lat;
                    position.long = json.results[0].geometry.location.lng;
                    callback(position, units, args);
                } else {
                    console.log(chalk.red('✗ address not found'));
                }
            });
    });
}

function automatic(ip, callback, units, args) {

    var location_options = {
        host: 'freegeoip.net',
        path: '/json/' + ip,
        method: 'GET'
    };

    http.get(location_options, function (res) {
        var text = '',
            position = {};
        res
            .on('data', function (chunk) {
                if (args.v || args.verbose) {
                    console.log(chalk.green('✓ got location server response'));
                }
                text +=  chunk.toString();
            })
            .on('end', function () {
                var json = JSON.parse(text),
                location = json.city + ', ' + json.region_name + ', ' + json.country_name;
                position.lat = json.latitude;
                position.long = json.longitude;

                if (args.v || args.verbose) {
                    console.log(chalk.green('✓ got location: ') + chalk.bgBlack.white(location));
                }
                callback(position, units, args);
            });
    });
}

module.exports = {
    weatherRequest: weatherRequest,
    address: address,
    automatic: automatic
};

@@ -2754,32 +2754,58 @@
 , function () %7B%0A
+                try %7B%0A    
                 
@@ -2831,17 +2831,17 @@
 se(text)
-,
+;
 %0A       
@@ -2849,16 +2849,24 @@
         
+    var 
 location
@@ -2940,32 +2940,36 @@
                 
+    
 position.lat = j
@@ -2982,16 +2982,20 @@
 titude;%0A
+    
         
@@ -3043,32 +3043,36 @@
                 
+    
 if (args.v %7C%7C ar
@@ -3077,32 +3077,36 @@
 args.verbose) %7B%0A
+    
                 
@@ -3195,34 +3195,42 @@
                 
+   
+ 
 %7D%0A
+    
                 
@@ -3266,33 +3266,322 @@
 s);%0A            
-%7D
+    %7D catch (e) %7B%0A                    console.log(chalk.red('Sorry, we couldn%5C't parse return data :/%5Cn it%5C's not our fault, it%5C's the geoip server'));%0A                %7D%0A            %7D);%0A    %7D).on('error', function () %7B%0A        console.log(chalk.red('Sorry, we got a socket hangup error :/')
 );%0A    %7D);%0A%7D%0A%0Amo

add error handling for bad http requests to geoip server