/* global TrelloPowerUp */

var Promise = TrelloPowerUp.Promise;
// var WHITE_ICON = './images/icon-white.svg';
// var GRAY_ICON = './images/icon-gray.svg';
var cardEndpoint = 'https://ygrstg2qvi.execute-api.us-west-2.amazonaws.com/dev/card/';

function makeRequest (method, url) {
  return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.onload = function () {
      if (this.status >= 200 && this.status < 300) {
        resolve(xhr.response);
      } else {
        reject({
          status: this.status,
          statusText: xhr.statusText
        });
      }
    };
    xhr.onerror = function () {
      reject({
        status: this.status,
        statusText: xhr.statusText
      });
    };
    xhr.send();
  });
}

function getCard(cardId) {
  return makeRequest('GET', cardEndpoint+cardId);
}

var getBadges = function(t) {
  return t.card('id').get('id').then(function(cardId) {
    return getCard(cardId).then(function(card) {
      var cardAutoDue = card.autoDue * 1000;
      var daysLeft = Math.ciel((cardAutoDue - Date.now())/(3600*24*1000));
      return [{
        text: 5,
        color: 'red',
        refresh: 3600
      }];
    });
  });
};

TrelloPowerUp.initialize({
  'card-badges': function(t, options) {
    return getBadges(t);
  }
});

@@ -858,16 +858,33 @@
 +cardId)
+.then(JSON.parse)
 ;%0A%7D%0A%0Avar
@@ -1091,12 +1091,13 @@
 th.c
-i
 e
+i
 l(
+ 
 (car
@@ -1133,16 +1133,17 @@
 24*1000)
+ 
 );%0A     
@@ -1171,9 +1171,31 @@
 xt: 
-5
+daysLeft + ' days left'
 ,%0A  

Fix bugs