/**
 * Copyright 20016 IBM Corp.
 *
 * 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.
 **/

module.exports = function (RED) {

  const SERVICE_IDENTIFIER = 'discovery';
  var fs = require('fs'),
    temp = require('temp'),
    fileType = require('file-type'),
    isDocx = require('is-docx'),
    pkg = require('../../package.json'),
    discoveryutils = require('./discovery-utils'),
    DiscoveryV1 = require('watson-developer-cloud/discovery/v1'),
    serviceutils = require('../../utilities/service-utils'),
    payloadutils = require('../../utilities/payload-utils'),
    dservice = serviceutils.getServiceCreds(SERVICE_IDENTIFIER),
    username = null,
    password = null,
    sUsername = null,
    sPassword = null,
    endpoint = '',
    sEndpoint = 'https://gateway.watsonplatform.net/discovery/api';

  temp.track();

  function initialCheck(u, p) {
    var message = '';
    if (!u || !p) {
      message = 'Missing Watson Discovery service credentials';
    }
    if (message) {
      return Promise.reject(message);
    }
    return Promise.resolve();
  }

  function checkParams(params){
    var response = discoveryutils.paramEnvCheck(params) +
           discoveryutils.paramCollectionCheck(params);
    if (response) {
      return Promise.reject(response);
    } else {
      return Promise.resolve();
    }
  }

  function verifyPayload(msg) {
    if (!msg.payload) {
      return Promise.reject('Missing property: msg.payload');
    } else if (msg.payload instanceof Buffer) {
      return Promise.resolve();
    } else {
      return Promise.reject('msg.payload should be a data buffer');
    }
  }

  function determinSuffix(msg) {
    var ext = '';
      ft = fileType(msg.payload);

    if (ft && ft.ext) {
      ext = '.' + ft.ext;
    }
    if (isDocx(msg.payload)) {
      ext = '.docx';
    }

    return Promise.resolve(ext);
  }

  function openTheFile(suffix) {
    var p = new Promise(function resolver(resolve, reject){
      var options = {};
      if (suffix) {
        options.suffix = suffix;
      }
      temp.open(options, function(err, info) {
        if (err) {
          reject('Error receiving the data buffer');
        } else {
          resolve(info);
        }
      });
    });
    return p;
  }

  function syncTheFile(info, msg) {
    var p = new Promise(function resolver(resolve, reject){
      fs.writeFile(info.path, msg.payload, function(err) {
        if (err) {
          reject('Error processing pdf buffer');
        }
        resolve();
      });
    });
    return p;
  }

  function createStream(info) {
    //var theStream = fs.createReadStream(info.path, 'utf8');
    var theStream = fs.readFileSync(info.path, 'utf8');
    return Promise.resolve(theStream);
  }

  function execute(params, msg) {
    var p = new Promise(function resolver(resolve, reject) {
      var discovery = null, p = null,
        serviceSettings = {
          username: username,
          password: password,
          version_date: '2017-09-01',
          headers: {
            'User-Agent': pkg.name + '-' + pkg.version
          }
        };

      if (endpoint) {
        serviceSettings.url = endpoint;
      }

      discovery = new DiscoveryV1(serviceSettings);

      discovery.addDocument(params, function (err, response) {
        if (err) {
          reject(err);
        } else {
          msg.document = response.document ? response.document : response;
          resolve();
        }
      });

    });
    return p;
  }

  if (dservice) {
    sUsername = dservice.username;
    sPassword = dservice.password;
    sEndpoint = dservice.url;
  }

  RED.httpAdmin.get('/watson-discovery-docs/vcap', function (req, res) {
    res.json(serviceutils.checkServiceBound(SERVICE_IDENTIFIER));
  });


  function Node (config) {
    var node = this;
    RED.nodes.createNode(this, config);

    this.on('input', function (msg) {
      var message = '',
        params = {};

      username = sUsername || this.credentials.username;
      password = sPassword || this.credentials.password;

      endpoint = sEndpoint;
      if ((!config['default-endpoint']) && config['service-endpoint']) {
        endpoint = config['service-endpoint'];
      }

      node.status({});
      initialCheck(username, password)
        .then(function(){
          return verifyPayload(msg);
        })
        .then(function(){
          params = discoveryutils.buildParams(msg, config);
          return checkParams(params);
        })
        .then(function(){
          return determinSuffix(msg);
        })
        .then(function(suffix) {
          node.status({ fill: 'blue', shape: 'dot', text: 'reading' });
          return openTheFile(suffix);
        })
        .then(function(info){
          fileInfo = info;
          return syncTheFile(fileInfo, msg);
        })
        .then(function(){
          return createStream(fileInfo);
        })
        .then(function(theStream){
          params.file = theStream;
          params.metadata = {'content-type': 'application/msword'}
          node.status({ fill: 'blue', shape: 'dot', text: 'processing' });
          return execute(params, msg);
        })
        .then(function(){
          temp.cleanup();
          node.status({});
          node.send(msg);
        })
        .catch(function(err){
          temp.cleanup();
          payloadutils.reportError(node,msg,err);
          node.send(msg);
        });
    });
  }

  RED.nodes.registerType('watson-discovery-v1-document-loader', Node, {
    credentials: {
      username: {type:'text'},
      password: {type:'password'}
    }
  });
};

@@ -2145,24 +2145,111 @@
 ffix(msg) %7B%0A
+    // Let's assume that if we can't determine the suffix that%0A    // its a word doc. %0A
     var ext 
@@ -2247,24 +2247,28 @@
  var ext = '
+.doc
 ';%0A      ft 
@@ -4481,24 +4481,49 @@
 ssage = '',%0A
+        fileSuffix = '',%0A
         para
@@ -5175,32 +5175,63 @@
 ction(suffix) %7B%0A
+          fileSuffix = suffix;%0A
           node.s
@@ -5567,24 +5567,26 @@
 %7B%0A          
+//
 params.file 
@@ -5604,24 +5604,204 @@
 ;%0A          
+var fname = 'temp' + fileSuffix;%0A          params.file = %7B%0A            value: theStream,%0A            options: %7B%0A            filename: fname%0A            %7D%0A          %7D;%0A%0A          //
 params.metad

Create Shell for execute