'use strict';

const router = require( 'express' ).Router();
const Busboy = require( 'busboy' );
const config = require( '../../config/config' );
// const submission = require( '../models/submission' );
const debug = require( 'debug' )( 'submission controller' );
module.exports = ( app ) => {
    app.use( '/submission', router );
    app.use( '/fieldsubmission', router );
};

router
    .all( '*', ( req, res, next ) => {
        // console.log( 'submission request Authorization headers', req.headers.authorization );
        res.set( {
            'X-OpenRosa-Version': '1.0',
            'X-OpenRosa-Accept-Content-Length': config[ 'max submission size' ] || 50 * 1024 * 1024
        } );

        //if ( !req.headers[ 'x-openrosa-version' ] || req.headers[ 'x-openrosa-version' ] !== '1.0' ) {
        //    error = new Error( 'This server only supports OpenRosa 1.0 clients' );
        //    error.status = 400;
        //    next( error );
        //} else {
        next();
        //}
    } )
    .head( '/', ( req, res, next ) => {
        console.log( 'head' );
        res.status( 204 );
        res.end();
    } )
    .post( '/', ( req, res, next ) => {
        const busboy = new Busboy( {
            headers: req.headers
        } );

        debug( 'submission coming in from ', req.url, req.headers );

        busboy.on( 'file', ( fieldname, stream, filename, transferEncoding, mimeType ) => {
            //if ( fieldname === 'xml_submission_file' ) { }
            debug( 'file:', fieldname, filename, mimeType, stream );
        } );

        // What to do? 
        // Save as temporary files and pass the paths to the submission model?
        // Pass XML submission as string?
        // What would work the best under heavy load?

        busboy.on( 'finish', function() {
            /*return submission.set( xmlData, files)
            .then( function( response ) {
                res.set( {
                    'Content-Type': 'text/xml'
                } );
                res.status(response.status)
                res.send( response.xml );
            } )
            .catch( next );*/
        } );

        setTimeout( function() {
            console.log( 'going to return OK' );
            res.status( 201 ).send( '<ok/>' );
        }, 0.2 * 1000 );
        //req.pipe( busboy );

    } )
    .put( '/', function( req, res, next ) {
        console.log( 'put' );
        //const busboy = new Busboy( {   headers: req.headers} );

        setTimeout( function() {
            console.log( 'going to return OK' );
            res.status( 201 ).send( '<ok/>' );
        }, 1.5 * 1000 );
        //req.pipe( busboy );

    } ).all( '*', function( req, res, next ) {
        res.status( 405 );
        res.send( 'you is bad!' );
    } );
@@ -60,17 +60,17 @@
 ;%0Aconst 
-B
+b
 usboy = 
@@ -961,16 +961,17 @@
  else %7B%0A
+%0A
         
@@ -1042,39 +1042,8 @@
 %3E %7B%0A
-        console.log( 'head' );%0A
     
@@ -1143,37 +1143,29 @@
      const b
-usboy = new B
+b = b
 usboy( %7B%0A   
@@ -1220,84 +1220,10 @@
     
-debug( 'submission coming in from ', req.url, req.headers );%0A%0A        busboy
+bb
 .on(
@@ -1369,20 +1369,26 @@
         
-debu
+console.lo
 g( 'file
@@ -1428,24 +1428,91 @@
 , stream );%0A
+            // do nothing with stream%0A            stream.resume();%0A
         %7D );
@@ -1712,17 +1712,16 @@
 y load?%0A
-%0A
         
@@ -1725,38 +1725,28 @@
    b
-usboy
+b
 .on( '
-finish', function()
+close', () =%3E
  %7B%0A 
@@ -1757,17 +1757,17 @@
        /
-*
+/
 return s
@@ -1797,16 +1797,82 @@
  files)%0A
+            return Promise.resolve(%7B status: 201, xml: '%3Cok/%3E' %7D)%0A
         
@@ -2056,27 +2056,28 @@
             
-res
+    
 .send( respo
@@ -2134,10 +2134,8 @@
 t );
-*/
 %0A   
@@ -2132,33 +2132,32 @@
 ext );%0A        %7D
- 
 );%0A%0A        setT
@@ -2156,153 +2156,30 @@
     
-setTimeout( function() %7B%0A            console.log( 'going to return OK' );%0A            res.status( 201 ).send( '%3Cok/%3E' );%0A        %7D, 0.2 * 1000 );
+bb.on('error', next);%0A
 %0A   
@@ -2175,34 +2175,32 @@
 next);%0A%0A        
-//
 req.pipe( busboy
@@ -2190,34 +2190,29 @@
  req.pipe( b
-usboy
+b
  );%0A
-%0A
     %7D )%0A    
@@ -2219,16 +2219,17 @@
 .put( '/
+*
 ', funct
@@ -2303,21 +2303,13 @@
 st b
-usboy = new B
+b = b
 usbo
@@ -2485,11 +2485,11 @@
  %7D, 
-1.5
+0.2
  * 1
@@ -2516,21 +2516,17 @@
 .pipe( b
-usboy
+b
  );%0A%0A   
@@ -2529,16 +2529,21 @@
 %0A    %7D )
+%0A    
 .all( '*
@@ -2624,18 +2624,19 @@
 d( 'you 
-is
+are
  bad!' )

allow for better submission testing