// <auto-generated>
// DO NOT EDIT: generated by fsdgencsharp
// </auto-generated>
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Facility.Core;
using Facility.Core.Http;

namespace OapApi.v1.Client.Http
{
	[System.CodeDom.Compiler.GeneratedCode("fsdgencsharp", "")]
	public sealed partial class OapApiV1HttpHandler : ServiceHttpHandler
	{
		/// <summary>
		/// Creates the handler.
		/// </summary>
		public OapApiV1HttpHandler(IOapApiV1 service, ServiceHttpHandlerSettings settings = null)
			: base(settings, s_defaults)
		{
			m_service = service ?? throw new ArgumentNullException(nameof(service));
		}

		/// <summary>
		/// Creates the handler.
		/// </summary>
		public OapApiV1HttpHandler(Func<HttpRequestMessage, IOapApiV1> getService, ServiceHttpHandlerSettings settings = null)
			: base(settings, s_defaults)
		{
			m_getService = getService ?? throw new ArgumentNullException(nameof(getService));
		}

		/// <summary>
		/// Attempts to handle the HTTP request.
		/// </summary>
		public override async Task<HttpResponseMessage> TryHandleHttpRequestAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken = default)
		{
			return await AdaptTask(TryHandleGetApplicationsAsync(httpRequest, cancellationToken)).ConfigureAwait(true) ??
				await AdaptTask(TryHandleCreateApplicationAsync(httpRequest, cancellationToken)).ConfigureAwait(true) ??
				await AdaptTask(TryHandleGetApplicationAsync(httpRequest, cancellationToken)).ConfigureAwait(true) ??
				await AdaptTask(TryHandleUpdateApplicationAsync(httpRequest, cancellationToken)).ConfigureAwait(true);
		}

		/// <summary>
		/// Gets applications for the current user.
		/// </summary>
		public Task<HttpResponseMessage> TryHandleGetApplicationsAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken = default) =>
			TryHandleServiceMethodAsync(OapApiV1HttpMapping.GetApplicationsMapping, httpRequest, GetService(httpRequest).GetApplicationsAsync, cancellationToken);

		/// <summary>
		/// Creates a new application for the current user.
		/// </summary>
		public Task<HttpResponseMessage> TryHandleCreateApplicationAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken = default) =>
			TryHandleServiceMethodAsync(OapApiV1HttpMapping.CreateApplicationMapping, httpRequest, GetService(httpRequest).CreateApplicationAsync, cancellationToken);

		/// <summary>
		/// Gets the specified application for the current user.
		/// </summary>
		public Task<HttpResponseMessage> TryHandleGetApplicationAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken = default) =>
			TryHandleServiceMethodAsync(OapApiV1HttpMapping.GetApplicationMapping, httpRequest, GetService(httpRequest).GetApplicationAsync, cancellationToken);

		/// <summary>
		/// Updates the application.
		/// </summary>
		public Task<HttpResponseMessage> TryHandleUpdateApplicationAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken = default) =>
			TryHandleServiceMethodAsync(OapApiV1HttpMapping.UpdateApplicationMapping, httpRequest, GetService(httpRequest).UpdateApplicationAsync, cancellationToken);

		private IOapApiV1 GetService(HttpRequestMessage httpRequest) => m_service ?? m_getService(httpRequest);

		private static readonly ServiceHttpHandlerDefaults s_defaults = new ServiceHttpHandlerDefaults
		{
			ContentSerializer = HttpContentSerializer.Create(SystemTextJsonServiceSerializer.Instance),
		};

		private readonly IOapApiV1 m_service;
		private readonly Func<HttpRequestMessage, IOapApiV1> m_getService;
	}
}
