var express = require('express');
var app = express();
var url = require('url');
var request = require('request');
var config = require( './config.json' );

var format = ".json";
var apikey = config.apikey;

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

//use port is set in the environment variable, or 9001 if it isn’t set.
app.set( 'port', ( config.port || 9001 ) );

//for testing that the app is running
app.get('/', function(req, res){
  var msg = 'Running!';

  if ( ! apikey.length ) {
    msg += ' Please set the API key.'
  }

  res.send( msg );
});

//app.post is triggered when a POST request is sent to the URL ‘/post’
app.post('/post', function(req, res){
  // Can't proceed without an API key
  if ( ! apikey.length ) {
    return res.sendStatus( 500 );
  }

  // Don't bother if there's nothing to look up
  if ( 'undefined' === typeof req.body.text || ! req.body.text.length ) {
	  return res.sendStatus( 400 );
  }

  var parsed_url = url.format({
    pathname: 'http://api.wunderground.com/api/' + apikey + '/conditions/q/' + req.body.text + format,
  });

  console.log(parsed_url);

  request(parsed_url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var data = JSON.parse(body);
      var temperature = data.current_observation.temperature_string;
      var weatherCondition = data.current_observation.weather;
      var icon_url = data.current_observation.icon_url;
      var location = data.current_observation.display_location.full;

      var body = {
        response_type: "in_channel",
        "attachments": [
          {
            "text": "Location: " + location + "\n"
                  + "Temperature: " + temperature + "\n"
                  + "Condition: " + weatherCondition,
            "image_url": icon_url,
          }
        ]
      };

      res.send(body);
    }
  });
});

//tells Node which port to listen on
app.listen(app.get('port'), function() {
  console.log('Weather on the Slash is running on port', app.get('port'));
});