/**
 * Copyright 2014 IBM Corp. All Rights Reserved.
 *
 * 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.
 */

'use strict';

// Module dependencies
var request = require('superagent');
var express    = require('express'),
  errorhandler = require('errorhandler'),
  bodyParser   = require('body-parser');

module.exports = function (app, options) {

  // Configure Express
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(bodyParser.json());

  // Setup static public directory
  app.use(express.static(__dirname + '/../public'));
  app.set('view engine', 'jade');
  app.set('views', __dirname + '/../views');

  // Add error handling in dev
  if (!process.env.VCAP_SERVICES) {
    app.use(errorhandler());
  }

  // render index page
  app.get('/', function(req, res) {
    // textToSpeech.voices({}, function(err, data) {
    //   if (!err) {
    //     console.log('data', data);
    //     res.render('index', {result: 'success', data: data});
    //   } else {
    //     console.log('err', err);
    //     res.render('index', {result: 'error', data: null});
    //   }
    // })
    res.render('index');
  });

  app.post('/synthesize', function(req, res) {
    var req = request
      .get('https://stream.watsonplatform.net/text-to-speech-beta/api/v1/synthesize')
      .set('Accept', 'audio/ogg;codecs=opus')
      .auth(options.username, options.password)
      .query(req.body)
      .end(function(err, data) {
        console.log('data', data);
      });

    // params['Accept'] = 'audio/ogg;codecs=opus';
    // var transcript = textToSpeech.synthesize(params);
    // transcript.on('response', function(response) {
    //   response.headers['Mime-Type'] = 'audio/ogg; codecs=opus';
    //   if (req.query.download) {
    //     response.headers['content-disposition'] = 'attachment; filename=transcript.ogg';
    //   }
    // });
    // res.headers['Content-Type'] = 'audio/ogg; codecs=opus';
    req.pipe(res);
  });

  // app.get('/synthesize', function(req, res) {
  //   var transcript = textToSpeech.synthesize(req.query);
  //   transcript.on('response', function(response) {
  //     response.headers['Mime-Type'] = 'audio/ogg; codecs=opus';
  //     if (req.query.download) {
  //       response.headers['content-disposition'] = 'attachment; filename=transcript.ogg';
  //     }
  //   });
  //   transcript.pipe(res);
  // });


};

@@ -838,23 +838,28 @@
 n (app, 
-options
+textToSpeech
 ) %7B%0A%0A  /
@@ -1254,16 +1254,35 @@
 dex page
+ with voice options
 %0A  app.g
@@ -1314,18 +1314,62 @@
 ) %7B%0A    
-//
+console.log('textToSpeech', textToSpeech);%0A   
  textToS
@@ -1406,27 +1406,24 @@
  data) %7B%0A   
- //
    if (!err)
@@ -1424,27 +1424,24 @@
 (!err) %7B%0A   
- //
      console
@@ -1459,27 +1459,24 @@
 , data);%0A   
- //
      res.ren
@@ -1526,21 +1526,18 @@
 %7D);%0A    
-//
   
- 
 %7D else %7B
@@ -1536,27 +1536,24 @@
 %7D else %7B%0A   
- //
      console
@@ -1569,27 +1569,24 @@
 ', err);%0A   
- //
      res.ren
@@ -1629,35 +1629,32 @@
  null%7D);%0A   
- //
    %7D%0A   
  // %7D)%0A    r
@@ -1645,40 +1645,12 @@
 %0A   
- //
  %7D)%0A
-    res.render('index');%0A
   %7D)
@@ -1706,366 +1706,8 @@
 %0A   
- var req = request%0A      .get('https://stream.watsonplatform.net/text-to-speech-beta/api/v1/synthesize')%0A      .set('Accept', 'audio/ogg;codecs=opus')%0A      .auth(options.username, options.password)%0A      .query(req.body)%0A      .end(function(err, data) %7B%0A        console.log('data', data);%0A      %7D);%0A%0A    // params%5B'Accept'%5D = 'audio/ogg;codecs=opus';%0A    //
  var
@@ -1748,23 +1748,22 @@
 ize(
-params
+req.body
 );%0A   
- //
  tra
@@ -1809,19 +1809,16 @@
 e) %7B%0A   
- //
    respo
@@ -1834,520 +1834,52 @@
 rs%5B'
-Mime-Type'%5D = 'audio/ogg; codecs=opus';%0A    //   if (req.query.download) %7B%0A    //     response.headers%5B'content-disposition'%5D = 'attachment; filename=transcript.ogg';%0A    //   %7D%0A    // %7D);%0A    // res.headers%5B'Content-Type'%5D = 'audio/ogg; codecs=opus';%0A    req.pipe(res);%0A  %7D);%0A%0A  // app.get('/synthesize', function(req, res) %7B%0A  //   var transcript = textToSpeech.synthesize(req.query);%0A  //   transcript.on('response', function(response) %7B%0A  //     response.headers%5B'Mime-Type'%5D = 'audio/ogg; codecs=opus';%0A  //
+Content-Type'%5D = 'audio/ogg; codecs=opus';%0A 
     
@@ -1906,19 +1906,16 @@
 oad) %7B%0A 
- //
        r
@@ -1999,19 +1999,16 @@
 ';%0A 
- //
      %7D%0A 
  // 
@@ -2007,19 +2007,16 @@
  %7D%0A 
- //
    %7D);%0A 
  // 
@@ -2011,19 +2011,16 @@
    %7D);%0A 
- //
    trans
@@ -2041,17 +2041,12 @@
 );%0A 
- //
  %7D);%0A%0A
-%0A
 %7D;
-%0A

Remove GET request handler for synthesis