var Promise, SpyParticipant, msgflo_nodejs;

msgflo_nodejs = require('msgflo-nodejs');

Promise = require('bluebird');

SpyParticipant = function(address, role, queueMapping) {
  var definition, exchange, func, name, part;
  definition = {
    component: 'test/SpyParticipant',
    label: 'Lets you snoop on data!',
    inports: [],
    outports: []
  };
  for (name in queueMapping) {
    exchange = queueMapping[name];
    definition.inports.push({
      id: name,
      hidden: false
    });
    definition.outports.push({
      id: name,
      hidden: true
    });
  }
  part = null;
  func = function(portname, indata, send) {
    part.data[portname].push(indata);
    return send(portname, null, indata);
  };
  part = new msgflo_nodejs.participant.Participant(address, definition, func, role);
  part.reset = function() {
    var _, results;
    part.data = {};
    results = [];
    for (name in queueMapping) {
      _ = queueMapping[name];
      results.push(part.data[name] = []);
    }
    return results;
  };
  // connects to the queues
  part.setupBindings = function(callback) {
    var addBinding, client, returnResults;
    // need Broker interface to do bindings
    client = msgflo_nodejs.transport.getBroker(address);
    addBinding = function(name, _i, _j, cb) {
      var binding, srcExchange;
      srcExchange = queueMapping[name];
      if (!srcExchange) { // no binding
        return cb(null);
      }
      binding = {
        type: 'pubsub',
        src: srcExchange,
        tgt: `${role}.${name.toUpperCase()}`
      };
      return client.addBinding(binding, cb);
    };
    returnResults = function(err, data) {
      return client.disconnect(function() {
        return callback(err, data);
      });
    };
    return client.connect(function(err) {
      var names;
      if (err) {
        return callback(err);
      }
      names = Object.keys(queueMapping);
      return Promise.map(names, Promise.promisify(addBinding)).then(function(data) {
        return returnResults(null, data);
      }).catch(returnResults);
    });
  };
  // Convenience for starting participant and setting up bindings
  part.startSpying = function(callback) {
    return part.start(function(err) {
      if (err) {
        return callback(err);
      }
      return part.setupBindings(callback);
    });
  };
  part.getMessages = function(port, number, callback) {
    var checkSendMessages;
    checkSendMessages = function() {
      var messages;
      messages = part.data[port];
      //console.log 'SpyParticipant.checkSendMessages', port, messages
      if (messages == null) {
        return;
      }
      if (messages.length < number) {
        return;
      }
      messages = messages.splice(0, number);
      //console.log 'SpyParticipant.getMessages returning', messages, part.data
      part.removeListener('data', checkSendMessages);
      return callback(messages);
    };
    part.on('data', checkSendMessages);
    return checkSendMessages(); // we might already have the requested data
  };
  part.reset();
  return part;
};

module.exports = SpyParticipant;
