var http = require('http'),
  _ = require('underscore'),
  updateTimer;
var sync = require('synchronize')

function JenkinsButler(options) {
  var self = this;

  self.options = options || {};
  self.host = self.options.host || null;
  if (!self.host) throw new Error('JenkinsButler: No Jenkins host provided.');

  self.port = self.options.port || null;
  if (!self.port) throw new Error('JenkinsButler: No Jenkins port provided.');

  self.username = self.options.username || null;
  if (!self.username) throw new Error('JenkinsButler: No username provided.');

  self.token = self.options.token || null;
  if (!self.token) throw new Error('JenkinsButler: No token provided.');

  self.jobs = self.options.jobs || null;
  if (!self.jobs) throw new Error('JenkinsButler: No jobs provided.');

  self.updateIntervall = self.options.updateIntervall || null;
}

JenkinsButler.prototype.buildRequestOptions = function(job) {
  var options = {
    hostname: this.host,
    port: this.port,
    path: '/job/' + job.job + '/lastBuild/api/json?token=' + this.token,
    headers: {
      'Authorization': 'Basic ' + new Buffer(this.username + ':' + this.token).toString('base64')
    }
  };
  return options;
}

JenkinsButler.prototype.getJobStatus = function(job, callback) {
  console.log('get status ' + JSON.stringify(job));

  http.get(this.buildRequestOptions(job), function(res) {

    if (res.statusCode != 200) {
      callback('Failed to fetch jenkins data, http status: ' + res.statusCode);
      return;
    }

    var response = '';

    res.on('data', function(data) {
      response += data;
    });

    res.on('end', function(){
      var resi = JSON.parse(response);
      callback(null, resi.building ? "BUILDING" : resi.result);
    })

  }).on('error', function(e) {
    console.error('Error: ' + e);
    callback(e);
    return;
  }).on('end', function(e) {
    console.error('End:' + e);
    callback(e);
    return;
  });
}

function updateStatesOfJobs(options) {
  console.log('update status from all jobs');

  butler.jobs.forEach(function(job) {
    sync.fiber(function() {
      butler.getJobStatus(job, function(err, result) {
        console.log('Result of ' + job.job + ' is ' + JSON.stringify(result));
  
        if (!err) {
          sync.fiber(function() { 
            setLEDForJob(job, result, options, sync.defer())
          });
        } else {
          console.log("ERROR: " + err);
        }
      });
    });
  })
}

function setLEDForJob(job, result, options, callback) {
  console.log ("set led for " + job);
  setTimeout(function(){ 
    switch (result) {
      case "UNSTABLE":
        http.get(buildLedRequestOptions(options, job, options.leds.unstable)).on('error', onRequestError).on('end', callback);
        break;
      case "FAILURE":
        http.get(buildLedRequestOptions(options, job, options.leds.failure)).on('error', onRequestError).on('end', callback);
        break;
      case "SUCCESS":
        http.get(buildLedRequestOptions(options, job, options.leds.success)).on('error', onRequestError).on('end', callback);
        break;
      case "ABORTED":
        http.get(buildLedRequestOptions(options, job, options.leds.aborted)).on('error', onRequestError).on('end', callback);
        break;
      case "BUILDING":
        http.get(buildLedRequestOptions(options, job, options.leds.building)).on('error', onRequestError).on('end', callback);
        break;
      default:
        console.log('something is happen... Request: ' + JSON.stringify(option) + " JOB: " + JSON.stringify(job))
        http.get(buildLedRequestOptions(options, job, "000000")).on('error', onRequestError).on('end', callback);
    }
  }, 1000);
}

function onRequestError(err) {
  console.log("Got error: " + err.message);
}

function buildLedRequestOptions(options, job, color) {
  return {
    hostname: options.leds.host,
    port: options.leds.port,
    path: '/led/from/' + job.leds.start + '/to/' + job.leds.end + '/fill/' + color
  };
}

// function buildLedRequestOptionsPost(options) {
//   return {
//     hostname: options.leds.host,
//     port: options.leds.port,
//     method: "POST",
//     path: "/api/range",
//     headers: {
//       'Content-Type': "application/json"
//     }
//   };
// }
//
// function makeLedsShining(start, end, color, options) {
//   req = http.request(buildLedRequestOptionsPost(options));
//
//   req.on('error', function(e) {
//     console.log('problem with request: ' + e.message);
//   });
//
//   var post_data = JSON.stringify({
//     "from": start,
//     "to": end,
//     "rgb": color
//   });
//
//   req.write(post_data);
//   req.end();
// }

function JenkinsButlerService(options) {

}

JenkinsButlerService.prototype.setup = function(config) {
  butler = new JenkinsButler(config.jenkins);
  updateStatesOfJobs(config);
  updateTimer = setInterval(updateStatesOfJobs, config.jenkins.updateIntervall * 1000, config);
}

JenkinsButlerService.prototype.shutdown = function(config) {
  if (updateTimer) {
    console.log('JenkinsButlerService shutdown...')
    clearInterval(updateTimer);

    http.get({
      hostname: config.leds.host,
      port: config.leds.port,
      path: '/api/black'
    }, function(res) {
      console.log("Result: " + res.statusCode)
    });
  }
}

exports = module.exports = new JenkinsButlerService();
exports.JenkinsButler = JenkinsButler;
exports.ServiceName = 'JenkinsButlerService';

@@ -2019,19 +2019,81 @@
 jobs');%0A
+  
 %0A  
+for(var i = 0; i %3C butler.jobs.length; i++) %7B%0A   %0A  //%7D%0A  //
 butler.j
@@ -2503,17 +2503,16 @@
  %7D);%0A  %7D
-)
 %0A%7D%0A%0Afunc

Update jenkins-butler.js