public UnreadFriendMessageList GetUnreadMessages(AccountViewModel account)
        {
            var unreadMessages = new List <FacebookMessageModel>();
            var userAgent      = new GetUserAgentQueryHandler(new DataBaseContext()).Handle(new GetUserAgentQuery
            {
                UserAgentId = account.UserAgentId
            });

            var newCorrespondenceRequests = new GetCorrespondenceRequestsEndine().Execute(new GetCorrespondenceRequestsModel
            {
                AccountFacebookId = account.FacebookId,
                Cookie            = account.Cookie,
                Proxy             = _accountManager.GetAccountProxy(account),
                Driver            = _seleniumManager.RegisterNewDriver(new AccountViewModel
                {
                    ProxyLogin    = account.ProxyLogin,
                    ProxyPassword = account.ProxyPassword,
                    Proxy         = account.Proxy,
                    UserAgentId   = account.UserAgentId
                })
            });

            unreadMessages.AddRange(newCorrespondenceRequests);

            var newUnreadMessages = new GetUnreadMessagesEngine().Execute(new GetUnreadMessagesModel
            {
                AccountId     = account.FacebookId,
                Cookie        = account.Cookie,
                Proxy         = _accountManager.GetAccountProxy(account),
                UrlParameters = new GetUrlParametersQueryHandler(new DataBaseContext()).Handle(new GetUrlParametersQuery
                {
                    NameUrlParameter = NamesUrlParameter.GetUnreadMessages
                }),
                UserAgent = userAgent.UserAgentString
            });

            foreach (var facebookMessageModel in newUnreadMessages)
            {
                if (
                    newCorrespondenceRequests.Any(
                        model => model.FriendFacebookId == facebookMessageModel.FriendFacebookId))
                {
                    continue;
                }

                unreadMessages.Add(facebookMessageModel);
            }

            if (unreadMessages.Count == 0)
            {
                return(new UnreadFriendMessageList
                {
                    UnreadMessages = unreadMessages.Select(model => new UnreadFriendMessageModel
                    {
                        FriendFacebookId = model.FriendFacebookId,
                        UnreadMessage = model.UnreadMessage,
                        LastMessage = model.LastMessage,
                        CountAllMessages = model.CountAllMessages,
                        CountUnreadMessages = model.CountUnreadMessages,
                        LastReadMessageDateTime = model.LastReadMessageDateTime,
                        LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                        FriendGender = model.Gender,
                        FriendName = model.Name,
                        FriendHref = model.Href,
                        FriendType = model.FriendType
                    }).ToList()
                });
            }

            new SaveUnreadMessagesCommandHandler(new DataBaseContext()).Handle(new SaveUnreadMessagesCommand()
            {
                AccountId      = account.Id,
                UnreadMessages = unreadMessages.Select(model => new FacebookMessageDbModel
                {
                    AccountId           = model.AccountId,
                    FriendFacebookId    = model.FriendFacebookId,
                    Gender              = model.Gender,
                    CountAllMessages    = model.CountAllMessages,
                    CountUnreadMessages = model.CountUnreadMessages,
                    Href                      = model.Href,
                    LastMessage               = model.LastMessage,
                    LastReadMessageDateTime   = model.LastReadMessageDateTime,
                    LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                    Name                      = model.Name,
                    UnreadMessage             = model.UnreadMessage,
                    FriendType                = model.FriendType
                }).ToList()
            });

            var ureadMessages = unreadMessages.Select(model => new UnreadFriendMessageModel
            {
                FriendFacebookId          = model.FriendFacebookId,
                UnreadMessage             = model.UnreadMessage,
                LastMessage               = model.LastMessage,
                CountAllMessages          = model.CountAllMessages,
                CountUnreadMessages       = model.CountUnreadMessages,
                LastReadMessageDateTime   = model.LastReadMessageDateTime,
                LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                FriendGender              = model.Gender,
                FriendName = model.Name,
                FriendHref = model.Href
            }).ToList();

            return(new UnreadFriendMessageList
            {
                UnreadMessages = ureadMessages
            });
        }
        public UnreadFriendMessageList GetUnreadMessages(AccountViewModel account)
        {
            const string functionName   = "Получение непрочитанных сообщений";
            var          unreadMessages = new List <FacebookMessageModel>();
            List <UnreadFriendMessageModel> ureadMessages = null;

            try
            {
                var userAgent = new GetUserAgentQueryHandler(new DataBaseContext()).Handle(new GetUserAgentQuery
                {
                    UserAgentId = account.UserAgentId
                });

                var newCorrespondenceRequests = new GetCorrespondenceRequestsEndine().Execute(
                    new GetCorrespondenceRequestsModel
                {
                    AccountFacebookId = account.FacebookId,
                    Cookie            = account.Cookie,
                    Proxy             = _accountManager.GetAccountProxy(account),
                    Driver            = _seleniumManager.RegisterNewDriver(new AccountViewModel
                    {
                        ProxyLogin    = account.ProxyLogin,
                        ProxyPassword = account.ProxyPassword,
                        Proxy         = account.Proxy,
                        UserAgentId   = account.UserAgentId
                    })
                });

                unreadMessages.AddRange(newCorrespondenceRequests);

                var newUnreadMessages = new GetUnreadMessagesEngine().Execute(new GetUnreadMessagesModel
                {
                    AccountId     = account.FacebookId,
                    Cookie        = account.Cookie,
                    Proxy         = _accountManager.GetAccountProxy(account),
                    UrlParameters = new GetUrlParametersQueryHandler(new DataBaseContext()).Handle(
                        new GetUrlParametersQuery
                    {
                        NameUrlParameter = NamesUrlParameter.GetUnreadMessages
                    }),
                    UserAgent          = userAgent.UserAgentString,
                    NumbersOfDialogues = 10
                });

                foreach (var facebookMessageModel in newUnreadMessages)
                {
                    if (newCorrespondenceRequests.Any(model => model.FriendFacebookId == facebookMessageModel.FriendFacebookId))
                    {
                        continue;
                    }

                    if (facebookMessageModel.LastMessage.Equals(string.Empty))
                    {
                        //todo: MARK UNREAD
                    }

                    unreadMessages.Add(facebookMessageModel);
                }

                if (unreadMessages.Count == 0)
                {
                    return(new UnreadFriendMessageList
                    {
                        UnreadMessages = unreadMessages.Select(model => new UnreadFriendMessageModel
                        {
                            FriendFacebookId = model.FriendFacebookId,
                            UnreadMessage = model.UnreadMessage,
                            LastMessage = model.LastMessage,
                            CountAllMessages = model.CountAllMessages,
                            CountUnreadMessages = model.CountUnreadMessages,
                            LastReadMessageDateTime = model.LastReadMessageDateTime,
                            LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                            FriendGender = model.Gender,
                            FriendName = model.Name,
                            FriendHref = model.Href,
                            FriendType = model.FriendType
                        }).ToList()
                    });
                }

                new SaveUnreadMessagesCommandHandler(new DataBaseContext()).Handle(new SaveUnreadMessagesCommand()
                {
                    AccountId      = account.Id,
                    UnreadMessages = unreadMessages.Select(model => new FacebookMessageDbModel
                    {
                        AccountId           = model.AccountId,
                        FriendFacebookId    = model.FriendFacebookId,
                        Gender              = model.Gender,
                        CountAllMessages    = model.CountAllMessages,
                        CountUnreadMessages = model.CountUnreadMessages,
                        Href                      = model.Href,
                        LastMessage               = model.LastMessage,
                        LastReadMessageDateTime   = model.LastReadMessageDateTime,
                        LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                        Name                      = model.Name,
                        UnreadMessage             = model.UnreadMessage,
                        FriendType                = model.FriendType
                    }).ToList()
                });

                ureadMessages = unreadMessages.Select(model => new UnreadFriendMessageModel
                {
                    FriendFacebookId          = model.FriendFacebookId,
                    UnreadMessage             = model.UnreadMessage,
                    LastMessage               = model.LastMessage,
                    CountAllMessages          = model.CountAllMessages,
                    CountUnreadMessages       = model.CountUnreadMessages,
                    LastReadMessageDateTime   = model.LastReadMessageDateTime,
                    LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                    FriendGender              = model.Gender,
                    FriendName = model.Name,
                    FriendHref = model.Href
                }).ToList();
            }
            catch (Exception ex)
            {
                _notice.AddNotice(functionName, account.Id, string.Format("Возникла ошибка {0}", ex.Message));
            }
            return(new UnreadFriendMessageList
            {
                UnreadMessages = ureadMessages
            });
        }