public virtual async Task<IEnumerable<TemporaryContactInformation>> GetContacts(string targetUser, bool allResults, long? displayOnlyContactId, SystemSession session)
        {
            var result =
                await
                    Task.Factory.StartNew(
                        () => Client.UserService.getUserContactDetails(session.UserId, targetUser, session.GetSession()))
                        .ConfigureAwait(false);

            if (!allResults)
            {
                result =
                    result.Where(
                        x =>
                            x.ContactTypeId < 3 || (x.ContactTypeId >= 3 && x.ContactId == result.Max(y => y.ContactId)))
                        .ToList();
            }
            else
            {
                if (displayOnlyContactId.HasValue)
                    result = result.Where(x => x.ContactId == displayOnlyContactId.Value).ToList();
            }

            var response = result.Select(x => new TemporaryContactInformation
            {
                ContactId = x.ContactId,
                UserId = x.UserId,
                ContactTypeId = (SystemContactType)x.ContactTypeId,
                Address = new ContactField<string> { Value = x.Address, Visibility = x.AddressVisible },
                Email = new ContactField<string> { Value = x.Email, Visibility = x.EmailVisible },
                Fax = new ContactField<string> { Value = x.Fax, Visibility = x.FaxVisible },
                Mobile = new ContactField<string> { Value = x.Mobile, Visibility = x.MobileVisible },
                Phone = new ContactField<string> { Value = x.Phone, Visibility = x.PhoneVisible },
                Website = new ContactField<string> { Value = x.Website, Visibility = x.WebsiteVisible },
                CompanyUserId =
                    new ContactFieldBasic<int?>
                    {
                        Value = x.CompanyUserId == 0 ? (int?)null : x.CompanyUserId,
                        Visibility = x.CompanyUserIdVisible
                    },
                StartDate = Convert.ToDateTime(x.StartDate),
                EndDate = Convert.ToDateTime(x.EndDate),
                SuggestionCounts = x.SuggestionCount
            }).ToList();

            foreach (var contactResponse in result)
            {
                var resp = response.First(x => x.ContactId == contactResponse.ContactId);
                if (!string.IsNullOrEmpty(contactResponse.StrFixedFieldSuggestions))
                {

                    var str = contactResponse.StrFixedFieldSuggestions.Split(new[] { "|" },
                        StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in str)
                    {
                        var components = item.Split(new[] { "," }, StringSplitOptions.None);
                        var id = (SystemContactMarkedType)Convert.ToByte(components[0]);
                        var value = string.Join(",",
                            Enumerable.Range(1, components.Count() - 3).Select(x => components[x]));
                        var cid = Convert.ToInt64(components[components.Count() - 2]);
                        var approved = Convert.ToByte(components.Last());
                        switch (id)
                        {
                            case SystemContactMarkedType.Address:
                                resp.Address.Suggestion = new FieldSuggestion<string>
                                {
                                    Value = value,
                                    Approved = (SystemApprovalStatus)approved,
                                    ContactCustomId = cid,
                                    FieldType = id
                                };
                                break;
                            case SystemContactMarkedType.Email:
                                resp.Email.Suggestion = new FieldSuggestion<string>
                                {
                                    Value = value,
                                    Approved = (SystemApprovalStatus)approved,
                                    ContactCustomId = cid,
                                    FieldType = id
                                };
                                break;
                            case SystemContactMarkedType.Fax:
                                resp.Fax.Suggestion = new FieldSuggestion<string>
                                {
                                    Value = value,
                                    Approved = (SystemApprovalStatus)approved,
                                    ContactCustomId = cid,
                                    FieldType = id
                                };
                                break;
                            case SystemContactMarkedType.Mobile:
                                resp.Mobile.Suggestion = new FieldSuggestion<string>
                                {
                                    Value = value,
                                    Approved = (SystemApprovalStatus)approved,
                                    ContactCustomId = cid,
                                    FieldType = id
                                };
                                break;
                            case SystemContactMarkedType.Phone:
                                resp.Phone.Suggestion = new FieldSuggestion<string>
                                {
                                    Value = value,
                                    Approved = (SystemApprovalStatus)approved,
                                    ContactCustomId = cid,
                                    FieldType = id
                                };
                                break;
                            case SystemContactMarkedType.Website:
                                resp.Website.Suggestion = new FieldSuggestion<string>
                                {
                                    Value = value,
                                    Approved = (SystemApprovalStatus)approved,
                                    ContactCustomId = cid,
                                    FieldType = id
                                };
                                break;
                        }
                    }
                }

                if (resp.ChatNetworks == null) resp.ChatNetworks = new List<ChatNetworkField>();
                if (!string.IsNullOrEmpty(contactResponse.StrChatNetWorks))
                {
                    var str = contactResponse.StrChatNetWorks.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in str)
                    {
                        var components = item.Split(new[] { "," }, StringSplitOptions.None);
                        var id = (SystemContactMarkedType)Convert.ToByte(components[0]);
                        var value = string.Join(",",
                            Enumerable.Range(1, components.Count() - 2).Select(x => components[x]));
                        var cid = Convert.ToInt32(components.Last());
                        resp.ChatNetworks.Add(new ChatNetworkField
                        {
                            Original =
                                new ContactChatNetwork { ChatNetworkId = id, Value = value, ContactChatNetworkId = cid }
                        });
                    }
                }

                if (!string.IsNullOrEmpty(contactResponse.StrContactCustoms))
                {
                    resp.CustomContacts = new List<CustomContactField>();
                    var str = contactResponse.StrContactCustoms.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in str)
                    {
                        var components = item.Split(new[] { "," }, StringSplitOptions.None);
                        var fid = string.IsNullOrEmpty(components[0])
                            ? SystemContactMarkedType.Custom
                            : (SystemContactMarkedType)Convert.ToInt32(components[0]);
                        var name = components[1];
                        var value = string.Join(",",
                            Enumerable.Range(2, components.Count() - 3).Select(x => components[x]));
                        var ccid = Convert.ToInt64(components.Last());
                        resp.CustomContacts.Add(new CustomContactField
                        {
                            Original =
                                new ContactCustom { FieldType = fid, Name = name, Value = value, ContactCustomId = ccid }
                        });
                    }
                }

                if (!string.IsNullOrEmpty(contactResponse.StrChatNetworkSuggestions))
                {
                    var str = contactResponse.StrChatNetworkSuggestions.Split(new[] { "|" },
                        StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in str)
                    {
                        var components = item.Split(new[] { "," }, StringSplitOptions.None);
                        var fid = (SystemContactMarkedType)Convert.ToByte(components[0]);
                        var value = string.Join(",",
                            Enumerable.Range(1, components.Count() - 4).Select(x => components[x]));
                        var contactCnid = Convert.ToInt32(components[components.Count() - 3]);
                        var cid = Convert.ToInt64(components[components.Count() - 2]);
                        var approved = Convert.ToByte(components.Last());

                        var data =
                            resp.ChatNetworks.FirstOrDefault(
                                x => x.Original != null && x.Original.ContactChatNetworkId == contactCnid);
                        if (data == null)
                        {
                            data = new ChatNetworkField();
                            resp.ChatNetworks.Add(data);
                        }
                        data.Suggestion = new ChatNetworkFieldSuggestion
                        {
                            FieldType = fid,
                            Value = value,
                            ContactChatNetworkId = contactCnid,
                            ContactCustomId = cid,
                            Approved = (SystemApprovalStatus)approved
                        };
                    }
                }

                if (resp.CustomContacts == null) resp.CustomContacts = new List<CustomContactField>();
                if (!string.IsNullOrEmpty(contactResponse.StrContactCustomSuggestions))
                {
                    var str = contactResponse.StrContactCustomSuggestions.Split(new[] { "|" },
                        StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in str)
                    {
                        var components = item.Split(new[] { "," }, StringSplitOptions.None);
                        var name = string.IsNullOrWhiteSpace(components[0]) ? null : components[0];
                        var value = string.Join(",",
                            Enumerable.Range(1, components.Count() - 5).Select(x => components[x]));
                        var ccid = Convert.ToInt64(components[components.Count() - 4]);
                        var refccid = string.IsNullOrWhiteSpace(components[components.Count() - 3])
                            ? 0
                            : Convert.ToInt64(components[components.Count() - 3]);
                        var fstr = components[components.Count() - 2];
                        byte fint;
                        var fid = SystemContactMarkedType.Custom;
                        if (!string.IsNullOrWhiteSpace(fstr) && byte.TryParse(fstr, out fint))
                            fid = (SystemContactMarkedType)fint;
                        var approved = Convert.ToByte(components.Last());

                        var data =
                            resp.CustomContacts.FirstOrDefault(
                                x => x.Original != null && x.Original.ContactCustomId == refccid);
                        if (data == null)
                        {
                            data = new CustomContactField();
                            resp.CustomContacts.Add(data);
                        }
                        data.Suggestion = new CustomContactFieldSuggestion
                        {
                            Name = name,
                            Value = value,
                            ContactCustomId = ccid,
                            ReferralContactCustomId = refccid == 0 ? (long?)null : refccid,
                            FieldType = fid,
                            Approved = (SystemApprovalStatus)approved
                        };
                    }
                }
                var defaultFields =
                    resp.CustomContacts.Where(
                        x =>
                            (x.Original != null && x.Original.FieldType.IsDefault()) ||
                            (x.Suggestion != null && x.Suggestion.FieldType.IsDefault()));
                foreach (var customContactField in defaultFields)
                {
                    var takeField = customContactField.Original != null
                        ? customContactField.Original.FieldType
                        : customContactField.Suggestion.FieldType;
                    switch (takeField)
                    {
                        case SystemContactMarkedType.Address:
                            resp.Address.CustomContacts.Add(customContactField);
                            break;
                        case SystemContactMarkedType.Email:
                            resp.Email.CustomContacts.Add(customContactField);
                            break;
                        case SystemContactMarkedType.Fax:
                            resp.Fax.CustomContacts.Add(customContactField);
                            break;
                        case SystemContactMarkedType.Mobile:
                            resp.Mobile.CustomContacts.Add(customContactField);
                            break;
                        case SystemContactMarkedType.Phone:
                            resp.Phone.CustomContacts.Add(customContactField);
                            break;
                        case SystemContactMarkedType.Website:
                            resp.Website.CustomContacts.Add(customContactField);
                            break;
                    }
                }
                resp.CustomContacts.RemoveAll(
                    x =>
                        (x.Original != null && x.Original.FieldType.IsDefault()) ||
                        (x.Suggestion != null && x.Suggestion.FieldType.IsDefault()));
            }

            return response;
        }
        public async Task <List <TemporaryContactInformation> > GetProfilesContacts(int userId, IEnumerable <int> targetUsers)
        {
            var users       = string.Join(",", targetUsers);
            var contactInfo = await Task.Factory.StartNew(() => Context.FNCONTACTSDETAILS(userId, users).ToList()).ConfigureAwait(false);


            var response = contactInfo.Select(x => new TemporaryContactInformation
            {
                ContactId     = x.ContactID,
                UserId        = x.UserID,
                ContactTypeId = (SystemContactType)x.ContactTypeID,
                Address       = new ContactField <string> {
                    Value = x.Address, Visibility = x.AddressVisible
                },
                Email = new ContactField <string> {
                    Value = x.Email, Visibility = x.EmailVisible
                },
                Fax = new ContactField <string> {
                    Value = x.Fax, Visibility = x.FaxVisible
                },
                Mobile = new ContactField <string> {
                    Value = x.Mobile, Visibility = x.MobileVisible
                },
                Phone = new ContactField <string> {
                    Value = x.Phone, Visibility = x.PhoneVisible
                },
                Website = new ContactField <string> {
                    Value = x.Website, Visibility = x.WebsiteVisible
                },
                CompanyUserId = new ContactFieldBasic <int?> {
                    Value = x.CompanyUserID == 0 ? (int?)null : x.CompanyUserID, Visibility = x.CompanyUserIDVisible
                },
                StartDate = Convert.ToDateTime(x.StartDate),
                EndDate   = Convert.ToDateTime(x.EndDate)
            }).ToList();

            foreach (var contactResponse in contactInfo)
            {
                var resp = response.First(x => x.ContactId == contactResponse.ContactID);
                if (!string.IsNullOrEmpty(contactResponse.StrFixedFieldSuggestions))
                {
                    var str = contactResponse.StrFixedFieldSuggestions.Split(new[] { "|" },
                                                                             StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in str)
                    {
                        var components = item.Split(new[] { "," }, StringSplitOptions.None);
                        var id         = (SystemContactMarkedType)Convert.ToByte(components[0]);
                        var value      = string.Join(",",
                                                     Enumerable.Range(1, components.Count() - 3).Select(x => components[x]));
                        var cid      = Convert.ToInt64(components[components.Count() - 2]);
                        var approved = Convert.ToByte(components.Last());
                        switch (id)
                        {
                        case SystemContactMarkedType.Address:
                            resp.Address.Suggestion = new FieldSuggestion <string> {
                                Value = value, Approved = (SystemApprovalStatus)approved, ContactCustomId = cid, FieldType = id
                            };
                            break;

                        case SystemContactMarkedType.Email:
                            resp.Email.Suggestion = new FieldSuggestion <string> {
                                Value = value, Approved = (SystemApprovalStatus)approved, ContactCustomId = cid, FieldType = id
                            };
                            break;

                        case SystemContactMarkedType.Fax:
                            resp.Fax.Suggestion = new FieldSuggestion <string> {
                                Value = value, Approved = (SystemApprovalStatus)approved, ContactCustomId = cid, FieldType = id
                            };
                            break;

                        case SystemContactMarkedType.Mobile:
                            resp.Mobile.Suggestion = new FieldSuggestion <string> {
                                Value = value, Approved = (SystemApprovalStatus)approved, ContactCustomId = cid, FieldType = id
                            };
                            break;

                        case SystemContactMarkedType.Phone:
                            resp.Phone.Suggestion = new FieldSuggestion <string> {
                                Value = value, Approved = (SystemApprovalStatus)approved, ContactCustomId = cid, FieldType = id
                            };
                            break;

                        case SystemContactMarkedType.Website:
                            resp.Website.Suggestion = new FieldSuggestion <string> {
                                Value = value, Approved = (SystemApprovalStatus)approved, ContactCustomId = cid, FieldType = id
                            };
                            break;
                        }
                    }
                }

                if (resp.ChatNetworks == null)
                {
                    resp.ChatNetworks = new List <ChatNetworkField>();
                }
                if (!string.IsNullOrEmpty(contactResponse.StrChatNetworks))
                {
                    var str = contactResponse.StrChatNetworks.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in str)
                    {
                        var components = item.Split(new[] { "," }, StringSplitOptions.None);
                        var id         = (SystemContactMarkedType)Convert.ToByte(components[0]);
                        var value      = string.Join(",", Enumerable.Range(1, components.Count() - 2).Select(x => components[x]));
                        var cid        = Convert.ToInt32(components.Last());
                        resp.ChatNetworks.Add(new ChatNetworkField
                        {
                            Original = new ContactChatNetwork {
                                ChatNetworkId = id, Value = value, ContactChatNetworkId = cid
                            }
                        });
                    }
                }

                if (!string.IsNullOrEmpty(contactResponse.StrContactCustoms))
                {
                    resp.CustomContacts = new List <CustomContactField>();
                    var str = contactResponse.StrContactCustoms.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in str)
                    {
                        var components = item.Split(new[] { "," }, StringSplitOptions.None);
                        var fid        = string.IsNullOrEmpty(components[0]) ? SystemContactMarkedType.Custom : (SystemContactMarkedType)Convert.ToInt32(components[0]);
                        var name       = components[1];
                        var value      = string.Join(",", Enumerable.Range(2, components.Count() - 3).Select(x => components[x]));
                        var ccid       = Convert.ToInt64(components.Last());
                        resp.CustomContacts.Add(new CustomContactField
                        {
                            Original = new ContactCustom {
                                FieldType = fid, Name = name, Value = value, ContactCustomId = ccid
                            }
                        });
                    }
                }

                if (!string.IsNullOrEmpty(contactResponse.StrChatNetworkSuggestions))
                {
                    var str = contactResponse.StrChatNetworkSuggestions.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in str)
                    {
                        var components  = item.Split(new[] { "," }, StringSplitOptions.None);
                        var fid         = (SystemContactMarkedType)Convert.ToByte(components[0]);
                        var value       = string.Join(",", Enumerable.Range(1, components.Count() - 4).Select(x => components[x]));
                        var contactCnid = Convert.ToInt32(components[components.Count() - 3]);
                        var cid         = Convert.ToInt64(components[components.Count() - 2]);
                        var approved    = Convert.ToByte(components.Last());

                        var data = resp.ChatNetworks.FirstOrDefault(x => x.Original != null && x.Original.ContactChatNetworkId == contactCnid);
                        if (data == null)
                        {
                            data = new ChatNetworkField();
                            resp.ChatNetworks.Add(data);
                        }
                        data.Suggestion = new ChatNetworkFieldSuggestion {
                            FieldType = fid, Value = value, ContactChatNetworkId = contactCnid, ContactCustomId = cid, Approved = (SystemApprovalStatus)approved
                        };
                    }
                }

                if (resp.CustomContacts == null)
                {
                    resp.CustomContacts = new List <CustomContactField>();
                }
                if (!string.IsNullOrEmpty(contactResponse.StrContactCustomSuggestions))
                {
                    var str = contactResponse.StrContactCustomSuggestions.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in str)
                    {
                        var  components = item.Split(new[] { "," }, StringSplitOptions.None);
                        var  name       = string.IsNullOrWhiteSpace(components[0]) ? null : components[0];
                        var  value      = string.Join(",", Enumerable.Range(1, components.Count() - 5).Select(x => components[x]));
                        var  ccid       = Convert.ToInt64(components[components.Count() - 4]);
                        var  refccid    = string.IsNullOrWhiteSpace(components[components.Count() - 3]) ? 0 : Convert.ToInt64(components[components.Count() - 3]);
                        var  fstr       = components[components.Count() - 2];
                        byte fint;
                        var  fid = SystemContactMarkedType.Custom;
                        if (!string.IsNullOrWhiteSpace(fstr) && byte.TryParse(fstr, out fint))
                        {
                            fid = (SystemContactMarkedType)fint;
                        }
                        var approved = Convert.ToByte(components.Last());

                        var data = resp.CustomContacts.FirstOrDefault(x => x.Original != null && x.Original.ContactCustomId == refccid);
                        if (data == null)
                        {
                            data = new CustomContactField();
                            resp.CustomContacts.Add(data);
                        }
                        data.Suggestion = new CustomContactFieldSuggestion {
                            Name = name, Value = value, ContactCustomId = ccid, ReferralContactCustomId = refccid == 0 ? (long?)null : refccid, FieldType = fid, Approved = (SystemApprovalStatus)approved
                        };
                    }
                }
                var defaultFields = resp.CustomContacts.Where(x => (x.Original != null && x.Original.FieldType.IsDefault()) || (x.Suggestion != null && x.Suggestion.FieldType.IsDefault()));
                foreach (var customContactField in defaultFields)
                {
                    var takeField = customContactField.Original != null ? customContactField.Original.FieldType : customContactField.Suggestion.FieldType;
                    switch (takeField)
                    {
                    case SystemContactMarkedType.Address:
                        resp.Address.CustomContacts.Add(customContactField);
                        break;

                    case SystemContactMarkedType.Email:
                        resp.Email.CustomContacts.Add(customContactField);
                        break;

                    case SystemContactMarkedType.Fax:
                        resp.Fax.CustomContacts.Add(customContactField);
                        break;

                    case SystemContactMarkedType.Mobile:
                        resp.Mobile.CustomContacts.Add(customContactField);
                        break;

                    case SystemContactMarkedType.Phone:
                        resp.Phone.CustomContacts.Add(customContactField);
                        break;

                    case SystemContactMarkedType.Website:
                        resp.Website.CustomContacts.Add(customContactField);
                        break;
                    }
                }
                resp.CustomContacts.RemoveAll(x => (x.Original != null && x.Original.FieldType.IsDefault()) || (x.Suggestion != null && x.Suggestion.FieldType.IsDefault()));
            }

            return(response);
        }