var EventEmitter = require('events').EventEmitter;
var util = require('util');

var Amount        = require('./amount').Amount;

var extend = require('extend');

/**
 * Represents a persistent path finding request.
 *
 * Only one path find request is allowed per connection, so when another path
 * find request is triggered it will supercede the existing one, making it emit
 * the 'end' and 'superceded' events.
 */
var PathFind = function (remote, src_account, dst_account,
                         dst_amount, src_currencies)
{
  EventEmitter.call(this);

  this.remote = remote;

  this.src_account    = src_account;
  this.dst_account    = dst_account;
  this.dst_amount     = dst_amount;
  this.src_currencies = src_currencies;
};

util.inherits(PathFind, EventEmitter);

/**
 * Submits a path_find_create request to the network.
 *
 * This starts a path find request, superceding all previous path finds.
 *
 * This will be called automatically by Remote when this object is instantiated,
 * so you should only have to call it if the path find was closed or superceded
 * and you wish to restart it.
 */
PathFind.prototype.create = function ()
{
  var req = this.remote.request_path_find_create(this.src_account,
                                                 this.dst_account,
                                                 this.dst_amount,
                                                 this.src_currencies);

  req.request();
};

PathFind.prototype.close = function ()
{
  this.remote.request_path_find_close().request();
  this.emit('end');
  this.emit('close');
};

PathFind.prototype.notify_update = function (message)
{
  var src_account = message.source_account;
  var dst_account = message.destination_account;
  var dst_amount  = Amount.from_json(message.destination_amount);

  // Only pass the event along if this path find response matches what we were
  // looking for.
  if (message.alternatives && message.alternatives.length) console.log(Amount.from_json(message.alternatives[0].source_amount).to_text_full());
  if (this.src_account === src_account &&
      this.dst_account === dst_account &&
      this.dst_amount.equals(dst_amount)) {
    this.emit('update', message);
  }
};

PathFind.prototype.notify_superceded = function ()
{
  this.emit('end');
  this.emit('superceded');
};

exports.PathFind = PathFind;

var EventEmitter = require('events').EventEmitter;
var util = require('util');

var Amount        = require('./amount').Amount;

var extend = require('extend');

/**
 * Represents a persistent path finding request.
 *
 * Only one path find request is allowed per connection, so when another path
 * find request is triggered it will supercede the existing one, making it emit
 * the 'end' and 'superceded' events.
 */
var PathFind = function (remote, src_account, dst_account,
                         dst_amount, src_currencies)
{
  EventEmitter.call(this);

  this.remote = remote;

  this.src_account    = src_account;
  this.dst_account    = dst_account;
  this.dst_amount     = dst_amount;
  this.src_currencies = src_currencies;
};

util.inherits(PathFind, EventEmitter);

/**
 * Submits a path_find_create request to the network.
 *
 * This starts a path find request, superceding all previous path finds.
 *
 * This will be called automatically by Remote when this object is instantiated,
 * so you should only have to call it if the path find was closed or superceded
 * and you wish to restart it.
 */
PathFind.prototype.create = function ()
{
  var self = this;

  var req = this.remote.request_path_find_create(this.src_account,
                                                 this.dst_account,
                                                 this.dst_amount,
                                                 this.src_currencies,
                                                 handleInitialPath);

  function handleInitialPath(err, msg) {
    if (err) {
      // XXX Handle error
      return;
    }
    self.notify_update(msg);
  }

  req.request();
};

PathFind.prototype.close = function ()
{
  this.remote.request_path_find_close().request();
  this.emit('end');
  this.emit('close');
};

PathFind.prototype.notify_update = function (message)
{
  var src_account = message.source_account;
  var dst_account = message.destination_account;
  var dst_amount  = Amount.from_json(message.destination_amount);

  // Only pass the event along if this path find response matches what we were
  // looking for.
  if (message.alternatives && message.alternatives.length) console.log(Amount.from_json(message.alternatives[0].source_amount).to_text_full());
  if (this.src_account === src_account &&
      this.dst_account === dst_account &&
      this.dst_amount.equals(dst_amount)) {
    this.emit('update', message);
  }
};

PathFind.prototype.notify_superceded = function ()
{
  this.emit('end');
  this.emit('superceded');
};

exports.PathFind = PathFind;

Use the initial path find response as a first set of alternatives.