// Node modules
var https = require('https'),
  url = require('url'),
  request = require('request'),
  fs = require('fs');

// EdgeGrid Auth Module
var auth = require('./auth'),
  edgerc = require('./edgerc'),
  logger = require('./logger');

var EdgeGrid = function(client_token, client_secret, access_token, base_uri) {
  // accepting an object containing a path to .edgerc and a config group
  if (typeof arguments[0] === 'object') {
    var path = arguments[0].path;
    var group = arguments[0].group;
    if (path === undefined) {
      logger.error("No .edgerc path");
      return false;
    }

    this.config = edgerc(path, group);
  } else {
    if (!validatedArgs([client_token, client_secret, access_token, base_uri])) {
      throw new Error('Insufficient Akamai credentials');
    }

    this.config = {
      client_token: client_token,
      client_secret: client_secret,
      access_token: access_token,
      base_uri: base_uri
    };
  }

  return this;
};

EdgeGrid.prototype.auth = function(req, callback) {
  this.request = auth.generate_auth(req, this.config.client_token, this.config.client_secret, this.config.access_token, this.config.base_uri);

  if (callback && typeof callback == "function") {
    callback(this);
  }

  return this;
};

EdgeGrid.prototype.send = function(callback) {
  request(this.request, function(error, response, body) {
    if (error) { throw new Error(error); }

    callback(body, response);
  });
};

function validatedArgs(args) {
  var expected = [
    'client_token', 'client_secret', 'access_token', 'base_uri'
  ],
  valid = true,
  i;

  expected.forEach(function(arg, i) {
    if (!args[i]) {
      if (process.env.EDGEGRID_ENV !== 'test' ) {
        logger.error('No defined ' + arg);
      }

      valid = false;
    }
  });

  return valid;
}

module.exports = EdgeGrid;

var https = require('https'),
    url = require('url'),
    request = require('request'),
    fs = require('fs'),
    auth = require('./auth'),
    edgerc = require('./edgerc'),
    logger = require('./logger');

var EdgeGrid = function(client_token, client_secret, access_token, base_uri) {
  // accepting an object containing a path to .edgerc and a config group
  if (typeof arguments[0] === 'object') {
    var path = arguments[0].path;
    var group = arguments[0].group;
    if (path === undefined) {
      logger.error("No .edgerc path");
      return false;
    }

    this.config = edgerc(path, group);
  } else {
    if (!validatedArgs([client_token, client_secret, access_token, base_uri])) {
      throw new Error('Insufficient Akamai credentials');
    }

    this.config = {
      client_token: client_token,
      client_secret: client_secret,
      access_token: access_token,
      base_uri: base_uri
    };
  }

  return this;
};

EdgeGrid.prototype.auth = function(req, callback) {
  this.request = auth.generate_auth(req, this.config.client_token, this.config.client_secret, this.config.access_token, this.config.base_uri);

  if (callback && typeof callback == "function") {
    callback(this);
  }

  return this;
};

EdgeGrid.prototype.send = function(callback) {
  request(this.request, function(error, response, body) {
    if (error) { throw new Error(error); }

    callback(body, response);
  });
};

function validatedArgs(args) {
  var expected = [
        'client_token', 'client_secret', 'access_token', 'base_uri'
      ],
      valid = true,
      i;

  expected.forEach(function(arg, i) {
    if (!args[i]) {
      if (process.env.EDGEGRID_ENV !== 'test' ) {
        logger.error('No defined ' + arg);
      }

      valid = false;
    }
  });

  return valid;
}

module.exports = EdgeGrid;

correct semicolon to comma