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

// <auto-generated/>

#nullable disable

using System;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.ResourceManager.Authorization.Models;

namespace Azure.ResourceManager.Authorization
{
    /// <summary> The RoleDefinitions service client. </summary>
    public partial class RoleDefinitionsOperations
    {
        private readonly ClientDiagnostics _clientDiagnostics;
        private readonly HttpPipeline _pipeline;
        internal RoleDefinitionsRestOperations RestClient { get; }
        /// <summary> Initializes a new instance of RoleDefinitionsOperations for mocking. </summary>
        protected RoleDefinitionsOperations()
        {
        }
        /// <summary> Initializes a new instance of RoleDefinitionsOperations. </summary>
        /// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param>
        /// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param>
        /// <param name="endpoint"> server parameter. </param>
        internal RoleDefinitionsOperations(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint = null)
        {
            RestClient = new RoleDefinitionsRestOperations(clientDiagnostics, pipeline, endpoint);
            _clientDiagnostics = clientDiagnostics;
            _pipeline = pipeline;
        }

        /// <summary> Deletes a role definition. </summary>
        /// <param name="scope"> The scope of the role definition. </param>
        /// <param name="roleDefinitionId"> The ID of the role definition to delete. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        public virtual async Task<Response<RoleDefinition>> DeleteAsync(string scope, string roleDefinitionId, CancellationToken cancellationToken = default)
        {
            using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.Delete");
            scope0.Start();
            try
            {
                return await RestClient.DeleteAsync(scope, roleDefinitionId, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                scope0.Failed(e);
                throw;
            }
        }

        /// <summary> Deletes a role definition. </summary>
        /// <param name="scope"> The scope of the role definition. </param>
        /// <param name="roleDefinitionId"> The ID of the role definition to delete. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        public virtual Response<RoleDefinition> Delete(string scope, string roleDefinitionId, CancellationToken cancellationToken = default)
        {
            using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.Delete");
            scope0.Start();
            try
            {
                return RestClient.Delete(scope, roleDefinitionId, cancellationToken);
            }
            catch (Exception e)
            {
                scope0.Failed(e);
                throw;
            }
        }

        /// <summary> Get role definition by name (GUID). </summary>
        /// <param name="scope"> The scope of the role definition. </param>
        /// <param name="roleDefinitionId"> The ID of the role definition. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        public virtual async Task<Response<RoleDefinition>> GetAsync(string scope, string roleDefinitionId, CancellationToken cancellationToken = default)
        {
            using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.Get");
            scope0.Start();
            try
            {
                return await RestClient.GetAsync(scope, roleDefinitionId, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                scope0.Failed(e);
                throw;
            }
        }

        /// <summary> Get role definition by name (GUID). </summary>
        /// <param name="scope"> The scope of the role definition. </param>
        /// <param name="roleDefinitionId"> The ID of the role definition. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        public virtual Response<RoleDefinition> Get(string scope, string roleDefinitionId, CancellationToken cancellationToken = default)
        {
            using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.Get");
            scope0.Start();
            try
            {
                return RestClient.Get(scope, roleDefinitionId, cancellationToken);
            }
            catch (Exception e)
            {
                scope0.Failed(e);
                throw;
            }
        }

        /// <summary> Creates or updates a role definition. </summary>
        /// <param name="scope"> The scope of the role definition. </param>
        /// <param name="roleDefinitionId"> The ID of the role definition. </param>
        /// <param name="roleDefinition"> The values for the role definition. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        public virtual async Task<Response<RoleDefinition>> CreateOrUpdateAsync(string scope, string roleDefinitionId, RoleDefinition roleDefinition, CancellationToken cancellationToken = default)
        {
            using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.CreateOrUpdate");
            scope0.Start();
            try
            {
                return await RestClient.CreateOrUpdateAsync(scope, roleDefinitionId, roleDefinition, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                scope0.Failed(e);
                throw;
            }
        }

        /// <summary> Creates or updates a role definition. </summary>
        /// <param name="scope"> The scope of the role definition. </param>
        /// <param name="roleDefinitionId"> The ID of the role definition. </param>
        /// <param name="roleDefinition"> The values for the role definition. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        public virtual Response<RoleDefinition> CreateOrUpdate(string scope, string roleDefinitionId, RoleDefinition roleDefinition, CancellationToken cancellationToken = default)
        {
            using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.CreateOrUpdate");
            scope0.Start();
            try
            {
                return RestClient.CreateOrUpdate(scope, roleDefinitionId, roleDefinition, cancellationToken);
            }
            catch (Exception e)
            {
                scope0.Failed(e);
                throw;
            }
        }

        /// <summary> Gets a role definition by ID. </summary>
        /// <param name="roleId"> The fully qualified role definition ID. Use the format, /subscriptions/{guid}/providers/Microsoft.Authorization/roleDefinitions/{roleDefinitionId} for subscription level role definitions, or /providers/Microsoft.Authorization/roleDefinitions/{roleDefinitionId} for tenant level role definitions. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        public virtual async Task<Response<RoleDefinition>> GetByIdAsync(string roleId, CancellationToken cancellationToken = default)
        {
            using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.GetById");
            scope0.Start();
            try
            {
                return await RestClient.GetByIdAsync(roleId, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                scope0.Failed(e);
                throw;
            }
        }

        /// <summary> Gets a role definition by ID. </summary>
        /// <param name="roleId"> The fully qualified role definition ID. Use the format, /subscriptions/{guid}/providers/Microsoft.Authorization/roleDefinitions/{roleDefinitionId} for subscription level role definitions, or /providers/Microsoft.Authorization/roleDefinitions/{roleDefinitionId} for tenant level role definitions. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        public virtual Response<RoleDefinition> GetById(string roleId, CancellationToken cancellationToken = default)
        {
            using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.GetById");
            scope0.Start();
            try
            {
                return RestClient.GetById(roleId, cancellationToken);
            }
            catch (Exception e)
            {
                scope0.Failed(e);
                throw;
            }
        }

        /// <summary> Get all role definitions that are applicable at scope and above. </summary>
        /// <param name="scope"> The scope of the role definition. </param>
        /// <param name="filter"> The filter to apply on the operation. Use atScopeAndBelow filter to search below the given scope as well. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="scope"/> is null. </exception>
        public virtual AsyncPageable<RoleDefinition> ListAsync(string scope, string filter = null, CancellationToken cancellationToken = default)
        {
            if (scope == null)
            {
                throw new ArgumentNullException(nameof(scope));
            }

            async Task<Page<RoleDefinition>> FirstPageFunc(int? pageSizeHint)
            {
                using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.List");
                scope0.Start();
                try
                {
                    var response = await RestClient.ListAsync(scope, filter, cancellationToken).ConfigureAwait(false);
                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
                }
                catch (Exception e)
                {
                    scope0.Failed(e);
                    throw;
                }
            }
            async Task<Page<RoleDefinition>> NextPageFunc(string nextLink, int? pageSizeHint)
            {
                using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.List");
                scope0.Start();
                try
                {
                    var response = await RestClient.ListNextPageAsync(nextLink, scope, filter, cancellationToken).ConfigureAwait(false);
                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
                }
                catch (Exception e)
                {
                    scope0.Failed(e);
                    throw;
                }
            }
            return PageableHelpers.CreateAsyncEnumerable(FirstPageFunc, NextPageFunc);
        }

        /// <summary> Get all role definitions that are applicable at scope and above. </summary>
        /// <param name="scope"> The scope of the role definition. </param>
        /// <param name="filter"> The filter to apply on the operation. Use atScopeAndBelow filter to search below the given scope as well. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="scope"/> is null. </exception>
        public virtual Pageable<RoleDefinition> List(string scope, string filter = null, CancellationToken cancellationToken = default)
        {
            if (scope == null)
            {
                throw new ArgumentNullException(nameof(scope));
            }

            Page<RoleDefinition> FirstPageFunc(int? pageSizeHint)
            {
                using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.List");
                scope0.Start();
                try
                {
                    var response = RestClient.List(scope, filter, cancellationToken);
                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
                }
                catch (Exception e)
                {
                    scope0.Failed(e);
                    throw;
                }
            }
            Page<RoleDefinition> NextPageFunc(string nextLink, int? pageSizeHint)
            {
                using var scope0 = _clientDiagnostics.CreateScope("RoleDefinitionsOperations.List");
                scope0.Start();
                try
                {
                    var response = RestClient.ListNextPage(nextLink, scope, filter, cancellationToken);
                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
                }
                catch (Exception e)
                {
                    scope0.Failed(e);
                    throw;
                }
            }
            return PageableHelpers.CreateEnumerable(FirstPageFunc, NextPageFunc);
        }
    }
}
