text: 'use strict'
  
  var Config = require('../config')
  var FB = require('../connectors/facebook')
  var Wit = require('node-wit').Wit
  var request = require('request')
  var Promise = require('promise')
  var HttpsProxyAgent = require('https-proxy-agent')
  var proxy = 'http://127.0.0.1:3128'
  var agent = new HttpsProxyAgent(proxy);
  
  var firstEntityValue = function (entities, entity) {
  	var val = entities && entities[entity] &&
  		Array.isArray(entities[entity]) &&
  		entities[entity].length > 0 &&
  		entities[entity][0].value
  
  	if (!val) {
  		return null
  	}
  	return typeof val === 'object' ? val.value : val
  }
  
  
  var actions = {
  	say (sessionId, context, message, cb) {
  		// Bot testing mode, run cb() and return
  		if (require.main === module) {
  			cb()
  			return
  		}
  
  		console.log('WIT WANTS TO TALK TO:', context._fbid_)
  		console.log('WIT HAS SOMETHING TO SAY:', message)
  		console.log('WIT HAS A CONTEXT:', context)
  
  		if (checkURL(message)) {
  			FB.newMessage(context._fbid_, message, true)
  		} else {
  			FB.newMessage(context._fbid_, message)
  		}
  
  		
  		cb()
  		
  	},
  
  	merge(sessionId, context, entities, message, cb) {
  		// Reset the weather story
  		delete context.forecast
  
  		// Retrive the location entity and store it in the context field
  		var loc = firstEntityValue(entities, 'location')
  		if (loc) {
  			context.loc = loc
  		}
  
  		// Reset the cutepics story
  		delete context.pics
  
  		// Retrieve the category
  		var category = firstEntityValue(entities, 'category')
  		if (category) {
  			context.cat = category
  		}
  
  		// Retrieve the sentiment
  		var sentiment = firstEntityValue(entities, 'sentiment')
  		if (sentiment) {
  			context.ack = sentiment === 'positive' ? 'Glad your liked it!' : 'Aww, that sucks.'
  		} else {
  			delete context.ack
  		}
  
  		cb(context)
  	},
  
  	error(sessionId, context, error) {
  		console.log(error.message)
  	},
  
  	// list of functions Wit.ai can execute
  	['fetch-weather'](sessionId, context, cb) {
  		// Here we can place an API call to a weather service
  
  		 if (context.loc) {
  		 	getWeather(context.loc)
  		 		.then(function (response) {
  		 			if(response.orderStatus){
  		 				context.forecast = "Order ID:"+ response.orderId + ": " +response.orderStatusDesc
  		 			}else{
  		 				context.forecast = response;
  		 			}
  		 				
  		 			cb(context);
  		 		})
  		 		.catch(function (err) {
  		 			console.log(err)
  		 		})
  		 }
  
  		//context.forecast = 'Sunny'
  
  		//cb(context)
  	},
  
  	['fetch-pics'](sessionId, context, cb) {
  		var wantedPics = allPics[context.cat || 'default']
  		context.pics = wantedPics[Math.floor(Math.random() * wantedPics.length)]
  
  		cb(context)
  	},
  }
  
  // SETUP THE WIT.AI SERVICE
  var getWit = function () {
  	console.log('GRABBING WIT')
  	return new Wit(Config.WIT_TOKEN, actions)
  }
  
  module.exports = {
  	getWit: getWit,
  }
  
  // BOT TESTING MODE
  if (require.main === module) {
  	console.log('Bot testing mode!')
  	var client = getWit()
  	client.interactive()
  }
  
  // GET WEATHER FROM API
  var getWeather = function (location) {
  	return new Promise(function (resolve, reject) {
  		var d = new Date();
  		var n = d.getTime();
  		//var url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22'+ location +'%22)&format=json&t='+n;
  		var url = 'http://dlcenovap01.nonprod.avaya.com/dmp2/orderDetails?orderId='+location+'&'+n;
  		
  		var params = {
  			'uri': url,
      		'method': "GET",
      		'headers': {
          		'content-type': 'application/x-www-form-urlencoded'
      		},
      		'agent': agent,
  		};
  		
  		request(params, function (error, response, body) {
  			if(!error)
  		    	return resolve(body)
  		    else
  		    	return resolve(url);
  			})
  	})
  }
  
  // CHECK IF URL IS AN IMAGE FILE
  var checkURL = function (url) {
      return(url.match(/\.(jpeg|jpg|gif|png)$/) != null);
  }
  
  // LIST OF ALL PICS
  var allPics = {
    corgis: [
      'http://i.imgur.com/uYyICl0.jpeg',
      'http://i.imgur.com/useIJl6.jpeg',
      'http://i.imgur.com/LD242xr.jpeg',
      'http://i.imgur.com/Q7vn2vS.jpeg',
      'http://i.imgur.com/ZTmF9jm.jpeg',
      'http://i.imgur.com/jJlWH6x.jpeg',
  		'http://i.imgur.com/ZYUakqg.jpeg',
  		'http://i.imgur.com/RxoU9o9.jpeg',
    ],
    racoons: [
      'http://i.imgur.com/zCC3npm.jpeg',
      'http://i.imgur.com/OvxavBY.jpeg',
      'http://i.imgur.com/Z6oAGRu.jpeg',
  		'http://i.imgur.com/uAlg8Hl.jpeg',
  		'http://i.imgur.com/q0O0xYm.jpeg',
  		'http://i.imgur.com/BrhxR5a.jpeg',
  		'http://i.imgur.com/05hlAWU.jpeg',
  		'http://i.imgur.com/HAeMnSq.jpeg',
    ],
    default: [
      'http://blog.uprinting.com/wp-content/uploads/2011/09/Cute-Baby-Pictures-29.jpg',
    ],
  };
  