'use strict';

angular.module('chatspaces.controllers', [
  'chatspaces.dashboard',
  'chatspaces.friend',
  'chatspaces.profile',
  'chatspaces.blocked',
  'chatspaces.home',
  'chatspaces.message'
]).
controller('AppCtrl',
  function ($scope, authenticate, $rootScope, $http, $location, $routeParams, user, localCache, cameraHelper) {

  user.call();
  $rootScope.friendPredicate = '-username';
  $rootScope.recipients = {};
  $rootScope.recipientAvatars = [];
  $rootScope.latestThreadMessage;
  $rootScope.dashboardList = [];
  $rootScope.showCamera = false;
  $rootScope.showFollowing = false;

  socket.on('friend', function (data) {
    $rootScope.$apply(function () {
      $rootScope.friends[data.friend.userHash] = {
        username: data.friend.username,
        avatar: data.friend.avatar,
        userHash: data.friend.userHash,
        senderUserHash: data.friend.senderUserHash
      };
    });
  });

  socket.on('notification', function (data) {
    $rootScope.$apply(function () {
      if (data && $rootScope.notifications.indexOf(data) === -1 &&
         (!$routeParams.senderKey || $routeParams.senderKey !== data)) {
        $rootScope.notifications.push(data);
        $rootScope.latestMessage = data;
      }
    });
  });

  socket.on('message', function (data) {
    $rootScope.$apply(function () {
      var senderKey = data.value.reply || data.value.senderKey;

      if ($location.path() === '/dashboard' || $routeParams.senderKey === senderKey) {
        var key = data.value.reply || data.key;

        // also save message to local cache
        data.updated = data.value.created;

        localForage.setItem($rootScope.userHash + ':message[' + data.key + ']', data);
        localForage.setItem($rootScope.userHash + ':latestMessageKey', key); // last one at the top is the latest dashboard key
        localCache.setItem(key, data);

        $rootScope.$apply(function () {
          $rootScope.messages[data.key] = data;

          if (data.value.recipientAvatars) {
            $rootScope.recipientAvatars = data.value.recipientAvatars;
          }

          if ($routeParams.senderKey === senderKey) {
            $rootScope.latestThreadMessage = data.key; // last one at the top is the latest thread key
            data.value.recipients.forEach(function (userHash) {
              $rootScope.recipients[userHash] = userHash;
            });

            $rootScope.reply = senderKey;
          } else {
            $rootScope.latestMessage = key;
          }
        });
      }
    });
  });

  socket.on('blocked', function (data) {
    $rootScope.$apply(function () {
      $rootScope.blocked[data.user.userHash] = {
        username: data.user.username,
        avatar: data.user.avatar,
        userHash: data.user.userHash
      };
    });
  });

  $scope.loadDashboard = function () {
    var since = '';

    if ($rootScope.latestMessage) {
      since = '?since=' + $rootScope.latestMessage;
    }

    $http({
      url: '/api/feed?since=' + since,
      method: 'GET'
    }).success(function (data) {
      $location.path('/dashboard');
    });
  };

  $scope.getDate = function (timestamp) {
    return moment.unix(Math.round(timestamp / 1000)).fromNow();
  };

  $rootScope.toggleSettings = function () {
    if ($rootScope.settings) {
      $rootScope.settings = false;
    } else {
      $rootScope.settings = true;
    }
  };

  $scope.newMessage = function () {
      $rootScope.settings = false;
  };

  localForage.getItem('personaEmail', function (email) {
    if (email) {
      $rootScope.isAuthenticated = true;
    }
  });

  $scope.logout = function () {
    authenticate.logout();
  }

  $rootScope.promptCamera = function () {
    if ($rootScope.isAuthenticated && navigator.getMedia) {
      $rootScope.showCamera = true;
      cameraHelper.startStream();
    } else {
      $rootScope.cancelCamera();
    }
  };

  $rootScope.cancelCamera = function () {
    $rootScope.showCamera = false;
    $rootScope.showFollowing = false;
    $('#video-preview').empty();
    cameraHelper.resetStream();
  };
});

@@ -1287,46 +1287,8 @@
 ) %7B%0A
-    $rootScope.$apply(function () %7B%0A  
     
@@ -1342,26 +1342,24 @@
 senderKey;%0A%0A
-  
     if ($loc
@@ -1437,18 +1437,16 @@
 %7B%0A      
- 
- 
 var key 
@@ -1484,18 +1484,16 @@
 %0A%0A      
-  
 // also 
@@ -1520,18 +1520,16 @@
 l cache%0A
-  
       da
@@ -1564,26 +1564,24 @@
 ted;%0A%0A      
-  
 localForage.
@@ -1643,26 +1643,24 @@
 '%5D', data);%0A
-  
       localF
@@ -1769,26 +1769,24 @@
 shboard key%0A
-  
       localC
@@ -1813,26 +1813,24 @@
 ta);%0A%0A      
-  
 $rootScope.$
@@ -1845,26 +1845,24 @@
 nction () %7B%0A
-  
         $roo
@@ -1896,18 +1896,16 @@
  data;%0A%0A
-  
         
@@ -1941,34 +1941,32 @@
 rs) %7B%0A          
-  
 $rootScope.recip
@@ -2016,23 +2016,19 @@
         
-  
 %7D%0A%0A
-  
         
@@ -2073,34 +2073,32 @@
 ey) %7B%0A          
-  
 $rootScope.lates
@@ -2178,26 +2178,24 @@
 y%0A          
-  
 data.value.r
@@ -2238,34 +2238,32 @@
 ) %7B%0A            
- 
- 
 $rootScope.recip
@@ -2300,25 +2300,21 @@
         
-  
 %7D);%0A%0A
-  
         
@@ -2353,18 +2353,16 @@
         
- 
- 
 %7D else %7B
@@ -2358,26 +2358,24 @@
    %7D else %7B%0A
-  
           $r
@@ -2416,18 +2416,16 @@
     
-  
 %7D%0A      
   %7D)
@@ -2424,18 +2424,16 @@
     
- 
- 
 %7D);%0A    
   %7D%0A
@@ -2424,35 +2424,25 @@
     %7D);%0A    
-  %7D%0A    %7D);
+%7D
 %0A  %7D);%0A%0A  so

remove other apply