public void RemoveAccount(Account account)
 {
     IList<Account> accounts = GetAccounts();
     Account accountToRemove = accounts.Single(a => a.Name == account.Name);
     int accountIndex = accounts.IndexOf(accountToRemove);
     accounts.RemoveAt(accountIndex);
     SaveAccounts(accounts);
     _messenger.Send(new AccountSubscriptionUpdate { Accounts = accounts });
 }
        public async Task UpdateAccount(Account account)
        {
            if (account == null)
            {
                throw new ArgumentNullException(nameof(account));
            }

            IList<Account> existingAccounts = GetAccounts();
            
            AccountDetails accountDetails = new AccountDetails
            {
                AccountName = account.Name,
                EncodedCredentials = account.EncodedCredentials
            };

            // Get any updates to projects and builds
            account.Projects = await _vsoClient.GetProjects(accountDetails);

            foreach (Project vsoProject in account.Projects)
            {
                accountDetails.ProjectId = vsoProject.Id;
                vsoProject.Builds = await _vsoClient.GetBuildDefinitions(accountDetails);
                vsoProject.Builds.ToList().ForEach(b => b.IsSelected = false);
            }

            // Replace existing reference to account if there is one
            Account existingAccount = existingAccounts.SingleOrDefault(a => a.Name == account.Name);

            if (existingAccount == null)
            {
                existingAccounts.Add(account);
            }
            else
            {
                TransferSubscriptions(existingAccount, account); // todo - be careful that all data is transferred + new builds / projects included
                int indexOfExistingAccount = existingAccounts.IndexOf(existingAccount);
                existingAccounts[indexOfExistingAccount] = account;
            }

            SaveAccounts(existingAccounts);
        }
        private async void SaveAccount(PasswordBox passwordBox)
        {
            IsUpdateEnabled = false;

            var account = new Account
            {
                Name = VsoAccount,
                Username = Username,
                EncodedCredentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(Username + ":" + passwordBox.Password))
            };

            try
            {
                await _accountService.UpdateAccount(account);
                CloseDialog();
            }
            catch (Exception)
            {
                MessageBox.Show("Could not connect to your account with the credentials provided. Please try again.",
                    "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            IsUpdateEnabled = true;
        }
        private void RemoveAccount(Account account)
        {
            MessageBoxResult confirmation = MessageBox.Show(
                $"Are you sure you want to remove the account '{account.Name}'?",
                "Confirm Remove Account",
                MessageBoxButton.YesNo,
                MessageBoxImage.Question);

            if (confirmation == MessageBoxResult.Yes)
            {
                _accountService.RemoveAccount(account);
            }
        }
 private void RefreshAccount(Account account)
 {
     _accountService.UpdateAccount(account);
 }
        private void EditAccount(Account account)
        {
            ConfigureAccountWindow configureAccountWindow = new ConfigureAccountWindow
            {
                Top = Application.Current.MainWindow.Top + 100,
                Left = Application.Current.MainWindow.Left + 100
            };

            configureAccountWindow.ShowDialog();
        }
        private void TransferSubscriptions(Account oldAccount, Account newAccount)
        {
            newAccount.IsSelected = oldAccount.IsSelected;

            foreach (Project newProject in newAccount.Projects)
            {
                Project oldProject = oldAccount.Projects.SingleOrDefault(p => p.Id == newProject.Id);

                if (oldProject != null)
                {
                    newProject.IsSelected = oldProject.IsSelected;

                    foreach (BuildDefinition newBuildDefinition in newProject.Builds)
                    {
                        BuildDefinition oldBuildDefinition =
                            oldProject.Builds.SingleOrDefault(b => b.Id == newBuildDefinition.Id);

                        if (oldBuildDefinition != null)
                        {
                            newBuildDefinition.IsSelected = oldBuildDefinition.IsSelected;
                        }
                    }
                }
            }
        }