"use strict";

import Restify from 'restify';
import Helpers from './libs/route_helpers.js';

let server = Restify.createServer();

server.use(restify.queryParser());
server.use(restify.gzipResponse());
server.use(restify.bodyParser());

/*	
server.use(function(req, repl, next) {
	console.log(req.headers, req.url);
	return next();
});
*/

server.post('/actions/v0/:id/voice.xml', postHandlerVoice);
server.post('/actions/v0/:id/status', postHandlerStatus);
server.post('/actions/v0/:id/action', postHandlerAction);
server.post('/actions/v0/:id/action/:index', postHandlerAction);
server.post('/actions/v0/:id/dequeue', postHandlerDequeue);
server.post('/actions/v0/:id/wait/:index', postHandlerWait);

server.post('/actions/v0/:id/sms.xml', postHandlerSms);

server.listen(8000, function() {
	console.log('Started Call Router API server ', new Date());
	
	csp.go(function*() {
		console.log('Starting outbound channel loop');
		var val = yield csp.take(processor.outbound);
		while (val !== csp.CLOSED) {
			console.log('OUTBOUND: ', Object.keys(val))
			if (val != undefined) {
				val.req.reply.header('content-type', 'application/xml');
				if (val.body instanceof Error) val.req.reply.send(403, val.body.message, {'content-type': 'application/xml'});
				else if (val.body === undefined) val.req.reply.send(200);
				else val.req.reply.send(200, val.body, {'content-type': 'application/xml'});
				val.req.reply.end();
				val.req.next();
			}
			val = yield csp.take(processor.outbound);
		}
	});
});
@@ -132,25 +132,25 @@
 %0Aserver.use(
-r
+R
 estify.query
@@ -167,25 +167,25 @@
 %0Aserver.use(
-r
+R
 estify.gzipR
@@ -207,17 +207,17 @@
 ver.use(
-r
+R
 estify.b
@@ -371,24 +371,32 @@
 voice.xml', 
+Helpers.
 postHandlerV
@@ -440,16 +440,24 @@
 tatus', 
+Helpers.
 postHand
@@ -506,16 +506,24 @@
 ction', 
+Helpers.
 postHand
@@ -571,32 +571,40 @@
 action/:index', 
+Helpers.
 postHandlerActio
@@ -646,16 +646,24 @@
 queue', 
+Helpers.
 postHand
@@ -718,16 +718,24 @@
 index', 
+Helpers.
 postHand
@@ -784,16 +784,24 @@
 s.xml', 
+Helpers.
 postHand
@@ -908,656 +908,7 @@
 ));%0A
-%09%0A%09csp.go(function*() %7B%0A%09%09console.log('Starting outbound channel loop');%0A%09%09var val = yield csp.take(processor.outbound);%0A%09%09while (val !== csp.CLOSED) %7B%0A%09%09%09console.log('OUTBOUND: ', Object.keys(val))%0A%09%09%09if (val != undefined) %7B%0A%09%09%09%09val.req.reply.header('content-type', 'application/xml');%0A%09%09%09%09if (val.body instanceof Error) val.req.reply.send(403, val.body.message, %7B'content-type': 'application/xml'%7D);%0A%09%09%09%09else if (val.body === undefined) val.req.reply.send(200);%0A%09%09%09%09else val.req.reply.send(200, val.body, %7B'content-type': 'application/xml'%7D);%0A%09%09%09%09val.req.reply.end();%0A%09%09%09%09val.req.next();%0A%09%09%09%7D%0A%09%09%09val = yield csp.take(processor.outbound);%0A%09%09%7D%0A%09%7D);%0A
 %7D);

call router api server