using System; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using WotBlitzStatisticsPro.Common.Model; using WotBlitzStatisticsPro.Logic.AccountInformationPipeline; using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.OperationContext; using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations; using WotBlitzStatisticsPro.Logic.Pipeline; namespace WotBlitzStatisticsPro.Logic { public class WargamingAccounts: IWargamingAccounts { private readonly IOperationFactory _operationFactory; private readonly ILogger _logger; public WargamingAccounts(IOperationFactory operationFactory, ILogger logger) { _operationFactory = operationFactory; _logger = logger; } public async Task GatherAccountInformation( RealmType realm, long accountId, RequestLanguage requestLanguage) { var contextData = new AccountInformationPipelineContextData(); try { var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage)); context.AddOrReplace(contextData); var pipeline = new Pipeline(_operationFactory); pipeline.AddOperation() .AddOperation() .AddOperation() .AddOperation() ; var firstOperation = pipeline.Build(); if (firstOperation != null) { await firstOperation .Invoke(context, null) .ConfigureAwait(false); } } catch (Exception e) { _logger.LogError(e, "GatherAccountInformation error"); throw; } return contextData?.Response ?? new AccountInfoResponse(); } public async Task GatherAndSaveAccountInformation( RealmType realm, long accountId, RequestLanguage requestLanguage, string wargamingToken) { var contextData = new AccountInformationPipelineContextData(); try { var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage, wargamingToken)); context.AddOrReplace(contextData); var pipeline = new Pipeline(_operationFactory); pipeline.AddOperation() .AddOperation() .AddOperation() .AddOperation() .AddOperation() .AddOperation() .AddOperation() ; var firstOperation = pipeline.Build(); if (firstOperation != null) { await firstOperation .Invoke(context, null) .ConfigureAwait(false); } } catch (Exception e) { _logger.LogError(e, "GatherAndSaveAccountInformation error"); throw; } return contextData?.Response ?? new AccountInfoResponse(); } public async Task GetAccountInfoHistory(RealmType realm, long accountId, DateTime startDate, RequestLanguage requestLanguage) { var contextData = new AccountHistoryInformationPipelineContextData(startDate); try { var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage)); context.AddOrReplace(contextData); var pipeline = new Pipeline(_operationFactory); pipeline.AddOperation() .AddOperation() .AddOperation() .AddOperation() .AddOperation() .AddOperation() .AddOperation() .AddOperation() ; var firstOperation = pipeline.Build(); if (firstOperation != null) { await firstOperation .Invoke(context, null) .ConfigureAwait(false); } } catch (Exception e) { _logger.LogError(e, "GetAccountInfoHistory error"); throw; } return contextData?.Response ?? new AccountInfoHistoryResponse(); } public async Task GetTankInfoHistory(RealmType realm, long accountId, long tankId, DateTime startDate, RequestLanguage requestLanguage) { var contextData = new TankHistoryInformationContextData(startDate, tankId); try { var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage)); context.AddOrReplace(contextData); var pipeline = new Pipeline(_operationFactory); pipeline.AddOperation() .AddOperation() .AddOperation() .AddOperation() .AddOperation() .AddOperation() .AddOperation() .AddOperation() ; var firstOperation = pipeline.Build(); if (firstOperation != null) { await firstOperation .Invoke(context, null) .ConfigureAwait(false); } } catch (Exception e) { _logger.LogError(e, "GetAccountInfoHistory error"); throw; } return contextData?.Response ?? new TankInfoHistoryResponse(); } } }