var Log = require('./log.js');
var Api = require('node-trello');

function Trello(trelloConfig) {
  this.config = trelloConfig;
  this.api = new Api(this.config.key, this.config.token);
  this.values = {};
  this.initialized = false;

  // initialize
  this.init = function(){
    self = this;
    Promise.all([
      self.loadMe(),
      self.loadBoard()
    ]).then(function(res) {
      return Promise.resolve(
        Promise.all([
          self.loadMycard(),
          self.loadList()
        ])
      );
    }).then(function(res) {
      Log.i('Trello data loaded!!');
      Log.d(self);

      self.initialized = true;
    }).catch(function(error) {
      Log.e(error);
    });
  };

  // API request GET me
  this.loadMe = function() {
    Log.i('GET trello me: ');

    self = this;
    return new Promise(function(resolve, reject) {
      self.api.get('/1/members/me', function(err, data) {
        if (err) reject(err);
        Log.i('GET trello me: Me ["' + data.fullName + '"] found');
        Log.d(data);

        self.values.me = data.id;
        resolve(true);
      });
    });
  };

  // API request GET board
  this.loadBoard = function() {
    Log.i('GET trello board: ');

    self = this;
    return new Promise(function(resolve, reject) {
      self.api.get('/1/members/me/boards', function(err, data) {
        if (err) reject(err);
        Log.d(data);

        for (board of data) {
          if (board.name == self.config.board.name) {
            Log.i('GET trello board: Board ["' + self.config.board.name + '"] found');
            Log.d(board);

            self.values.board = board.id;
            resolve(true);
          }
        }
      });
    });
  };

  // API request GET my card
  this.loadMycard = function() {
    Log.i('GET trello my card: ');
    self = this;

    return new Promise(function(resolve, reject) {
      self.api.get('/1/boards/' + self.values.board + '/cards?fields=id,idMembers,name', function(err, data) {
        if (err) reject(err);
        Log.d(data);

        for (card of data) {
          for (member of card.idMembers) {
            if (member == self.values.me) {
              Log.i('GET trello my card: My Card ["' + card.name + '"] found');
              Log.d(card);

              self.values.mycard = card.id;
              resolve(true);
            }
          }
        }
      });
    });
  };

  // API request GET list
  this.loadList = function() {
    Log.i('GET trello list: ');
    self = this;

    return new Promise(function(resolve, reject) {
      self.api.get('/1/boards/' + self.values.board + '/lists', function(err, data) {
        if (err) reject(err);
        Log.d(data);

        for (list of data) {
          if (list.name == self.config.board.list.online) {
            Log.i('GET trello list: List ["' + list.name + '"] found');
            Log.d(list);

            self.values.online = list.id;
          }
          if (list.name == self.config.board.list.offline) {
            Log.i('GET trello list: List ["' + list.name + '"] found');
            Log.d(list);

            self.values.offline = list.id;
          }
          if (list.name == self.config.board.list.away) {
            Log.i('GET trello list: List ["' + list.name + '"] found');
            Log.d(list);

            self.values.away = list.id;
          }
        }
        resolve(true);
      });
    });
  };

  // API request PUT card
  this.moveCard = function(listName) {
    Log.i('PUT trello card: ');
    if (listName == 'online') {
      list = this.values.online;
    } else if (listName == 'offline') {
      list = this.values.offline;
    } else if (listName == 'away') {
      list = this.values.away;
    } else {
      throw new Error('Invalid list name');
    }

    this.api.put('/1/cards/' + this.values.mycard + '/idList?value=' + list, function(err, data) {
      if (err) throw err;
      Log.i('PUT trello card: card was moved to "' + listName + '"');
      Log.d(data);
    });
  };
};

module.exports = Trello;

@@ -448,22 +448,21 @@
 elf.load
-Myc
+C
 ard
+s
 (),%0A    
@@ -1707,23 +1707,21 @@
 est GET 
-my 
 card
+s
 %0A  this.
@@ -1728,14 +1728,13 @@
 load
-Myc
+C
 ard
+s
  = f
@@ -2003,32 +2003,63 @@
    Log.d(data);%0A
+        self.values.cards = %5B%5D;
 %0A        for (ca
@@ -2327,33 +2327,20 @@
     
+%7D%0A
   
-resolve(true);%0A
         
     
@@ -2335,32 +2335,69 @@
         
+%7D%0A
     
-%7D%0A
      
+ self.values.cards.push(card.id);%0A
      
+   
 %7D%0A      
@@ -2390,33 +2390,46 @@
       %7D%0A        
-%7D
+resolve(true);
 %0A      %7D);%0A    %7D
@@ -3506,16 +3506,22 @@
 unction(
+card, 
 listName
@@ -3862,22 +3862,8 @@
 ' + 
-this.values.my
 card
@@ -3968,20 +3968,28 @@
 o card: 
-card
+' + card + '
  was mov
@@ -4047,16 +4047,294 @@
 %7D);%0A  %7D;
+%0A%0A  // API request PUT card%0A  this.moveMyCard = function(listName) %7B%0A    this.moveCard(this.values.mycard, listName);%0A  %7D%0A%0A  // API requrest PUT cards%0A  this.moveAllCards = function(listName) %7B%0A    for (card of this.values.cards) %7B%0A      this.moveCard(card, listName);%0A    %7D%0A  %7D
 %0A%7D;%0A%0Amod

Add '#moveAllCards' and store all card ids