/**
 * @file HTTP server REQUEST handler
 * @author zdying
 */
'use strict';

var proxyFlow = require('../../proxy');
var utils = require('../../../helpers/utils');

module.exports = function (req, res) {
  var hiproxy = this;
  var ctx = {
    req: req,
    res: res,
    // hiproxy: hiproxy,
    logger: this.logger
  };

  req.requestId = utils.randomId();
  req._startTime = Date.now();

  /* Emitted each time there is a request.
   * @event ProxyServer#request
   * @property {http.IncomingMessage} request request object
   * @property {http.ServerResponse} response response object
   */
  this.emit('request', req, res);

  var oldWrite = res.write;
  var oldEnd = res.end;
  var isString = false;
  var body = [];
  console.log('init body:', Array.isArray(body), body);
  var collectChunk = function (chunk) {
    if (!chunk) {
      return;
    }

    if (typeof chunk === 'string') {
      isString = true;
    }

    body.push(chunk);
  };

  res.write = function (chunk, encoding) {
    console.log('write body:', Array.isArray(body), body);

    collectChunk(chunk);
    /**
     * Emitted whenever the response stream received some chunk of data.
     * @event ProxyServer#data
     * @property {Buffer} data response data
     * @property {http.IncomingMessage} request request object
     * @property {http.ServerResponse} response response object
     */
    hiproxy.emit('data', chunk, req, res, ctx.proxy, encoding);
    // console.log('on data获取代理信息:', ctx.proxy);
  };

  res.end = function (chunk, encoding) {
    console.log('end body:', Array.isArray(body), body);
    collectChunk(chunk);
    body = isString ? body.join('') : Buffer.concat(body);

    /**
     * Emitted when a response is end. This event is emitted only once.
     * @event ProxyServer#response
     * @property {http.IncomingMessage} request request object
     * @property {http.ServerResponse} response response object
     */
    hiproxy.emit('response', req, res, ctx.proxy, encoding);

    // console.log('on response获取代理信息:', ctx.proxy);

    // oldEnd会再次调用write，所以这里要还原write方法
    res.write = oldWrite;
    oldEnd.call(res, body);
  };

  proxyFlow.run(ctx, null, this);
};

@@ -721,64 +721,8 @@
 %5B%5D;%0A
-  console.log('init body:', Array.isArray(body), body);%0A
   va
@@ -938,68 +938,8 @@
 ) %7B%0A
-    console.log('write body:', Array.isArray(body), body);%0A%0A
     
@@ -1321,158 +1321,52 @@
 ;%0A  
-  // console.log('on data%E8%8E%B7%E5%8F%96%E4%BB%A3%E7%90%86%E4%BF%A1%E6%81%AF:', ctx.proxy);%0A  %7D;%0A%0A  res.end = function (chunk, encoding) %7B%0A    console.log('end body:', Array.isArray(body), body);
+%7D;%0A%0A  res.end = function (chunk, encoding) %7B
 %0A   
@@ -1763,62 +1763,8 @@
 );%0A%0A
-    // console.log('on response%E8%8E%B7%E5%8F%96%E4%BB%A3%E7%90%86%E4%BF%A1%E6%81%AF:', ctx.proxy);%0A%0A
     

delete test console