/**
 * 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 discoveryutils = require('./discovery-utils'),
    DiscoveryV1 = require('watson-developer-cloud/discovery/v1'),
    serviceutils = require('../../utilities/service-utils'),
    dservice = serviceutils.getServiceCreds(SERVICE_IDENTIFIER),
    username = null,
    password = null,
    sUsername = null,
    sPassword = null;

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

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

  // API used by widget to fetch available envrionments
  RED.httpAdmin.get('/watson-discovery-v1-query-builder/environments', function (req, res) {
    console.log('----- Pre Environment Check on Credentials ');
    console.log(sUsername);
    console.log(sPassword);
    var discovery = new DiscoveryV1({
      username: sUsername ? sUsername : req.query.un,
      password: sPassword ? sPassword : req.query.pwd,
      version_date: '2016-12-15'
    });

    var xxx = { error: 'Testing for Bluemix Settings' ,
                sUsername : sUsername,
                password: sPassword,
                req_un: req.query.un,
                req_pwd: req.query.pwd
    };
    res.json(xxx);

//    console.log('Need to fetch envrionments here');
//    discovery.getEnvironments({}, function (err, response) {
    //lt.getModels({}, function (err, models) {
//      if (err) {
//        res.json(err);
//      }
//      else {
//        console.log('List of Envrionments :')
//        console.log(response);
//        res.json(response.environments ? response.environments : response);
        //res.json(models);
//      }
//    });
  });



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

    this.on('input', function (msg) {
      // Simply return params for query on msg object
      msg.discoveryparams = {};
      node.send(msg);
    });
  }

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

/**
 * 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 discoveryutils = require('./discovery-utils'),
    DiscoveryV1 = require('watson-developer-cloud/discovery/v1'),
    serviceutils = require('../../utilities/service-utils'),
    dservice = serviceutils.getServiceCreds(SERVICE_IDENTIFIER),
    username = null,
    password = null,
    sUsername = null,
    sPassword = null;

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

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

  // API used by widget to fetch available envrionments
  RED.httpAdmin.get('/watson-discovery-v1-query-builder/environments', function (req, res) {
    var discovery = new DiscoveryV1({
      username: sUsername ? sUsername : req.query.un,
      password: sPassword ? sPassword : req.query.pwd,
      version_date: '2016-12-15'
    });

//    console.log('Need to fetch envrionments here');
    discovery.getEnvironments({}, function (err, response) {
    //lt.getModels({}, function (err, models) {
      if (err) {
        res.json(err);
      }
      else {
        console.log('List of Envrionments :')
        console.log(response);
        res.json(response.environments ? response.environments : response);
        //res.json(models);
      }
    });
  });



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

    this.on('input', function (msg) {
      // Simply return params for query on msg object
      msg.discoveryparams = {};
      node.send(msg);
    });
  }

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

Create New Query Builder Node for Discovery Service