/* jshint esnext: true */

/**
 * Module Dependencies
 */

import request from "superagent";
import encode from 'base64-encode';
import array from 'array';

class SimpleNote {
  constructor(email, password) {
    this.email = email;
    this.password = password;
    this.token = null;
    this.expires = null; //utc seconds
    this.notes = [];
    this.api = "https://simple-note.appspot.com/api/";
    this.api2 = "https://simple-note.appspot.com/api2/";
  }

  auth() {
    // base64 encoding of auth params
    var query = encode("email=" + this.email + '&password=' + this.password);

    //no token or expired token, get a new one
    if (this.token === null || Date.now() > this.expires) {
      return new Promise(
        (resolve, reject) => {
          request
            .post(this.api + 'login')
            .send(query)
            .end((err, res) => {
              if (res.error) {
                reject(res.error);
              }
              this.token = res.text;
              this.expires = Date.parse(res.headers.expires) + 86400000; // expires 24 hours from now
              resolve(res.text);
            });
        });
    }

    return Promise.resolve(this.token);
  }

  all(len) {
    // 100 is the highest value you can query
    len = (len && len < 100) ? len : 100;

    var auth = this.auth();

    // TODO: loop through if `mark` is set
    // https://github.com/cpbotha/nvpy/blob/master/nvpy/simplenote.py#L211
    return auth.then(token =>
        new Promise(
          (resolve, reject) => {
            request
              .get(self.api2 + 'index')
              .type('json')
              .query({
                auth: token,
                email: this.email,
                length: len
              })
              .end((err, res) => {
                if (res.error) reject(res.error);
                else if (!res.text) reject([]);
                var json = JSON.parse(res.text);
                this.notes = array(json.data).sort('modifydate', 'desc');
                resolve(this.notes);
              });
          })
      )
      .catch(e => console.log(e));
  }

  get(key) {
    if (!key) throw 'error no key';

    var auth = this.auth();

    return auth.then(token =>
        new Promise(
          (resolve, reject) => {
            request
              .get(this.api2 + 'data/' + key)
              .type('json')
              .query({
                auth: token,
                email: this.email
              })
              .end((err, res) => {
                if (res.error) reject(res.error);
                else if (!res.text) reject({});
                resolve(JSON.parse(res.text));
              });
          })
      )
      .catch(e => console.log(e));
  }
}

export default SimpleNote;

@@ -2738,16 +2738,1383 @@
 e));%0A  %7D
+%0A%0A  /**%0A   * Updates a note%0A   * @param  %7Bobject%7D note Note object%0A   * @return %7Barray%7D       Status of update%0A   */%0A  update(note) %7B%0A    var auth = this.auth();%0A%0A    if (note.key !== undefined) %7B%0A      return auth.then(token =%3E%0A          new Promise(%0A            (resolve, reject) =%3E %7B%0A              request%0A                .post(this.api2 + 'data/' + note.key)%0A                .type('json')%0A                .query(%7B%0A                  auth: token,%0A                  email: this.email%0A                %7D)%0A                .send(note)%0A                .end((err, res) =%3E %7B%0A                  if (res.error) reject(res.error);%0A                  else if (!res.text) reject(%7B%7D);%0A                  resolve(JSON.parse(res.text));%0A                %7D);%0A            %7D)%0A        )%0A        .catch(e =%3E console.log(e));%0A    %7D%0A%0A    return auth.then(token =%3E%0A        new Promise(%0A          (resolve, reject) =%3E %7B%0A            request%0A              .post(this.api2 + 'data')%0A              .type('json')%0A              .query(%7B%0A                auth: token,%0A                email: this.email%0A              %7D)%0A              .send(note)%0A              .end((err, res) =%3E %7B%0A                if (res.error) reject(res.error);%0A                else if (!res.text) reject(%7B%7D);%0A                resolve(JSON.parse(res.text));%0A              %7D);%0A          %7D)%0A      )%0A      .catch(e =%3E console.log(e));%0A  %7D
 %0A%7D%0A%0Aexpo

Add update function.