'use strict';

/**
 * @ngdoc function
 * @name Teem.controller:ProjectsCtrl
 * @description
 * # ProjectsCtrl
 * Controller of the Teem
 */

angular.module('Teem')
  .config(['$routeProvider', function ($routeProvider) {
    $routeProvider
      .when('/communities/:communityId/projects', {
        templateUrl: 'projects/index.html',
        controller: 'ProjectsCtrl'
      })
      .when('/home/projects', {
        templateUrl: 'projects/index.html',
        controller: 'ProjectsCtrl'
      })
      .when('/projects', {
        templateUrl: 'projects/index.html',
        controller: 'ProjectsCtrl'
      });
  }])
  .controller('ProjectsCtrl', [
  'SessionSvc', 'url', '$scope', '$location', '$route', 'time',
  'CommunitiesSvc', 'ProjectsSvc', 'ProfilesSvc', '$timeout', 'Loading',
  '$rootScope',
  function (SessionSvc, url, $scope, $location, $route, time,
  CommunitiesSvc, ProjectsSvc, ProfilesSvc, $timeout, Loading, $rootScope) {
    var communityId = $route.current.params.communityId;

    $scope.translationData = {};

    if (communityId) {
      $scope.context = 'community';
    } else if ($location.path() === '/home/projects') {
      $scope.context = 'home';
    } else {
      $scope.context = 'public';
    }

    SessionSvc.onLoad(function(){
      switch ($scope.context) {
        case 'community':
          Loading.show(CommunitiesSvc.findByUrlId($route.current.params.communityId)).
            then(function(community){
              $scope.community = community;

              $scope.translationData.community = community.name;

              Loading.show(community.myAndPublicProjects()).
                then(function (projects){

                  $scope.projects = projects;

                });
            });

          break;
        case 'home':
          SessionSvc.loginRequired($scope, function() {
            Loading.show(ProjectsSvc.all({ contributor: SessionSvc.users.current() })).
              then(function(projects) {

                $scope.projects = projects;

                $scope.translationData.count = projects.length;

                $rootScope.$broadcast('teem.project.count', projects.length);
              });
          });

          break;
        default:
          Loading.show(ProjectsSvc.all({ shareMode: 'public' })).
            then(function(projects) {

              $scope.projects = projects;
            });
      }
    });
  }]);

@@ -1230,16 +1230,447 @@
 %0A    %7D%0A%0A
+    function getCommunities(projects) %7B%0A      angular.forEach(projects, function(p) %7B%0A        angular.forEach(p.communities, function(id) %7B%0A          CommunitiesSvc.find(id).then(function(c) %7B%0A            $timeout(function() %7B%0A              if (! p.loadedCommunities) %7B%0A                p.loadedCommunities = %5B%5D;%0A              %7D%0A%0A              p.loadedCommunities.push(c);%0A            %7D);%0A          %7D);%0A        %7D);%0A      %7D);%0A    %7D%0A%0A
     Sess
@@ -1695,16 +1695,16 @@
 tion()%7B%0A
-
       sw
@@ -2390,32 +2390,74 @@
 tion(projects) %7B
+%0A                getCommunities(projects);
 %0A%0A              
@@ -2759,24 +2759,24 @@
 ublic' %7D)).%0A
-
             
@@ -2800,16 +2800,56 @@
 jects) %7B
+%0A              getCommunities(projects);
 %0A%0A      

Revert "Remove unneeded code which causes performance issues"