// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Core.Pipeline;

namespace Microsoft.Azure.NotificationHubs
{
    // Data plane generated client. Azure Notification Hubs scheduled send operations
    /// <summary> Azure Notification Hubs scheduled send operations. </summary>
    public partial class ScheduledNotificationsClient
    {
        private readonly HttpPipeline _pipeline;
        private readonly string _namespaceName;
        private readonly string _hubName;
        private readonly string _apiVersion;

        /// <summary> The ClientDiagnostics is used to provide tracing support for the client library. </summary>
        internal ClientDiagnostics ClientDiagnostics { get; }

        /// <summary> The HTTP pipeline for sending and receiving REST requests and responses. </summary>
        public virtual HttpPipeline Pipeline => _pipeline;

        /// <summary> Initializes a new instance of ScheduledNotificationsClient for mocking. </summary>
        protected ScheduledNotificationsClient()
        {
        }

        /// <summary> Initializes a new instance of ScheduledNotificationsClient. </summary>
        /// <param name="namespaceName"> Notification Hubs Namespace. </param>
        /// <param name="hubName"> Notification Hub Name. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="namespaceName"/> or <paramref name="hubName"/> is null. </exception>
        /// <exception cref="ArgumentException"> <paramref name="namespaceName"/> or <paramref name="hubName"/> is an empty string, and was expected to be non-empty. </exception>
        public ScheduledNotificationsClient(string namespaceName, string hubName) : this(namespaceName, hubName, new NotificationhubsClientOptions())
        {
        }

        /// <summary> Initializes a new instance of ScheduledNotificationsClient. </summary>
        /// <param name="namespaceName"> Notification Hubs Namespace. </param>
        /// <param name="hubName"> Notification Hub Name. </param>
        /// <param name="options"> The options for configuring the client. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="namespaceName"/> or <paramref name="hubName"/> is null. </exception>
        /// <exception cref="ArgumentException"> <paramref name="namespaceName"/> or <paramref name="hubName"/> is an empty string, and was expected to be non-empty. </exception>
        public ScheduledNotificationsClient(string namespaceName, string hubName, NotificationhubsClientOptions options)
        {
            Argument.AssertNotNullOrEmpty(namespaceName, nameof(namespaceName));
            Argument.AssertNotNullOrEmpty(hubName, nameof(hubName));
            options ??= new NotificationhubsClientOptions();

            ClientDiagnostics = new ClientDiagnostics(options, true);
            _pipeline = HttpPipelineBuilder.Build(options, Array.Empty<HttpPipelinePolicy>(), Array.Empty<HttpPipelinePolicy>(), new ResponseClassifier());
            _namespaceName = namespaceName;
            _hubName = hubName;
            _apiVersion = options.Version;
        }

        /// <summary> Cancel a scheduled notification. </summary>
        /// <param name="notificationId"> The ID of the notification to cancel. </param>
        /// <param name="context"> The request context, which can override default behaviors of the client pipeline on a per-call basis. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="notificationId"/> is null. </exception>
        /// <exception cref="ArgumentException"> <paramref name="notificationId"/> is an empty string, and was expected to be non-empty. </exception>
        /// <exception cref="RequestFailedException"> Service returned a non-success status code. </exception>
        /// <returns> The response returned from the service. </returns>
        /// <example>
        /// This sample shows how to call CancelNotificationAsync with required parameters.
        /// <code><![CDATA[
        /// var client = new ScheduledNotificationsClient("<namespaceName>", "<hubName>");
        /// 
        /// Response response = await client.CancelNotificationAsync("<notificationId>");
        /// Console.WriteLine(response.Status);
        /// ]]></code>
        /// </example>
        public virtual async Task<Response> CancelNotificationAsync(string notificationId, RequestContext context = null)
        {
            Argument.AssertNotNullOrEmpty(notificationId, nameof(notificationId));

            using var scope = ClientDiagnostics.CreateScope("ScheduledNotificationsClient.CancelNotification");
            scope.Start();
            try
            {
                using HttpMessage message = CreateCancelNotificationRequest(notificationId, context);
                return await _pipeline.ProcessMessageAsync(message, context).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }

        /// <summary> Cancel a scheduled notification. </summary>
        /// <param name="notificationId"> The ID of the notification to cancel. </param>
        /// <param name="context"> The request context, which can override default behaviors of the client pipeline on a per-call basis. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="notificationId"/> is null. </exception>
        /// <exception cref="ArgumentException"> <paramref name="notificationId"/> is an empty string, and was expected to be non-empty. </exception>
        /// <exception cref="RequestFailedException"> Service returned a non-success status code. </exception>
        /// <returns> The response returned from the service. </returns>
        /// <example>
        /// This sample shows how to call CancelNotification with required parameters.
        /// <code><![CDATA[
        /// var client = new ScheduledNotificationsClient("<namespaceName>", "<hubName>");
        /// 
        /// Response response = client.CancelNotification("<notificationId>");
        /// Console.WriteLine(response.Status);
        /// ]]></code>
        /// </example>
        public virtual Response CancelNotification(string notificationId, RequestContext context = null)
        {
            Argument.AssertNotNullOrEmpty(notificationId, nameof(notificationId));

            using var scope = ClientDiagnostics.CreateScope("ScheduledNotificationsClient.CancelNotification");
            scope.Start();
            try
            {
                using HttpMessage message = CreateCancelNotificationRequest(notificationId, context);
                return _pipeline.ProcessMessage(message, context);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }

        /// <summary> Schedule a notification. </summary>
        /// <param name="scheduleTime"> The notification scheduled time. </param>
        /// <param name="platform"> The notification target platform. </param>
        /// <param name="content"> The content to send as the body of the request. Details of the request body schema are in the Remarks section below. </param>
        /// <param name="tags"> The notification target tag expression. </param>
        /// <param name="context"> The request context, which can override default behaviors of the client pipeline on a per-call basis. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="scheduleTime"/>, <paramref name="platform"/> or <paramref name="content"/> is null. </exception>
        /// <exception cref="RequestFailedException"> Service returned a non-success status code. </exception>
        /// <returns> The response returned from the service. </returns>
        /// <example>
        /// This sample shows how to call ScheduleNotificationAsync with required parameters and request content.
        /// <code><![CDATA[
        /// var client = new ScheduledNotificationsClient("<namespaceName>", "<hubName>");
        /// 
        /// var data = "<String>";
        /// 
        /// Response response = await client.ScheduleNotificationAsync("<scheduleTime>", "<platform>", RequestContent.Create(data));
        /// Console.WriteLine(response.Status);
        /// ]]></code>
        /// This sample shows how to call ScheduleNotificationAsync with all parameters and request content.
        /// <code><![CDATA[
        /// var client = new ScheduledNotificationsClient("<namespaceName>", "<hubName>");
        /// 
        /// var data = "<String>";
        /// 
        /// Response response = await client.ScheduleNotificationAsync("<scheduleTime>", "<platform>", RequestContent.Create(data), "<tags>");
        /// Console.WriteLine(response.Status);
        /// ]]></code>
        /// </example>
        public virtual async Task<Response> ScheduleNotificationAsync(string scheduleTime, string platform, RequestContent content, string tags = null, RequestContext context = null)
        {
            Argument.AssertNotNull(scheduleTime, nameof(scheduleTime));
            Argument.AssertNotNull(platform, nameof(platform));
            Argument.AssertNotNull(content, nameof(content));

            using var scope = ClientDiagnostics.CreateScope("ScheduledNotificationsClient.ScheduleNotification");
            scope.Start();
            try
            {
                using HttpMessage message = CreateScheduleNotificationRequest(scheduleTime, platform, content, tags, context);
                return await _pipeline.ProcessMessageAsync(message, context).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }

        /// <summary> Schedule a notification. </summary>
        /// <param name="scheduleTime"> The notification scheduled time. </param>
        /// <param name="platform"> The notification target platform. </param>
        /// <param name="content"> The content to send as the body of the request. Details of the request body schema are in the Remarks section below. </param>
        /// <param name="tags"> The notification target tag expression. </param>
        /// <param name="context"> The request context, which can override default behaviors of the client pipeline on a per-call basis. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="scheduleTime"/>, <paramref name="platform"/> or <paramref name="content"/> is null. </exception>
        /// <exception cref="RequestFailedException"> Service returned a non-success status code. </exception>
        /// <returns> The response returned from the service. </returns>
        /// <example>
        /// This sample shows how to call ScheduleNotification with required parameters and request content.
        /// <code><![CDATA[
        /// var client = new ScheduledNotificationsClient("<namespaceName>", "<hubName>");
        /// 
        /// var data = "<String>";
        /// 
        /// Response response = client.ScheduleNotification("<scheduleTime>", "<platform>", RequestContent.Create(data));
        /// Console.WriteLine(response.Status);
        /// ]]></code>
        /// This sample shows how to call ScheduleNotification with all parameters and request content.
        /// <code><![CDATA[
        /// var client = new ScheduledNotificationsClient("<namespaceName>", "<hubName>");
        /// 
        /// var data = "<String>";
        /// 
        /// Response response = client.ScheduleNotification("<scheduleTime>", "<platform>", RequestContent.Create(data), "<tags>");
        /// Console.WriteLine(response.Status);
        /// ]]></code>
        /// </example>
        public virtual Response ScheduleNotification(string scheduleTime, string platform, RequestContent content, string tags = null, RequestContext context = null)
        {
            Argument.AssertNotNull(scheduleTime, nameof(scheduleTime));
            Argument.AssertNotNull(platform, nameof(platform));
            Argument.AssertNotNull(content, nameof(content));

            using var scope = ClientDiagnostics.CreateScope("ScheduledNotificationsClient.ScheduleNotification");
            scope.Start();
            try
            {
                using HttpMessage message = CreateScheduleNotificationRequest(scheduleTime, platform, content, tags, context);
                return _pipeline.ProcessMessage(message, context);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }

        internal HttpMessage CreateCancelNotificationRequest(string notificationId, RequestContext context)
        {
            var message = _pipeline.CreateMessage(context, ResponseClassifier200);
            var request = message.Request;
            request.Method = RequestMethod.Delete;
            var uri = new RawRequestUriBuilder();
            uri.AppendRaw("https://", false);
            uri.AppendRaw(_namespaceName, true);
            uri.AppendRaw(".servicebus.windows.net/", false);
            uri.AppendRaw(_hubName, true);
            uri.AppendPath("/schedulednotifications/", false);
            uri.AppendPath(notificationId, true);
            uri.AppendQuery("api-version", _apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json");
            return message;
        }

        internal HttpMessage CreateScheduleNotificationRequest(string scheduleTime, string platform, RequestContent content, string tags, RequestContext context)
        {
            var message = _pipeline.CreateMessage(context, ResponseClassifier201);
            var request = message.Request;
            request.Method = RequestMethod.Post;
            var uri = new RawRequestUriBuilder();
            uri.AppendRaw("https://", false);
            uri.AppendRaw(_namespaceName, true);
            uri.AppendRaw(".servicebus.windows.net/", false);
            uri.AppendRaw(_hubName, true);
            uri.AppendPath("/schedulednotifications", false);
            uri.AppendQuery("api-version", _apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("ServiceBusNotification-ScheduleTime", scheduleTime);
            request.Headers.Add("ServiceBusNotification-Format", platform);
            if (tags != null)
            {
                request.Headers.Add("ServiceBusNotification-Tags", tags);
            }
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("Content-Type", "application/json");
            request.Content = content;
            return message;
        }

        private static ResponseClassifier _responseClassifier200;
        private static ResponseClassifier ResponseClassifier200 => _responseClassifier200 ??= new StatusCodeClassifier(stackalloc ushort[] { 200 });
        private static ResponseClassifier _responseClassifier201;
        private static ResponseClassifier ResponseClassifier201 => _responseClassifier201 ??= new StatusCodeClassifier(stackalloc ushort[] { 201 });
    }
}
