using Hcs.Broker.Internal;
using Hcs.Service.Async.Bills;

namespace Hcs.Broker.Api.Request.Bills
{
    internal class ExportPaymentDocumentDataRequest(Client client) : BillsRequestBase(client)
    {
        protected override bool CanBeRestarted => true;

        internal async Task<IEnumerable<exportPaymentDocumentResultType>> ExecuteByPaymentDocumentIDAsync(string paymentDocumentID, CancellationToken token)
        {
            var request = new exportPaymentDocumentRequest()
            {
                Id = Constants.SIGNED_XML_ELEMENT_ID,
                version = "13.1.0.1",
                Items = [paymentDocumentID],
                ItemsElementName = [ItemsChoiceType7.PaymentDocumentID]
            };

            var result = await SendAndWaitResultAsync(request, async asyncClient =>
            {
                var response = await asyncClient.exportPaymentDocumentDataAsync(CreateRequestHeader(), request);
                return response.AckRequest.Ack;
            }, token);

            return result.Items.OfType<exportPaymentDocumentResultType>();
        }

        internal async Task<IEnumerable<exportPaymentDocumentResultType>> ExecuteByAccountNumberAsync(short year, int month, string fiasHouseGuid, string accountNumber, CancellationToken token)
        {
            var request = new exportPaymentDocumentRequest()
            {
                Id = Constants.SIGNED_XML_ELEMENT_ID,
                version = "13.1.0.1",
                Items = [year, month, fiasHouseGuid, accountNumber],
                ItemsElementName = [ItemsChoiceType7.Year, ItemsChoiceType7.Month, ItemsChoiceType7.FIASHouseGuid, ItemsChoiceType7.AccountNumber]
            };

            var result = await SendAndWaitResultAsync(request, async asyncClient =>
            {
                var response = await asyncClient.exportPaymentDocumentDataAsync(CreateRequestHeader(), request);
                return response.AckRequest.Ack;
            }, token);

            return result.Items.OfType<exportPaymentDocumentResultType>();
        }

        internal async Task<IEnumerable<exportPaymentDocumentResultType>> ExecuteByPaymentDocumentNumberAsync(short year, int month, string fiasHouseGuid, string paymentDocumentNumber, CancellationToken token)
        {
            var request = new exportPaymentDocumentRequest()
            {
                Id = Constants.SIGNED_XML_ELEMENT_ID,
                version = "13.1.0.1",
                Items = [year, month, fiasHouseGuid, paymentDocumentNumber],
                ItemsElementName = [ItemsChoiceType7.Year, ItemsChoiceType7.Month, ItemsChoiceType7.FIASHouseGuid, ItemsChoiceType7.PaymentDocumentNumber]
            };

            var result = await SendAndWaitResultAsync(request, async asyncClient =>
            {
                var response = await asyncClient.exportPaymentDocumentDataAsync(CreateRequestHeader(), request);
                return response.AckRequest.Ack;
            }, token);

            return result.Items.OfType<exportPaymentDocumentResultType>();
        }
    }
}
