'use strict';

angular.module('copayApp.controllers').controller('tabSendController', function($scope, $log, $timeout, $ionicScrollDelegate, addressbookService, profileService, lodash, $state, walletService, incomingData, popupService) {

  var originalList;
  var CONTACTS_SHOW_LIMIT;
  var currentContactsPage;


  var updateList = function() {
    CONTACTS_SHOW_LIMIT = 10;
    currentContactsPage = 0;
    originalList = [];

    var wallets = profileService.getWallets({
      onlyComplete: true
    });
    $scope.hasWallets = lodash.isEmpty(wallets) ? false : true;
    $scope.oneWallet = wallets.length == 1;

    if (!$scope.oneWallet) {
      lodash.each(wallets, function(v) {
        originalList.push({
          color: v.color,
          name: v.name,
          isWallet: true,
          getAddress: function(cb) {
            walletService.getAddress(v, false, cb);
          },
        });
      });
    }

    addressbookService.list(function(err, ab) {
      if (err) $log.error(err);

      $scope.hasContacts = lodash.isEmpty(ab) ? false : true;
      var completeContacts = [];
      lodash.each(ab, function(v, k) {
        completeContacts.push({
          name: lodash.isObject(v) ? v.name : v,
          address: k,
          email: lodash.isObject(v) ? v.email : null,
          getAddress: function(cb) {
            return cb(null, k);
          },
        });
      });

      var contacts = completeContacts.slice(0, (currentContactsPage + 1) * CONTACTS_SHOW_LIMIT);
      $scope.contactsShowMore = completeContacts.length > contacts.length;
      originalList = originalList.concat(contacts);
      $scope.list = lodash.clone(originalList);

      $timeout(function() {
        $ionicScrollDelegate.resize();
        $scope.$apply();
      });
    });
  };

  $scope.showMore = function() {
    currentContactsPage++;
    updateList();
  };

  $scope.findContact = function(search) {

    if (incomingData.redir(search)) {
      return;
    }

    if (!search || search.length < 2) {
      $scope.list = originalList;
      $timeout(function() {
        $scope.$apply();
      });
      return;
    }

    var result = lodash.filter(originalList, function(item) {
      var val = item.name;
      return lodash.includes(val.toLowerCase(), search.toLowerCase());
    });

    $scope.list = result;
  };

  $scope.goToAmount = function(item) {
    $timeout(function() {
      item.getAddress(function(err, addr) {
        if (err || !addr) {
          $log.error(err);
          return;
        }
        $log.debug('Got toAddress:' + addr + ' | ' + item.name);
        return $state.transitionTo('tabs.send.amount', {
          isWallet: item.isWallet,
          toAddress: addr,
          toName: item.name,
          toEmail: item.email
        })
      });
    });
  };


  var updateHasFunds = function() {
    $scope.hasFunds = true;

    var wallets = profileService.getWallets({
      onlyComplete: true,
    });

    if (!wallets || !wallets.length) {
      $scope.hasFunds = false;
      $timeout(function() {
        $scope.$apply();
      });
    }

    var index = 0;
    lodash.each(wallets, function(w) {
      walletService.getStatus(w, {}, function(err, status) {
        ++index;
        if (err || !status) {
          $log.error(err);
          return;
        }
        if (status.availableBalanceSat && status.availableBalanceSat > 0) {
          $scope.hasFunds = true;
        }
        else $scope.hasFunds = false;
        if (index == wallets.length) {
          $scope.hasFunds = $scope.hasFunds || false;
        }
        $timeout(function() {
          $scope.$apply();
        })
      });
    });
  };

  $scope.$on("$ionicView.beforeEnter", function(event, data) {
    $scope.formData = {
      search: null
    };
    updateList();
    updateHasFunds();
  });

  // This could probably be enhanced refactoring the routes abstract states
  $scope.createWallet = function() {
    $state.go('tabs.home').then(function() {
      $state.go('tabs.add.create-personal');
    });
  };

  $scope.buyBitcoin = function() {
    $state.go('tabs.home').then(function() {
      $state.go('tabs.buyandsell.glidera');
    });
  };

});

@@ -2856,20 +2856,20 @@
 Funds = 
-true
+null
 ;%0A%0A    v
@@ -3018,71 +3018,8 @@
 se;%0A
-      $timeout(function() %7B%0A        $scope.$apply();%0A      %7D);%0A
     
@@ -3234,32 +3234,33 @@
 turn;%0A        %7D%0A
+%0A
         if (stat
@@ -3285,42 +3285,8 @@
 eSat
- && status.availableBalanceSat %3E 0
 ) %7B%0A
@@ -3333,46 +3333,8 @@
   %7D%0A
-        else $scope.hasFunds = false;%0A
     
@@ -3418,24 +3418,24 @@
 s %7C%7C false;%0A
+
         %7D%0A  
@@ -3436,76 +3436,8 @@
   %7D%0A
-        $timeout(function() %7B%0A          $scope.$apply();%0A        %7D)%0A
     

Revert "fix send tip"