private List <DocumentItem> FilterOrderDocuments(FilterDocumentsVm filterItems, List <DocumentItem> documents)
        {
            if (documents != null)
            {
                if (filterItems.DateFrom != null)
                {
                    documents = documents.Where(x => x.Received.Value.Date >= filterItems.DateFrom).ToList();
                }

                if (filterItems.DateTo != null)
                {
                    documents = documents.Where(x => x.Received.Value.Date <= filterItems.DateTo).ToList();
                }

                if (!string.IsNullOrEmpty(filterItems.KeyWord))
                {
                    documents = documents.Where(x => x.Subject.ToLower().Contains(filterItems.KeyWord.ToLower()))
                                .ToList();
                }

                if (filterItems.OrderByReceived != null)
                {
                    switch (filterItems.OrderByReceived)
                    {
                    case "ascending": documents = documents.OrderBy(x => x.Received).ToList(); break;

                    case "descending": documents = documents.OrderByDescending(x => x.Received).ToList(); break;
                    }

                    filterItems.OrderByReceived = filterItems.OrderByReceived == "ascending" ? "descending" : "ascending";
                }

                if (filterItems.OrderByRead != null)
                {
                    switch (filterItems.OrderByRead)
                    {
                    case "ascending": documents = documents.OrderBy(x => x.Read).ToList(); break;

                    case "descending": documents = documents.OrderByDescending(x => x.Read).ToList(); break;
                    }

                    filterItems.OrderByRead = filterItems.OrderByRead == "ascending" ? "descending" : "ascending";
                }

                if (filterItems.OrderBySubject != null)
                {
                    switch (filterItems.OrderBySubject)
                    {
                    case "ascending": documents = documents.OrderBy(x => x.Subject).ToList(); break;

                    case "descending": documents = documents.OrderByDescending(x => x.Subject).ToList(); break;
                    }

                    filterItems.OrderBySubject = filterItems.OrderBySubject == "ascending" ? "descending" : "ascending";
                }

                if (!documents.Any())
                {
                    filterItems.Message = "No Results Found";
                }

                return(documents);
            }

            return(new List <DocumentItem>());
        }
        public async Task <IActionResult> FilterItems(FilterDocumentsVm filterItems,
                                                      string accountReference,
                                                      string orderByReceived,
                                                      string orderByRead,
                                                      string orderBySubject,
                                                      DateTime?dateFrom,
                                                      DateTime?dateTo,
                                                      int pageNumber = 1)
        {
            // 1. Get all accounts for this user and Cache it.
            var allAccounts = await GetAccounts();

            // 2. Get lowell reference
            var lowellReference = GetLowellReferenceFromSessionState(accountReference);

            // 3. Find an account in random to assume to be selected.
            var selectedAccount = allAccounts.Where(x => x.Reference == lowellReference).FirstOrDefault();
            var viewModel       = _mapper.Map <MyDocumentsVm>(selectedAccount);

            // 4. Get all available documents for Selected Account
            var documents = await GetDocuments(lowellReference);

            var pagedList = _mapper.Map <List <DocumentVm> >(documents)?.ToPagedList(pageNumber, 10);

            viewModel = _mapper.Map(pagedList, viewModel);

            //5. Map linked accounts
            var linkedAccounts = MapLinkedAccounts(allAccounts, lowellReference);

            viewModel = _mapper.Map(linkedAccounts, viewModel);
            viewModel = _mapper.Map(filterItems, viewModel);
            viewModel.AccountsHaveUnreadDocuments = allAccounts.Any(x => x.UnreadDocuments);

            //5. Check model state
            if (!ModelState.IsValid)
            {
                return(View("Index", viewModel));
            }

            //6. Map order by vm to filter items
            var mappedFilter = _mapper.Map <FilterDocumentsVm>(new OrderByVm
            {
                DateFrom        = dateFrom,
                DateTo          = dateTo,
                OrderByReceived = orderByReceived,
                OrderByRead     = orderByRead,
                OrderBySubject  = orderBySubject
            });

            //6. Merge both order by vm and filter items
            filterItems = _mapper.Map(mappedFilter, filterItems);

            //7. Filter documents
            documents = FilterOrderDocuments(filterItems, documents);

            pagedList = _mapper.Map <List <DocumentVm> >(documents)?.ToPagedList(pageNumber, 10);
            viewModel = _mapper.Map(pagedList, viewModel);
            viewModel = _mapper.Map(filterItems, viewModel);

            return(View("Index", viewModel));
        }