// list of messages' details; each message contains the details: messageid, isread, sentdate, first name, last name [IN THIS ORDER]
        public IList <MessageListing> GetAllMessages(int receiverID)
        {
            try
            {
                IList <IList <string> > listOfMessagesDetails = _messageService.GetInbox(_dao, receiverID, true);
                var messageInbox = new List <MessageListing>(); // Container for all message listings in the inbox
                foreach (IList <string> message in listOfMessagesDetails)
                {
                    // Combine first and last to get full name
                    StringBuilder sb = new StringBuilder(message[3]);
                    sb.Append(" ");
                    sb.Append(message[4]);
                    var formatDate = DateTime.Parse(message[2]).ToString("f", CultureInfo.CreateSpecificCulture("en-US"));

                    var MessageListing = new MessageListing()
                    {
                        MessageID = Int32.Parse(message[0]),
                        IsRead    = bool.Parse(message[1]),
                        SentDate  = formatDate,
                        FullName  = sb.ToString()
                    };

                    messageInbox.Add(MessageListing);
                }
                return(messageInbox);
            }
            catch (Exception e)
            {
                _errorHandler.Handle(e);
                return(null); // NOTE: should this be a different return type? If kept, controller should check if null
            }
        }
        public IResult UpdateAccount(User user)
        {
            //var id = _updatePermissionService.SysIdFinder(user);
            //foreach (string s in Enum.GetValues(typeof(StandardUser)))
            //{
            //    perms.AddPermission(s);
            //}
            //for (int i = 0; i < 3; i++)
            //{
            //    try
            //    {
            //        _updatePermissionService._permissions = new List<Permission> { perms };
            //        var permResult = _updatePermissionService.Update();
            //        if (permResult.IsSuccess)
            //            break;
            //    }
            //    catch (Exception e)
            //    {
            //        ErrorController handler = new ErrorController();
            //        handler.Handle(e);
            //    }
            //    if (i == 2)
            //        throw new Exception("Failed to create Permissions");
            //}

            _updateAccountService._newUsers = new List <User> {
                user
            };
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    var updateAccountResult = _updateAccountService.Update();
                    if (updateAccountResult.IsSuccess)
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    ErrorController handler = new ErrorController();
                    handler.Handle(e);
                }
                if (i == 2)
                {
                    throw new Exception("Failed to create Account");
                }
            }

            return(new CheckResult("Account updated successfully", true));
        }
        public string LoginAccount(LoginAttemptModel account)
        {
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    _authNService._userEmail = account.Email;
                    var authNSuccess = _authNService.Authenticate(account.Password);
                    if (authNSuccess)
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    var handler = new ErrorController();
                    handler.Handle(e);
                }
                if (i == 2)
                {
                    throw new Exception("Failed to Authenticate User");
                }
            }

            for (int i = 0; i < 3; i++)
            {
                try
                {
                    var user = _authNService.FindUser(account);
                    if (user is object)
                    {
                        return(_authService.GenerateJWT(user));
                    }
                }
                catch (Exception e)
                {
                    var handler = new ErrorController();
                    handler.Handle(e);
                }
            }


            throw new Exception("Failed to Login");
        }
        public IResult DeleteAccount(User user)
        {
            var users = new List <User> {
                user
            };

            _deleteAccountService._targetUsers = users;

            var userId = _updatePermissionService.SysIdFinder(user);

            for (int i = 0; i < 3; i++)
            {
                Permission perms = new Permission(userId);
                foreach (string s in Enum.GetValues(typeof(AuthZEnum)))
                {
                    perms.RemovePermission(s);
                }
                try
                {
                    _updatePermissionService._permissions = new List <Permission> {
                        perms
                    };
                    var permResult = _updatePermissionService.Update();
                    if (permResult.IsSuccess)
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    ErrorController handler = new ErrorController();
                    handler.Handle(e);
                }
                if (i == 2)
                {
                    throw new Exception("Permissions failed to delete");
                }
            }
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    var deleteResult = _deleteAccountService.Delete();
                    if (deleteResult.IsSuccess)
                    {
                        LogService.Log(deleteResult.Message);
                        return(deleteResult);
                    }
                }
                catch (Exception e)
                {
                    ErrorController handler = new ErrorController();
                    handler.Handle(e);
                }
                if (i == 2)
                {
                    throw new Exception("Failed to delete Account");
                }
            }
            return(new CheckResult("Failed to Delete Account", false));
        }
        public IResult CreateAccount(Account account, User user)
        {
            var users = new List <User> {
                user
            };
            var accounts = new List <Account> {
                account
            };

            _createAccountService._newAccounts = accounts;
            _updateAccountService._newUsers    = users;
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    var createAccountResult = _createAccountService.Create();
                    if (createAccountResult.IsSuccess)
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    ErrorController handler = new ErrorController();
                    handler.Handle(e);
                }
                if (i == 2)
                {
                    throw new Exception("Failed to create Account");
                }
            }
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    var updateAccountResult = _updateAccountService.Update();
                    if (updateAccountResult.IsSuccess)
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    ErrorController handler = new ErrorController();
                    handler.Handle(e);
                }
                if (i == 2)
                {
                    throw new Exception("Failed to create Account");
                }
            }
            var        id    = _updatePermissionService.SysIdFinder(user);
            Permission perms = new Permission(id);

            foreach (string s in Enum.GetValues(typeof(StandardUser)))
            {
                perms.AddPermission(s);
            }
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    _updatePermissionService._permissions = new List <Permission> {
                        perms
                    };
                    var permResult = _updatePermissionService.Update();
                    if (permResult.IsSuccess)
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    ErrorController handler = new ErrorController();
                    handler.Handle(e);
                }
                if (i == 2)
                {
                    throw new Exception("Failed to create Permissions");
                }
            }


            var checkResult = new CheckResult("Account Successfully Created", true);

            LogService.Log(checkResult.Message);
            return(checkResult);
        }