// 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';

// Necessary libs
var request = require('request');
var assert  = require('assert');
var _       = require('underscore');

// Global
var gds;

// Init
module.exports = exports = gds = function init(config) {

  // Validate config
  assert.equal(typeof config, 'object',
    'You must specify the endpoint url when invoking this module');
  assert.ok(/^https?:/.test(config.url), 'url is not valid');

  // Handle URL Requests
  function apiCall(opts) {
    if (!opts) var opts = {}

    // Force the URL
    opts.url = config.url + opts.url;

    // Auth
    opts.auth = {
      user: config.username,
      pass: config.password
    }
console.log(opts)
    return request(opts, function(error, response, body) {
      return console.log({
        error: error,
        response: response,
        body: body
      });
    })
  }

  /*
  Vertices
   */

  // Create Vertice
  function createVertices() {
  }

  // List Vertices
  function listVertices() {
    var opts = {
      url: '/vertices',
      method: 'GET'
    };
    return apiCall(opts);
  }

  var vertices = {
    create: createVertices,
    list: listVertices
  };

  /*
  Gremlin
   */

  // Gremlin Endpoint
  function gremlinQuery(traversal) {
    var opts = {
      url: '/gremlin',
      method: 'POST',
      json: true,
      body: '{"gremlin": ""}'
    }

    // If traveral is string
    if (_.isArray(traversal)) {
      // Check first element is g
      if (traversal[0] != 'g') {
        traversal.unshift('g');
      }

      opts.body = JSON.stringify({gremlin:traversal.join('.')});
    }
    else {
      opts.body = JSON.stringify({gremlin:traversal});
    }

    return apiCall(opts);
  }

  var gremlin = gremlinQuery;

  // Return object
  return {
    vertices: vertices,
    gremlin: gremlin
  }
}

@@ -1202,25 +1202,8 @@
   %7D%0A
-console.log(opts)
 %0A   
@@ -2249,16 +2249,533 @@
 Query;%0A%0A
+  /*%0A  Input/Output%0A   */%0A%0A  // Bulk Upload - GraphML%0A  function uploadGraphMl() %7B%0A%0A  %7D%0A%0A  //  Buld Upload - graphson%0A  function uploadGraphSON() %7B%0A%0A  %7D%0A%0A  // Extract%0A  function extractBulk(format) %7B%0A    if (!format) var format = 'json';%0A%0A    var opts = %7B%0A      url: '/extract',%0A      headers: %7B%0A        'Content-Type': 'application/' + format%0A      %7D%0A    %7D%0A%0A    return apiCall(opts);%0A  %7D%0A%0A  var io = %7B%0A    bulkload: %7B%0A      graphml: uploadGraphMl,%0A      graphson: uploadGraphSON%0A    %7D,%0A    extract: extractBulk%0A  %7D%0A%0A
   // Ret
@@ -2840,15 +2840,27 @@
  gremlin
+,%0A    io: io
 %0A  %7D%0A%7D%0A

I/O extract: defaults to JSON