/**
 Copyright 2014 AlchemyAPI

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

var express = require('express');
var consolidate = require('consolidate');

var app = express();
var server = require('http').createServer(app);

//Create the AlchemyAPI object
var AlchemyAPI = require('./alchemyapi');
var alchemyapi = new AlchemyAPI();

// all environments
app.engine('dust', consolidate.dust);
app.set('views', __dirname + '/views');
app.set('view engine', 'dust');
app.set('port', process.env.PORT || 3000);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());

// development only
if ('development' == app.get('env')) {
	app.use(express.errorHandler());
}

app.get('/', index);

var port = process.env.PORT || 3000;
server.listen(port, function() {
	console.log('Express server listening on port ' + port);
	console.log('To view the example, point your favorite browser to: localhost:3000');
});

var demo_text = 'Yesterday dumb Bob destroyed my fancy iPhone in beautiful Denver, Colorado. I guess I will have to head over to the Apple Store and buy a new one.';
var demo_url = 'http://www.npr.org/2013/11/26/247336038/dont-stuff-the-turkey-and-other-tips-from-americas-test-kitchen';
var demo_html = '<html><head><title>Node.js Demo | AlchemyAPI</title></head><body><h1>Did you know that AlchemyAPI works on HTML?</h1><p>Well, you do now.</p></body></html>';

function example(req, res) {
	var output = {};

	//Start the analysis chain
	entities(req, res, output);
}

function entities(req, res, output) {
	alchemyapi.entities('text', demo_text, {
		'sentiment' : 1
	}, function(response) {
		output['entities'] = {
			text : demo_text,
			response : JSON.stringify(response, null, 4),
			results : response['entities']
		};
	});
}
