//
// Module: Templates
// =================
//
'use strict';

//
// <a name='constructor'></a>
// Constructor
// -----------
//
function Templates (opts, uri) {
  ['key', 'secret'].forEach(function (item) {
    if (opts[item] == null)
      throw new Error('There\'s a required argument missing: ' + item);
  });

  this.opts = opts;
  this.uri = uri;
}

//
// <a name='create'></a>
// Create a template
// -----------------
//
// #### Arguments
//
// + `params` -- Required
//   - `template`
//   - `auth` is required as well but this is created together with the signature
//   before doing the `POST` request inside this method.
// + `callback` - Function Callback
//
// #### Returns
//
// A `Callback` function with `(err, body)` from the request.
//
// #### Code
Templates.prototype.create = function (params, callback) {
  var self = this;
  var signature = new (require('./signature.js'))(self.opts);
  var request = require('request');

  if (params == null)
    return callback(new Error('params are required'));

  ['template'].forEach(function (item) {
    if (params[item] == null)
      return callback(new Error('Required param mising: ' + item));
  });

  var r = request.post(
    self.uri.base + self.uri.templates,
    function (err, response, body) {
      if (err)
        return callback(err);

      return callback(null, body);
  });

  var form = r.form();

  var x = signature.create(params);

  form.append('signature', x.signature);
  form.append('params', JSON.stringify(x.params));
};

//
// <a name='retrieve'></a>
// Retrieve a template
// -------------------
//
// #### Arguments
//
// + `templateId`
// + `callback` -- Callback function
//
// #### Returns
//
// Returns a callback with `(err, body)`, `body` being the template JSON content
// for the given template ID.
//
// #### Code
Templates.prototype.retrieve = function (templateId, callback) {
  var self = this;
  var signature = new (require('./signature.js'))(self.opts);
  var request = require('request');

  if (templateId == null)
    return callback(new Error('templateId is required'));

  var x = signature.create();

  request({
    method: 'GET',
    uri: self.uri.base + self.uri.templates + '/' + templateId,
    qs: {
      signature: x.signature
    }
  }, function (err, response, body) {
    if (err)
      return callback(err);

    return callback(null, body);
  });
};

//
// <a name='edit'></a>
// Edit a template
// ---------------
//
// #### Arguments
//
// + `params` -- Required
//   - `name`
//   - `template`
//   - `auth` is required as well but this is created together with the signature
//   before doing the `PUT` request inside this method.
// + `callback` - Function Callback
//
// #### Returns
//
// A `Callback` function with `(err, body)` from the request
//
// #### Code
Templates.prototype.edit = function (params, templateId, callback) {
  var self = this;
  var signature = new (require('./signature.js'))(self.opts);
  var request = require('request');

  if (params == null)
    return callback(new Error('params are required'));

  ['name', 'template'].forEach(function (item) {
    if (params[item] == null)
      return callback(new Error('Required param missing: ' + item));
  });

  var x = signature.create(params);
  var payload = {
    params: x.params,
    signature: x.signature
  };

  request({
    method: 'PUT',
    uri: self.uri.base + self.uri.templates + '/' + templateId,
    body: JSON.stringify(payload)
  }, function (err, response, body) {
    if (err)
      return callback(err);

    return callback(null, body);
  });
};

module.exports = Templates;

@@ -3575,16 +3575,893 @@
 %7D);%0A%7D;%0A%0A
+//%0A// %3Ca name='remove'%3E%3C/a%3E%0A// Remove a template%0A// -----------------%0A//%0A// #### Arguments%0A//%0A// + %60templateId%60 - required%0A// + %60callback%60 - Callback function%0A//%0A// #### Returns%0A// %0A// Returns a %60(err, response)%60 with the response from the request directly.%0A//%0A// #### Code%0ATemplates.prototype.remove = function (templateId, callback) %7B%0A  var self = this;%0A  var signature = new (require('./signature.js'))(self.opts);%0A  var request = require('request');%0A%0A  if (templateId == null)%0A    return callback(new Error('template id is required'));%0A%0A  var x = signature.create();%0A  var payload = %7B%0A    signature: x.signature%0A  %7D;%0A%0A  request(%7B%0A    method: 'DELETE',%0A    uri: self.uri.base + self.uri.templates + '/' + templateId,%0A    body: JSON.stringify(payload)%0A  %7D, function (err, response, body) %7B%0A    if (err)%0A      return callback(err);%0A%0A    return callback(null, body);%0A  %7D);%0A%7D;%0A%0A
 module.e

remove method