'use strict';

angular.module('app')
    .controller('rgiAssignAssessmentMultipleAssigneeDialogCtrl', function (
        $scope,
        $route,
        ngDialog,
        rgiNotifier,
        rgiAssessmentSrvc,
        rgiAssessmentMethodSrvc,
        rgiIdentitySrvc,
        rgiUserSrvc
    ) {
        $scope.current_user = rgiIdentitySrvc.currentUser;
        var assigneeField = $scope.userType + '_ID';

        rgiAssessmentSrvc.get({assessment_ID: $scope.$parent.assessment_update_ID}, function(assessment) {
            $scope.assessment = assessment;

            for(var assigneeId in $scope.assessment[assigneeField]) {
                if($scope.assessment[assigneeField].hasOwnProperty(assigneeId) && $scope.assessment[assigneeField][assigneeId]._id) {
                    $scope.assessment[assigneeField][assigneeId] = $scope.assessment[assigneeField][assigneeId]._id;
                }
            }

            if($scope.assessment[assigneeField].length === 0) {
                $scope.addAssignee();
            }

            rgiUserSrvc.query({role: $scope.userType}, function(users) {
                $scope.availableUsers = users;
            });
        });

        $scope.addAssignee = function () {
            $scope.assessment[assigneeField].push(undefined);
        };

        $scope.removeAssignee = function (index) {
            $scope.assessment[assigneeField].splice(index, 1);
        };

        $scope.isAssigneeListEmpty = function() {
            var assigneeNumber = 0;

            if($scope.assessment) {
                $scope.assessment[assigneeField].forEach(function(assignee) {
                    if(assignee) {
                        assigneeNumber++;
                    }
                });
            }

            return assigneeNumber < 1;
        };

        $scope.saveAssigneeList = function () {
            rgiAssessmentMethodSrvc.updateAssessment($scope.assessment)
                .then(function () {
                    rgiNotifier.notify('Assessment assigned!');
                    $scope.closeThisDialog();
                    $route.reload();
                }, function (reason) {
                    if (reason) {
                        rgiNotifier.error(reason);
                    } else {
                        rgiNotifier.error('Validation error');
                        $scope.closeThisDialog();
                        $route.reload();
                    }
                });
        };

        $scope.closeDialog = function () {
            ngDialog.close();
        };
    });

@@ -138,16 +138,28 @@
 $route,%0A
+        $q,%0A
         
@@ -270,24 +270,56 @@
 entitySrvc,%0A
+        rgiUserAssessmentsSrvc,%0A
         rgiU
@@ -396,32 +396,44 @@
 ser;%0A        var
+%0A           
  assigneeField =
@@ -456,16 +456,833 @@
  + '_ID'
+,%0A            originalAssigneeList = %5B%5D,%0A            getArrayDifference = function(original, modified) %7B%0A                var difference = %5B%5D;%0A%0A                original.forEach(function(item) %7B%0A                    if(modified.indexOf(item) === -1) %7B%0A                        difference.push(item);%0A                    %7D%0A                %7D);%0A%0A                return difference;%0A            %7D,%0A            getNonEmptyAssigneeList = function() %7B%0A                var assigneeList = %5B%5D;%0A%0A                if($scope.assessment) %7B%0A                    $scope.assessment%5BassigneeField%5D.forEach(function(assignee) %7B%0A                        if(assignee) %7B%0A                            assigneeList.push(assignee);%0A                        %7D%0A                    %7D);%0A                %7D%0A%0A                return assigneeList;%0A            %7D
 ;%0A%0A     
@@ -1774,32 +1774,110 @@
 %0A            %7D%0A%0A
+            originalAssigneeList = $scope.assessment%5BassigneeField%5D.slice();%0A%0A
             if($
@@ -2435,65 +2435,109 @@
     
-var assigneeNumber = 0;%0A
+return getNonEmptyAssigneeList().length %3C 1;
 %0A     
+   %7D;%0A%0A
        
-if(
+ 
 $scope.
-assessment
+saveAssigneeList = function (
 ) %7B%0A
@@ -2551,45 +2551,196 @@
     
+ 
-     $scope.assessment%5BassigneeField%5D
+var promises = %5B%5D;%0A            promises.push(rgiAssessmentMethodSrvc.updateAssessment($scope.assessment));%0A%0A            getArrayDifference(originalAssigneeList, getNonEmptyAssigneeList())
 .for
@@ -2749,32 +2749,41 @@
 ch(function(
-a
+removedA
 ssignee
+Id
 ) %7B%0A        
@@ -2794,108 +2794,226 @@
     
-    if(assignee) %7B%0A                        assigneeNumber++;%0A                    %7D%0A                %7D
+$scope.availableUsers.forEach(function(user) %7B%0A                    if((user._id === removedAssigneeId) && user.assessments) %7B%0A                        promises.push(rgiUserAssessmentsSrvc.remove(user, $scope.assessment)
 );%0A 
@@ -3019,35 +3019,32 @@
             
-%7D%0A%0A
         
     return a
@@ -3035,39 +3035,18 @@
     
+%7D%0A
     
-return assigneeNumber %3C 1;%0A
+    
     
@@ -3042,34 +3042,34 @@
                %7D
+)
 ;
-%0A
 %0A        $scope.
@@ -3065,118 +3065,44 @@
     
-$scope.saveAssigneeList = function () %7B%0A            rgiAssessmentMethodSrvc.updateAssessment($scope.assessment
+    %7D);%0A%0A            $q.all(promises
 )%0A  

remove supervisor and external reviewer assessment links