﻿(function () {
    'use strict';

    angular
        .module('GVA.Creation')
        .controller('ManageInviteesController', ManageInviteesController);

    ManageInviteesController.$inject = ['$scope', '$routeParams', '$location', 'ManageService', 'RoutingService'];


    function ManageInviteesController($scope, $routeParams, $location, ManageService, RoutingService) {

        $scope.poll = ManageService.poll;
        $scope.manageId = $routeParams.manageId;

        $scope.emailUpdated = emailUpdated;
        $scope.addInvitee = addInvitee;
        $scope.deletePendingVoter = deletePendingVoter;
        $scope.deleteInvitedVoter = deleteInvitedVoter;
        $scope.sendInvitations = sendInvitations;
        $scope.inviteString = '';

        $scope.saveChanges = updatePoll;
        $scope.discardChanges = returnToManage;

        $scope.isSaving = false;

        $scope.pendingUsers = [];
        $scope.invitedUsers = [];

        activate();

        var splitterTest = /[\n\s;>]+/;
        var emailRegex = /[\w._%+-]+@\w+(\.\w+)+/;

        function emailUpdated() {
            if (hasTerminatingCharacter($scope.inviteString)) {
                var allEmails = $scope.inviteString.trimLeft().split(splitterTest);

                var newEmails = allEmails.slice(0, -1);

                newEmails = newEmails
                    .filter(function (d) {
                        // Only add email-like tokens
                        return emailRegex.test(d);
                    }).map(function (d) {
                        // Parse to extract the email-like section. 
                        // E.g. Turns "Joe Bloggs <jbloggs@example.com>" into "jbloggs@example.com
                        return d.match(emailRegex)[0];
                    });

                for (var i = 0; i < newEmails.length; i++) {
                    addInvitee(newEmails[i]);
                }

                $scope.$apply();
            }
        }

        function deletePendingVoter(pending) {
            var indexOfInvitee = $scope.pendingUsers.indexOf(pending);
            $scope.pendingUsers.splice(indexOfInvitee, 1);
        }

        function deleteInvitedVoter(invitee) {
            var indexOfInvitee = $scope.invitedUsers.indexOf(invitee);
            $scope.invitedUsers.splice(indexOfInvitee, 1);
        }

        function addInvitee(invitee) {
            if (!invitee.match(emailRegex)) {
                return;
            }

            var allEmails = $scope.pendingUsers.concat($scope.invitedUsers);
            // Avoid duplicate invitations
            var existingEmails = allEmails.filter(function (d) {
                return (d.Email === invitee);
            });

            if (existingEmails.length === 0) {
                var newInvitee = { Email: invitee, EmailSent: false };
                $scope.pendingUsers.push(newInvitee);
            }


            // if inviteString.endsWith(invitee). Curse your obsfucation, Javascript!
            if ($scope.inviteString.indexOf(invitee, $scope.inviteString.length - invitee.length) !== 1) {
                $scope.inviteString = '';
            }
            else {
                $scope.inviteString = $scope.inviteeString.split(invitee)[1];
            }
        }

        function hasTerminatingCharacter(value) {
            return splitterTest.test(value);
        }

        function sendInvitations() {
            $scope.sendingInvitations = true;

            var existingInvitations = $scope.Invitations.map(function (d) {
                d.SendInvitation = false;
                return d;
            });

            var newInvitations = $scope.pendingUsers.map(function (d) {
                d.SendInvitation = true;
                return d;
            });

            var invitations = existingInvitations.concat(newInvitations);

            ManageService.sendInvitations($scope.manageId, invitations, function () {
                $scope.sendingInvitations = false;

                // We don't just do ManageService.getPoll, because we want to maintain any deleted "Invited" voters which have not yet been saved
                $scope.invitedUsers = $scope.invitedUsers.concat($scope.pendingUsers);
                $scope.pendingUsers = [];
            }, function () {
                $scope.sendingInvitations = false;
            });
        }

        function updatePoll() {
            // Apply pending change
            addInvitee($scope.inviteString);

            var invitations = $scope.invitedUsers.concat($scope.pendingUsers);

            ManageService.sendInvitations($scope.manageId, invitations, function () {
                returnToManage();
            });
        }

        function filterUsersByPending() {
            $scope.pendingUsers = [];
            $scope.invitedUsers = [];

            if ($scope.Invitations === undefined) {
                return;
            }

            for (var i = 0; i < $scope.Invitations.length; i++) {
                var voter = $scope.Invitations[i];

                if (voter.Email === null) {
                    continue;
                }
                
                if (voter.EmailSent) {
                    $scope.invitedUsers.push(voter);
                }
                else {
                    $scope.pendingUsers.push(voter);
                }
            }
        }

        function returnToManage() {
            RoutingService.navigateToManagePage($scope.manageId);
        }

        function activate() {
            ManageService.getInvitations($routeParams.manageId, function (data) {
                $scope.Invitations = data;
                filterUsersByPending();
            });

            filterUsersByPending();

            var inputField = document.getElementById('new-invitee');
            inputField.addEventListener('keydown', function (e) {
                if (e.keyCode === 13) { // User pressed "return key"
                    $scope.inviteString += '\n';
                    emailUpdated();
                }
            });
        }
    }
})();

@@ -3395,24 +3395,108 @@
 tations() %7B%0A
+            if ($scope.sendingInvitations) %7B%0A                return;%0A            %7D%0A%0A
             
@@ -3529,16 +3529,28 @@
 = true;%0A
+            
 %0A       
@@ -3587,26 +3587,27 @@
  $scope.
-I
+i
 nvit
-ation
+edUser
 s.map(fu
@@ -4025,60 +4025,8 @@
 ) %7B%0A
-                $scope.sendingInvitations = false;%0A%0A
     
@@ -4287,32 +4287,84 @@
 ndingUsers = %5B%5D;
+%0A%0A                $scope.sendingInvitations = false;
 %0A            %7D, 
@@ -4568,17 +4568,16 @@
 tring);%0A
-%0A
         
@@ -4584,163 +4584,27 @@
     
-var invitations = $scope.invitedUsers.concat($scope.pendingUsers);%0A%0A            ManageService.sendInvitations($scope.manageId, invitations, function () %7B%0A 
+sendInvitations();%0A
     
@@ -4607,27 +4607,24 @@
             
-   
 returnToMana
@@ -4629,32 +4629,16 @@
 nage();%0A
-            %7D);%0A
         

Fix multiple email sending