var Request = require('request');
var Promise = require('promise');
var List = require('immutable').List;
var Map = require('immutable').Map;
var Specberus = require("specberus/lib/validator").Specberus;
require('./config.js');

var SpecberusWrapper = function () {};

SpecberusWrapper.validate = function (url) {
  return new Promise(function (resolve, reject) {
    function Sink () {}
    require("util").inherits(Sink, require("events").EventEmitter);

    var sink = new Sink();
    var errors = List();
    var metadata = Map();

    sink.on("end-all", function (profilename) {
      resolve({
        errors: errors,
        metadata: metadata
      });
    });

    sink.on("metadata", function (key, value) {
      metadata = metadata.set(key, value);
    });

    sink.on("err", function (type, data) {
      data.type = type;
      errors = errors.push(data);
    });

    sink.on("exception", function (exception) {
      reject(new Error(exception.message));
    });

    var options = {
      url: url,
      profile: require("specberus/lib/profiles/WD"),
      events: sink,
      // validation: "recursive",
      validation: "simple-validation",
      noRecTrack: false,
      informativeOnly: false,
      processDocument: "2014"
    };

    if (process.env.NODE_ENV == "dev") {
      var port = (process.env.PORT || 3000) + 1;
      options.cssValidator = "http://localhost:" + port + "/css-validator/validator";
      options.htmlValidator = "http://localhost:" + port + "/check";
    }

    new Specberus().validate(options);
  });
};

exports.SpecberusWrapper = SpecberusWrapper;

var ThirdPartyChecker = function () {};

ThirdPartyChecker.check = function (url) {
  var args  = ['../third-party-resources-checker/detect-phantom.js', url, global.RESOURCES_WHITELIST];
  var spawn = require('child_process').spawn;

  return new Promise(function (resolve) {
    var phantom = spawn(global.PHANTOM, args);
    var buffer = "";

    phantom.stdout.on('data', function(data) {
      buffer += data;
    });

    phantom.on('close', function() {
      var consoleout = buffer.replace(global.DEFAULT_HTTP_LOCATION, '', 'g').split("\n");
      consoleout.pop();
      resolve(consoleout);
    });
  });
}

exports.ThirdPartyChecker = ThirdPartyChecker;

var TokenChecker = function () {};

TokenChecker.check = function (url, token) {
  return new Promise(function (resolve, reject) {
    Request.get({
      uri: global.TOKEN_ENDPOINT,
      auth: {
        user: global.USERNAME,
        pass: global.PASSWORD
      },
      qs: {
        spec: url,
        token: token
      }
    }, function (err, res, body) {
      if (err) reject(new Error("There was an error while checking the token: ", err));
      else resolve(JSON.parse(body));
    });
  });
}

exports.TokenChecker = TokenChecker;

@@ -1,12 +1,27 @@
+'use strict';%0A%0A
 var Request 
@@ -396,16 +396,17 @@
 k () %7B%7D%0A
+%0A
     requ
@@ -608,25 +608,16 @@
 esolve(%7B
-%0A        
 errors: 
@@ -623,24 +623,16 @@
  errors,
-%0A       
  metadat
@@ -642,23 +642,16 @@
 metadata
-%0A      
 %7D);%0A    
@@ -1687,16 +1687,21 @@
 rgs  = %5B
+%0A    
 '../thir
@@ -1745,21 +1745,29 @@
 tom.js',
+%0A   
  url,
+%0A   
  global.
@@ -1785,16 +1785,19 @@
 HITELIST
+%0A  
 %5D;%0A  var
@@ -1991,16 +1991,17 @@
 tion
+ 
 (data) %7B
 %0A   
@@ -1996,22 +1996,16 @@
 (data) %7B
-%0A     
  buffer 
@@ -2012,20 +2012,16 @@
 += data;
-%0A   
  %7D);%0A%0A  
@@ -2457,25 +2457,16 @@
  auth: %7B
-%0A        
 user: gl
@@ -2479,24 +2479,16 @@
 SERNAME,
-%0A       
  pass: g
@@ -2501,23 +2501,16 @@
 PASSWORD
-%0A      
 %7D,%0A     
@@ -2515,25 +2515,16 @@
    qs: %7B
-%0A        
 spec: ur
@@ -2529,16 +2529,8 @@
 url,
-%0A       
  tok
@@ -2538,23 +2538,16 @@
 n: token
-%0A      
 %7D%0A    %7D,

Reformat functions.js according to newly defined coding style