<commit_before>﻿// Original Work Copyright (c) <NAME> 2014-2016
// This file is subject to the GPL v2 License
// For additional details, see the LICENSE file

using System;
using System.Threading.Tasks;
using EndlessClient.Dialogs.Actions;
using EndlessClient.GameExecution;
using EOLib.Domain.Account;
using EOLib.Net;
using EOLib.Net.Communication;

namespace EndlessClient.Controllers
{
	public class AccountController : IAccountController
	{
		private readonly IAccountDialogDisplayActions _accountDialogDisplayActions;
		private readonly IErrorDialogDisplayAction _errorDisplayAction;
		private readonly IAccountActions _accountActions;
		private readonly IGameStateActions _gameStateActions;
		private readonly ISafeInBandNetworkOperationFactory _networkOperationFactory;

		public AccountController(IAccountDialogDisplayActions accountDialogDisplayActions,
								 IErrorDialogDisplayAction errorDisplayAction,
								 IAccountActions accountActions,
								 IGameStateActions gameStateActions,
								 ISafeInBandNetworkOperationFactory networkOperationFactory)
		{
			_accountDialogDisplayActions = accountDialogDisplayActions;
			_errorDisplayAction = errorDisplayAction;
			_accountActions = accountActions;
			_gameStateActions = gameStateActions;
			_networkOperationFactory = networkOperationFactory;
		}

		public async Task CreateAccount(ICreateAccountParameters createAccountParameters)
		{
			var paramsValidationResult = _accountActions.CheckAccountCreateParameters(createAccountParameters);
			if (paramsValidationResult.FaultingParameter != WhichParameter.None)
			{
				_accountDialogDisplayActions.ShowCreateParameterValidationError(paramsValidationResult);
				return;
			}

			var checkNameOperation = _networkOperationFactory.CreateSafeOperation(
				async () => await _accountActions.CheckAccountNameWithServer(createAccountParameters.AccountName),
				SetInitialStateAndShowError,
				SetInitialStateAndShowError);
			if (!await checkNameOperation.Invoke())
				return;

			var nameResult = checkNameOperation.Result;
			if (nameResult != AccountReply.Continue)
			{
				_accountDialogDisplayActions.ShowCreateAccountServerError(nameResult);
				return;
			}

			if (!await ShowAccountCreationPendingDialog()) return;

			var createAccountOperation = _networkOperationFactory.CreateSafeOperation(
				async () => await _accountActions.CreateAccount(createAccountParameters),
				SetInitialStateAndShowError,
				SetInitialStateAndShowError);
			if (!await createAccountOperation.Invoke())
				return;

			var accountResult = createAccountOperation.Result;
			if (accountResult != AccountReply.Created)
			{
				_accountDialogDisplayActions.ShowCreateAccountServerError(accountResult);
				return;
			}

			_gameStateActions.ChangeToState(GameStates.Initial);
			_accountDialogDisplayActions.ShowSuccessMessage();
		}

		public async Task ChangePassword()
		{
			IChangePasswordParameters changePasswordParameters;
			try
			{
				changePasswordParameters = await _accountDialogDisplayActions.ShowChangePasswordDialog();
			}
			catch (OperationCanceledException) { return; }

			//todo: change the password server-side
		}

		private async Task<bool> ShowAccountCreationPendingDialog()
		{
			try
			{
				await _accountDialogDisplayActions.ShowCreatePendingDialog();
			}
			catch (OperationCanceledException) { return false; }

			return true;
		}

		private void SetInitialStateAndShowError(NoDataSentException ex)<commit_msg>Do password change operations in AccountController<commit_after>﻿// Original Work Copyright (c) <NAME> 2014-2016
// This file is subject to the GPL v2 License
// For additional details, see the LICENSE file

using System;
using System.Threading.Tasks;
using EndlessClient.Dialogs.Actions;
using EndlessClient.GameExecution;
using EOLib.Domain.Account;
using EOLib.Net;
using EOLib.Net.Communication;

namespace EndlessClient.Controllers
{
	public class AccountController : IAccountController
	{
		private readonly IAccountDialogDisplayActions _accountDialogDisplayActions;
		private readonly IErrorDialogDisplayAction _errorDisplayAction;
		private readonly IAccountActions _accountActions;
		private readonly IGameStateActions _gameStateActions;
		private readonly ISafeInBandNetworkOperationFactory _networkOperationFactory;

		public AccountController(IAccountDialogDisplayActions accountDialogDisplayActions,
								 IErrorDialogDisplayAction errorDisplayAction,
								 IAccountActions accountActions,
								 IGameStateActions gameStateActions,
								 ISafeInBandNetworkOperationFactory networkOperationFactory)
		{
			_accountDialogDisplayActions = accountDialogDisplayActions;
			_errorDisplayAction = errorDisplayAction;
			_accountActions = accountActions;
			_gameStateActions = gameStateActions;
			_networkOperationFactory = networkOperationFactory;
		}

		public async Task CreateAccount(ICreateAccountParameters createAccountParameters)
		{
			var paramsValidationResult = _accountActions.CheckAccountCreateParameters(createAccountParameters);
			if (paramsValidationResult.FaultingParameter != WhichParameter.None)
			{
				_accountDialogDisplayActions.ShowCreateParameterValidationError(paramsValidationResult);
				return;
			}

			var checkNameOperation = _networkOperationFactory.CreateSafeOperation(
				async () => await _accountActions.CheckAccountNameWithServer(createAccountParameters.AccountName),
				SetInitialStateAndShowError,
				SetInitialStateAndShowError);
			if (!await checkNameOperation.Invoke())
				return;

			var nameResult = checkNameOperation.Result;
			if (nameResult != AccountReply.Continue)
			{
				_accountDialogDisplayActions.ShowCreateAccountServerError(nameResult);
				return;
			}

			if (!await ShowAccountCreationPendingDialog()) return;

			var createAccountOperation = _networkOperationFactory.CreateSafeOperation(
				async () => await _accountActions.CreateAccount(createAccountParameters),
				SetInitialStateAndShowError,
				SetInitialStateAndShowError);
			if (!await createAccountOperation.Invoke())
				return;

			var accountResult = createAccountOperation.Result;
			if (accountResult != AccountReply.Created)
			{
				_accountDialogDisplayActions.ShowCreateAccountServerError(accountResult);
				return;
			}

			_gameStateActions.ChangeToState(GameStates.Initial);
			_accountDialogDisplayActions.ShowSuccessMessage();
		}

		public async Task ChangePassword()
		{
			IChangePasswordParameters changePasswordParameters;
			try
			{
				changePasswordParameters = await _accountDialogDisplayActions.ShowChangePasswordDialog();
			}
			catch (OperationCanceledException) { return; }

			var changePasswordOperation = _networkOperationFactory.CreateSafeOperation(
				async () => await _accountActions.ChangePassword(changePasswordParameters),
				SetInitialStateAndShowError,
				SetInitialStateAndShowError);
			if (!await changePasswordOperation.Invoke())
				return;

			var result = changePasswordOperation.Result;
			_accountDialogDisplayActions.ShowCreateAccountServerError(result);
		}

		private async Task<bool> ShowAccountCreationPendingDialog()
		{
			try
			{
				await _accountDialogDisplayActions.ShowCreatePendingDialog();
			}
			catch (OperationCanceledException) { return false; }

			return true;
		}

		private void SetInitialStateAndShowError(NoDataSentException ex)
{'fraction_non_alphanumeric': 0.06092063925693211, 'fraction_numerical': 0.002458680508127305, 'mean_word_length': 5.68675799086758, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1552897', 'n_tokens_mistral': 2169, 'n_tokens_neox': 1787, 'n_words': 371}