using Hcs.Broker.Api.Request;
using Hcs.Broker.Api.Request.Exception;
using Hcs.Broker.Api.Request.NsiCommon;
using Hcs.Service.Async.NsiCommon;

namespace Hcs.Broker.Api
{
    /// <inheritdoc cref="INsiCommonApi"/>
    public class NsiCommonApi(Client client) : ApiBase(client), INsiCommonApi
    {
        /// <inheritdoc cref="INsiCommonApi"/>
        public async Task<NsiItemType> ExportNsiItemAsync(int registryNumber, ListGroup listGroup, CancellationToken token = default)
        {
            try
            {
                var request = new ExportNsiItemRequest(client);
                return await request.ExecuteAsync(registryNumber, listGroup, token);
            }
            catch (NoResultsRemoteException)
            {
                return null;
            }
        }

        /// <inheritdoc cref="INsiCommonApi"/>
        public async Task<NsiListType> ExportNsiListAsync(ListGroup listGroup, CancellationToken token = default)
        {
            try
            {
                var request = new ExportNsiListRequest(client);
                return await request.ExecuteAsync(listGroup, token);
            }
            catch (NoResultsRemoteException)
            {
                return null;
            }
        }

        /// <inheritdoc cref="INsiCommonApi"/>
        public async Task<string> RequestExportNsiItemAsync(int registryNumber, ListGroup listGroup, CancellationToken token = default)
        {
            var request = new ExportNsiItemRequest(client);
            return await request.SendAsync(registryNumber, listGroup, token);
        }

        /// <inheritdoc cref="INsiCommonApi"/>
        public async Task<RequestSingleResult<NsiItemType>> GetExportNsiItemResultAsync(string messageGuid)
        {
            var request = new ExportNsiItemRequest(client);
            return await request.GetResultAsync(messageGuid);
        }
    }
}
