// Routines for working with an account.
//
// Events:
//   wallet_clean	: True, iff the wallet has been updated.
//   wallet_dirty	: True, iff the wallet needs to be updated.
//   balance		: The current stamp balance.
//   balance_proposed
//

// var network = require("./network.js");

var EventEmitter = require('events').EventEmitter;
var Amount = require('./amount').Amount;
var UInt160 = require('./uint160').UInt160;

var extend = require('extend');

var Account = function (remote, account) {
  var self = this;

  this._remote = remote;
  this._account = UInt160.from_json(account);
  this._account_id = this._account.to_json();
  this._subs = 0;

  // Ledger entry object
  // Important: This must never be overwritten, only extend()-ed
  this._entry = {};

  this.on('newListener', function (type, listener) {
    if (Account.subscribe_events.indexOf(type) !== -1) {
      if (!this._subs && 'open' === this._remote._online_state) {
        this._remote.request_subscribe()
          .accounts(this._account_id)
          .request();
      }
      this._subs  += 1;
    }
  });

  this.on('removeListener', function (type, listener) {
    if (Account.subscribe_events.indexOf(type) !== -1) {
      this._subs  -= 1;

      if (!this._subs && 'open' === this._remote._online_state) {
        this._remote.request_unsubscribe()
          .accounts(this._account_id)
          .request();
      }
    }
  });

  this._remote.on('connect', function () {
    if (self._subs) {
      this._remote.request_subscribe()
        .accounts(this._account_id)
        .request();
    }
  });

  this.on('transaction', function (msg) {
    var changed = false;
    msg.mmeta.each(function (an) {
      if (an.entryType === 'AccountRoot' &&
          an.fields.Account === self._account_id) {
        extend(self._entry, an.fieldsNew, an.fieldsFinal);
        changed = true;
      }
    });
    if (changed) {
      self.emit('entry', self._entry);
    }
  });

  return this;
};

Account.prototype = new EventEmitter;

/**
 * List of events that require a remote subscription to the account.
 */
Account.subscribe_events = ['transaction', 'entry'];

Account.prototype.to_json = function ()
{
  return this._account.to_json();
};

/**
 * Whether the AccountId is valid.
 *
 * Note: This does not tell you whether the account exists in the ledger.
 */
Account.prototype.is_valid = function ()
{
  return this._account.is_valid();
};

/**
 * Retrieve the current AccountRoot entry.
 *
 * To keep up-to-date with changes to the AccountRoot entry, subscribe to the
 * "entry" event.
 *
 * @param {function (err, entry)} callback Called with the result
 */
Account.prototype.entry = function (callback)
{
  var self = this;

  self._remote.request_account_info(this._account_id)
    .on('success', function (e) {
      extend(self._entry, e.account_data);
      self.emit('entry', self._entry);

      if ("function" === typeof callback) {
        callback(null, e);
      }
    })
    .on('error', function (e) {
      callback(e);
    })
    .request();

  return this;
};

exports.Account	    = Account;

// vim:sw=2:sts=2:ts=8:et

@@ -874,36 +874,36 @@
 1) %7B%0A      if (!
-this
+self
 ._subs && 'open'
@@ -899,36 +899,36 @@
 s && 'open' === 
-this
+self
 ._remote._online
@@ -937,36 +937,36 @@
 tate) %7B%0A        
-this
+self
 ._remote.request
@@ -990,36 +990,36 @@
       .accounts(
-this
+self
 ._account_id)%0A  
@@ -1048,28 +1048,28 @@
     %7D%0A      
-this
+self
 ._subs  += 1
@@ -1202,20 +1202,20 @@
 %7B%0A      
-this
+self
 ._subs  
@@ -1232,20 +1232,20 @@
    if (!
-this
+self
 ._subs &
@@ -1257,20 +1257,20 @@
 en' === 
-this
+self
 ._remote
@@ -1291,28 +1291,28 @@
 ) %7B%0A        
-this
+self
 ._remote.req
@@ -1342,36 +1342,36 @@
       .accounts(
-this
+self
 ._account_id)%0A  
@@ -1482,20 +1482,20 @@
 %7B%0A      
-this
+self
 ._remote
@@ -1533,20 +1533,20 @@
 ccounts(
-this
+self
 ._accoun

Correct object reference in Account.