var gTrackingSheetUrl = '';
var gEstimationSheetUrl = '';
var cardUrl = '';
var timeTracked = '';
var timeEstimated = '';

TrelloPowerUp.initialize({
  'card-buttons': function(t, options){
    return [{
      icon: './images/icon-gray.svg',
      text: 'Time track',
      callback: function(t){
        return t.overlay({
          title: 'Time track',
          url: './overlay.html',
          args: { user: '' }
        });
      }
    },
    {
      icon: './images/icon-gray.svg',
      text: 'Estimate Card',
      callback: function(t){
        return t.overlay({
          title: 'Estimate Card',
          url: './estimate.html'
        });
      }
    }];
  },
  'card-badges': function(t, card) {
    return getBadges(t, card);
  },
  'show-settings': function(t, options){
    return t.popup({
      title: 'Google Form Settings',
      url: './settings.html',
      height: 184
    });
  }
});

var getEstimatedTime = function(t, gEstimationSheetUrl, cardUrl, getValues){
  console.log('from function');
  if (gEstimationSheetUrl) {
    getValues = "select O WHERE D = '" + cardUrl + "'";
    console.log('gEstimationSheetUrl: ', gEstimationSheetUrl);
    console.log('getValues: ', getValues);
    sheetrock({
      url: gEstimationSheetUrl,
      query: getValues,
      callback: function (error, options, response) {
        if (!error) {
          timeEstimated = $(response.html).find('td').text();
          t.set('card', 'shared', 'estimatedtime', timeEstimated);
          console.log("Estimated Time: ", timeEstimated);
        } else {
          console.log('Estimation ', error);
        }
      }
    });
  }

  return timeEstimated;
}


var getBadges = function(t, card){
  console.log('Compilation: ', 44);

  return t.get('board', 'shared', 'trakingsheet')
  .then(function(savedTrackingSheetUrl) {
    gTrackingSheetUrl = savedTrackingSheetUrl;
    return t.get('board', 'shared', 'estimationsheet')
  })
  .then(function(savedEstimationSheetUrl) {
    gEstimationSheetUrl = savedEstimationSheetUrl;
    return t.card('url').get('url')
  })
  .then(function(savedCardUrl) {
    cardUrl = savedCardUrl;
    if (gTrackingSheetUrl) {
      getValues = "select sum(F) WHERE D = '" + cardUrl + "'";

      sheetrock({
        url: gTrackingSheetUrl,
        query: getValues,
        callback: function (error, options, response) {
          if (!error) {
            timeTracked = $(response.html).find('td').text();
            t.set('card', 'shared', 'trackedtime', timeTracked);
            console.log("Tracked Time: ", timeTracked);
          } else {
            console.log('Traking ', error);
          }
        }
      });
    }
    if (gEstimationSheetUrl) {
      getValues = "select O WHERE D = '" + cardUrl + "'";
      console.log('gEstimationSheetUrl: ', gEstimationSheetUrl);
      console.log('getValues: ', getValues);
      sheetrock({
        url: gEstimationSheetUrl,
        query: getValues,
        callback: function (error, options, response) {
          if (!error) {
            timeEstimated = $(response.html).find('td').text();
            t.set('card', 'shared', 'estimatedtime', timeEstimated);
            console.log("Estimated Time: ", timeEstimated);
          } else {
            console.log('Estimation ', error);
          }
        }
      });
    }
    return t.get('card', 'shared', 'trackedtime')
    .then(function(trackedtime) {
      return t.get('card', 'shared', 'estimatedtime')
      .then(function(estimatedtime){
        return [
          {
            dynamic: function() {
              console.log('ok');
              return{
                title: 'Time Tracked',
                text: trackedtime,
                icon: './images/clock-track.svg', // for card front badges only
                color: 'red',
                refresh: 60
              }
            }
          },
          {
            dynamic: function() {
              console.log('ok2');
              return {
                title: 'Time Estimated',
                text: getEstimatedTime(t, gEstimationSheetUrl, cardUrl, getValues),
                icon: './images/clock-estimation.svg', // for card front badges only
                color: 'yellow',
                refresh: 60
              }
            }
          }
        ]
      })
    })
  })
};

@@ -1726,17 +1726,17 @@
 on: ', 4
-4
+5
 );%0A%0A  re
@@ -2659,24 +2659,27 @@
 );%0A    %7D%0A   
+ //
  if (gEstima
@@ -2689,32 +2689,35 @@
 nSheetUrl) %7B%0A   
+ //
    getValues = %22
@@ -2763,117 +2763,10 @@
     
-  console.log('gEstimationSheetUrl: ', gEstimationSheetUrl);%0A      console.log('getValues: ', getValues);%0A   
+//
    s
@@ -2771,32 +2771,35 @@
  sheetrock(%7B%0A   
+ //
      url: gEstim
@@ -2809,36 +2809,39 @@
 onSheetUrl,%0A    
+//
     
+ 
 query: getValues
@@ -2837,32 +2837,35 @@
 : getValues,%0A   
+ //
      callback: f
@@ -2896,32 +2896,35 @@
  response) %7B%0A   
+ //
        if (!erro
@@ -2923,32 +2923,35 @@
 f (!error) %7B%0A   
+ //
          timeEst
@@ -2990,32 +2990,35 @@
 td').text();%0A   
+ //
          t.set('
@@ -3067,32 +3067,34 @@
 mated);%0A    
+//
         
 console.log(
@@ -3073,32 +3073,33 @@
 ;%0A    //        
+ 
 console.log(%22Est
@@ -3125,32 +3125,35 @@
 eEstimated);%0A   
+ //
        %7D else %7B%0A
@@ -3147,32 +3147,35 @@
     %7D else %7B%0A   
+ //
          console
@@ -3197,32 +3197,35 @@
 n ', error);%0A   
+ //
        %7D%0A       
@@ -3212,32 +3212,35 @@
   //       %7D%0A   
+ //
      %7D%0A      %7D);
@@ -3230,33 +3230,39 @@
 /     %7D%0A    
+//
   
+ 
 %7D);%0A   
+ //
  %7D%0A    retur

fix getEstimationTime()