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.loadCards(),
          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 cards
  this.loadCards = 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);
        self.values.cards = [];
        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;
            }
          }
          self.values.cards.push(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(card, 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/' + card + '/idList?value=' + list, function(err, data) {
      if (err) throw err;
      Log.i('PUT trello card: ' + card + ' was moved to "' + listName + '"');
      Log.d(data);
    });
  };

  // API request PUT card
  this.moveMyCard = function(listName) {
    this.moveCard(this.values.mycard, listName);
  }

  // API requrest PUT cards
  this.moveAllCards = function(listName) {
    for (card of this.values.cards) {
      this.moveCard(card, listName);
    }
  }
};

module.exports = Trello;

@@ -1421,22 +1421,19 @@
 if (
-board.name == 
+new RegExp(
 self
@@ -1447,24 +1447,42 @@
 .board.name)
+.test(board.name))
  %7B%0A         
@@ -2760,302 +2760,200 @@
     
-for (list of data) %7B%0A          if (list.name == self.config.board.list.online) %7B%0A            Log.i('GET trello list: List %5B%22' + list.name + '%22%5D found');%0A            Log.d(list);%0A%0A            self.values.online = list.id;%0A          %7D%0A          if (list.name == self.config.board.list.offlin
+states = %5B'online', 'offline', 'away'%5D;%0A        for (list of data) %7B%0A          for (state of states) %7B%0A            if (new RegExp(eval('self.config.board.list.' + state)).test(list.nam
 e)
+)
  %7B%0A
+  
     
@@ -3024,32 +3024,34 @@
 ');%0A            
+  
 Log.d(list);%0A%0A  
@@ -3064,207 +3064,55 @@
     
-self.values.offline = list.id;%0A          %7D%0A          if (list.name == self.config.board.list.away) %7B%0A            Log.i('GET trello list: List %5B%22' + list.name + '%22%5D found');%0A            Log.d(list
+  eval('self.values.' + state + ' = list.id'
 );%0A
-%0A
     
@@ -3123,35 +3123,9 @@
     
-self.values.away = list.id;
+%7D
 %0A   

Refactor use RegExp