var debug = require('debug')('revo:router'),
	express = require('express'),
    router = express.Router(),
	_ = require('underscore');

const ERROR = require('chalk').red.bold;
const WARN = require('chalk').yellow.bold;
const INFO = require('chalk').green;

module.exports = router;

//todo: refactor this into a revo component
process.on('http.route:create', function(payload){
	debug('registered new dynamic route:', payload.path);
	if(!!payload.path) {
		router.route(payload.path).get(function (req, res, next) {
			if(!!payload.trigger) {
				//todo: add x-headers & query data
				//todo: move this block into sendFile's callback
				var reqData = req.params
				setTimeout(function(){
					process.emit(payload.trigger, reqData);
				}, 500);
			}
			if(payload.webpage) {
// 				var fullPath = [__dirname, '..', 'public', (payload.webpage=='/')?'index.html':payload.webpage].join('/');
// 				fullPath = 'index.html';
// 				var options = {
// 					root: __dirname + '/public/',
// 					dotfiles: 'deny',
// 					headers: {
// 					    'x-timestamp': Date.now(),
// 					    'x-sent': true
// 					}
// 				};				
// 				res.sendFile(fullPath, options, function(err){
// 					if(err) debug(ERROR(err));
					// process.emit(payload.trigger, reqData);
// 				})
				res.redirect(payload.webpage);
			}
			else res.end();
			next();
		});
	}
})

// //TODO
// router.use('test', function (req, res, next) {
// 	debug('intercepting component load:', req.params.component)	
// 	// console.log('Time: ', Date.now());
// 	next();
// });

// router.get('/components', function (req, res, next) {
// 	next();
// });


@@ -541,47 +541,8 @@
 ) %7B%0A
-%09%09%09%09//todo: add x-headers & query data%0A
 %09%09%09%09
@@ -607,16 +607,24 @@
 eqData =
+ %7B path:
  req.par
@@ -626,16 +626,156 @@
 q.params
+%5B0%5D, data: %7B%7D %7D;%0A%09%09%09%09Object.keys(req.query).forEach(function(key)%7B%0A%09%09%09%09%09reqData.data%5Bkey%5D = req.query%5Bkey%5D;%0A%09%09%09%09%7D)%0A%09%09%09%09//todo: add x-headers
 %0A%09%09%09%09set

structure change of the route event payload