/// <summary>
        /// Stores the DL invalid members info in a local file.
        /// </summary>
        /// <param name="distributionList">Existing distribution list model.</param>
        /// <param name="actionName">Name of the flow attempting to write a failure list</param>
        /// <param name="errorDetails">Add Members details to be written</param>
        /// <returns></returns>
        internal string StoreDistributionListFailures(DistributionList distributionList, string actionName, AddMembersErrorDetails errorDetails)
        {
            string path;

            try
            {
                var directory = Directory.CreateDirectory(AppPath);

                var serializer = new XmlSerializer(typeof(AddMembersErrorDetails));
                path = Path.Combine(directory.FullName, distributionList.Name + "_" + actionName + "_Failures.xmldl");

                using (var writer = new StreamWriter(path))
                {
                    serializer.Serialize(writer, errorDetails);
                }

                LoggingViewModel.Instance.Logger.Write(string.Concat("StoreDistributionListInvalidMembers:OK ", path,
                    Environment.NewLine,
                    distributionList.Name));
            }
            catch (Exception exception)
            {
                LoggingViewModel.Instance.Logger.Write(string.Concat("StoreDistributionListInvalidMembers:Error ",
                    exception.Message, Environment.NewLine,
                    exception.StackTrace,
                    Environment.NewLine,
                    distributionList.Name));

                path = string.Empty;
            }

            return path;
        }
        public DistributionList SimplifyGroup(Member[] members, string groupName)
        {
            var list = new DistributionList {Name = groupName, Owner = "INVALID", Members = new ObservableCollection<string>()};

            foreach (var member in members)
            {
                list.Members.Add(member.Persona.EmailAddress.EmailAddress);
            }

            return list;
        }
        private async void btnCreateBackup_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(TxtDlAlias.Text))
            {
                var credManager = new CredentialManager();
                var userCredentials = credManager.GetUserCredentials();

                if (userCredentials != null)
                {
                    // Disabling pieces of UI that might interfere.
                    DlGroupMigrationViewModel.Instance.BackupControlsEnabled = false;
                    DlGroupMigrationViewModel.Instance.RestoreControlsEnabled = false;

                    TxtBackupStatus.Text = "Identifying alias...";

                    var adConnector = new ActiveDirectoryConnector();
                    var dl = new DistributionList {Name = TxtDlAlias.Text};
                    var owner = string.Empty;
                    bool isValidDl;
                    if (!AccountSettingsViewModel.Instance.IsInternal)
                    {
                        // The user is external.
                        var connector = new ExchangeConnector();
                        owner = connector.GetExternalDistributionListOwner(TxtDlAlias.Text, userCredentials, out isValidDl);
                    }
                    else
                    {
                        // The user is internal
                        var dlDetails = await adConnector.GetDistributionListOwner(TxtDlAlias.Text);
                        owner = dlDetails.Item1;
                        isValidDl = dlDetails.Item2;
                    }

                    bool resolveMembers = !string.IsNullOrWhiteSpace(owner);

                    if (!isValidDl)
                    {
                        var result =
                            ModernDialog.ShowMessage(
                                "The alias you provided doesn't map to a valid DL.",
                                "Hummingbird", MessageBoxButton.OK);
                        resolveMembers = false;
                    }
                    else if (!resolveMembers)
                    {
                        var result =
                            ModernDialog.ShowMessage(
                                "The group owner couldn't be found. If you attempt to retrieve members for the group, the backup will be missing the Owner property. Continue?",
                                "Hummingbird", MessageBoxButton.YesNo);
                        resolveMembers = result == MessageBoxResult.Yes;
                    }

                    if (resolveMembers)
                    {
                        dl.Owner = owner;

                        TxtBackupStatus.Text = "getting members...";

                        var members = await adConnector.GetDistributionListMembers(TxtDlAlias.Text);
                        if (members != null)
                        {
                            dl.Members = members;

                            var fsOperator = new FileSystemOperator();
                            var filePath = fsOperator.StoreDistributionListInformation(dl);

                            if (!string.IsNullOrWhiteSpace(filePath))
                            {
                                var result =
                                    ModernDialog.ShowMessage(
                                        "A backup was created for the distribution list. Do you want to open File Explorer to find its location?",
                                        "Hummingbird", MessageBoxButton.YesNo);

                                if (result == MessageBoxResult.Yes)
                                {
                                    Process.Start("explorer.exe", string.Concat("/select, ", filePath));
                                }
                            }
                            else
                            {
                                ModernDialog.ShowMessage(
                                    "We couldn't create a backup file for this distribution list. Please try again.", "Hummingbird",
                                    MessageBoxButton.OK);
                            }
                        }
                        else
                        {
                            ModernDialog.ShowMessage(
                                "We couldn't retrieve members of the distribution list. Check your credentials and try again.", "Hummingbird",
                                MessageBoxButton.OK);
                        }
                    }

                    // Re-enable the pieces of UI that might interfere.
                    DlGroupMigrationViewModel.Instance.BackupControlsEnabled = true;
                    DlGroupMigrationViewModel.Instance.RestoreControlsEnabled = true;
                }
                else
                {
                    ModernDialog.ShowMessage(
                        "No credentials were provided. Open Settings and add your credentials.", "Hummingbird",
                        MessageBoxButton.OK);
                }
            }
            else
            {
                ModernDialog.ShowMessage("You must include an alias.", "Hummingbird", MessageBoxButton.OK);
            }
        }
        /// <summary>
        /// Stores the DL information in a local file.
        /// </summary>
        /// <param name="distributionList">Existing distribution list model.</param>
        /// <returns></returns>
        internal string StoreDistributionListInformation(DistributionList distributionList)
        {
            string path;

            try
            {
                var directory = Directory.CreateDirectory(AppPath);

                var serializer = new XmlSerializer(typeof (DistributionList));
                path = Path.Combine(directory.FullName, distributionList.Name + ".xmldl");

                using (var writer = new StreamWriter(path))
                {
                    serializer.Serialize(writer, distributionList);
                }

                LoggingViewModel.Instance.Logger.Write(string.Concat("StoreDistributionListInformation:OK ", path,
                    Environment.NewLine,
                    distributionList.Name));
            }
            catch (Exception exception)
            {
                LoggingViewModel.Instance.Logger.Write(string.Concat("StoreDistributionListInformation:Error ",
                    exception.Message, Environment.NewLine,
                    exception.StackTrace, Environment.NewLine, string.Join(",", distributionList.Members.ToArray()),
                    Environment.NewLine,
                    distributionList.Name));

                path = string.Empty;
            }

            return path;
        }