'use strict';

angular.module('publisherApp')
    .controller('TargetsPublishController', function ($scope, $state, $filter, Subject, Group, User, loadedTargets) {
        $scope.$parent.targets = $scope.$parent.targets || [];

        $scope.selectedtargets = $scope.$parent.targets;

        $scope.search = {target: {}};



        if (angular.equals([],$scope.$parent.targets) && loadedTargets) {
            //console.log('loaded targets :' + JSON.stringify(loadedTargets));
            // on récupère le subject car pas utile de mettre le subscribeType car FORCED par défaut
            if (loadedTargets.length > 0) {
                for (var i = 0; i < loadedTargets.length; ++i) {
                    $scope.$parent.targets.push(angular.copy(loadedTargets[i].subject));
                }
            }
        }

        $scope.$watch('search.target', function(newData, oldData) {
            console.log("search selected :", newData, oldData);
            if (angular.isDefined(newData) && !angular.equals({},newData) && newData.hasOwnProperty('modelId') && !containsSubcriber(newData.modelId)) {
                var target = {modelId :newData.modelId, displayName: newData.displayName};
                $scope.$parent.targets.push(target);

            }
        });


        /*$scope.submitUsers = function() {
            $scope.selectedtargets.push.apply($scope.selectedtargets,$scope.checkedUsers);
            $scope.$parent.targets = [];
            for(var i = 0; i < $scope.selectedtargets.length; ++i) {
                var target = {modelId :$scope.selectedtargets[i].modelId, displayName: $scope.selectedtargets[i].displayName};
                $scope.$parent.targets.push(target);
            }
            $scope.checkedUsers = [];
            $scope.type="";
            $("#userListModal").modal("hide");
        };

        $scope.submitGroups = function() {
            $scope.selectedtargets.push.apply($scope.selectedtargets,$scope.checkedGroups);
            $scope.$parent.targets = [];
            for(var i = 0; i < $scope.selectedtargets.length; ++i) {
                var target = {modelId :$scope.selectedtargets[i].modelId, displayName: $scope.selectedtargets[i].displayName};
                $scope.$parent.targets.push(target);
            }
            $scope.checkedGroups = [];
            $scope.type="";
        };*/

        $scope.remove = function(subject) {
            /*$scope.selectedtargets = $scope.selectedtargets.filter(function(element) {
                return element.modelId.keyId != subject.modelId.keyId;
            });*/
            $scope.$parent.targets = $scope.$parent.targets.filter(function(element) {
                return element.modelId.keyId != subject.modelId.keyId;
            });
        };

        $scope.viewSubject= function(subject) {
            if (subject.modelId) {
                $state.go('publish.targets.subject', subject.modelId);
            }
            return false;
        };

        function containsSubcriber (subjectKey) {
            var list = $scope.$parent.targets;
            if (list.length > 0) {
                for (var i = 0, size = list.length; i < size; i++) {
                    //console.log("checking equals ", list[i], subjectKey);
                    if (angular.equals(list[i].modelId, subjectKey)) {
                        return true;
                    }
                }
            }
            return false;
        }
    });


@@ -1276,1314 +1276,51 @@
 );%0A%0A
-%0A        /*$scope.submitUsers = function() %7B%0A            $scope.selectedtargets.push.apply($scope.selectedtargets,$scope.checkedUsers);%0A            $scope.$parent.targets = %5B%5D;%0A            for(var i = 0; i %3C $scope.selectedtargets.length; ++i) %7B%0A                var target = %7BmodelId :$scope.selectedtargets%5Bi%5D.modelId, displayName: $scope.selectedtargets%5Bi%5D.displayName%7D;%0A                $scope.$parent.targets.push(target);%0A            %7D%0A            $scope.checkedUsers = %5B%5D;%0A            $scope.type=%22%22;%0A            $(%22#userListModal%22).modal(%22hide%22);%0A        %7D;%0A%0A        $scope.submitGroups = function() %7B%0A            $scope.selectedtargets.push.apply($scope.selectedtargets,$scope.checkedGroups);%0A            $scope.$parent.targets = %5B%5D;%0A            for(var i = 0; i %3C $scope.selectedtargets.length; ++i) %7B%0A                var target = %7BmodelId :$scope.selectedtargets%5Bi%5D.modelId, displayName: $scope.selectedtargets%5Bi%5D.displayName%7D;%0A                $scope.$parent.targets.push(target);%0A            %7D%0A            $scope.checkedGroups = %5B%5D;%0A            $scope.type=%22%22;%0A        %7D;*/%0A%0A        $scope.remove = function(subject) %7B%0A            /*$scope.selectedtargets = $scope.selectedtargets.filter(function(element) %7B%0A                return element.modelId.keyId != subject.modelId.keyId;%0A            %7D);*/
+        $scope.remove = function(subject) %7B
 %0A   
@@ -1426,16 +1426,32 @@
  return 
+!angular.equals(
 element.
@@ -1461,17 +1461,9 @@
 elId
-.keyId !=
+,
  sub
@@ -1478,14 +1478,9 @@
 elId
-.keyId
+)
 ;%0A  

solve potential error on removing selected targets when publishing, a better filter is done