// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) Go Code Generator. DO NOT EDIT.

package armdatafactory

import (
	"context"
	"errors"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"net/http"
	"net/url"
	"strings"
)

// GlobalParametersClient contains the methods for the GlobalParameters group.
// Don't use this type directly, use NewGlobalParametersClient() instead.
//
// Generated from API version 2018-06-01
type GlobalParametersClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewGlobalParametersClient creates a new instance of GlobalParametersClient with the specified values.
//   - subscriptionID - The ID of the target subscription. The value must be an UUID.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - Contains optional client configuration. Pass nil to accept the default values.
func NewGlobalParametersClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*GlobalParametersClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &GlobalParametersClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// CreateOrUpdate - Creates or updates a Global parameter
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - factoryName - The factory name.
//   - globalParameterName - The global parameter name.
//   - defaultParam - Global parameter resource definition.
//   - options - GlobalParametersClientCreateOrUpdateOptions contains the optional parameters for the GlobalParametersClient.CreateOrUpdate
//     method.
func (client *GlobalParametersClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, factoryName string, globalParameterName string, defaultParam GlobalParameterResource, options *GlobalParametersClientCreateOrUpdateOptions) (GlobalParametersClientCreateOrUpdateResponse, error) {
	var err error
	const operationName = "GlobalParametersClient.CreateOrUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, factoryName, globalParameterName, defaultParam, options)
	if err != nil {
		return GlobalParametersClientCreateOrUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return GlobalParametersClientCreateOrUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return GlobalParametersClientCreateOrUpdateResponse{}, err
	}
	resp, err := client.createOrUpdateHandleResponse(httpResp)
	return resp, err
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *GlobalParametersClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, factoryName string, globalParameterName string, defaultParam GlobalParameterResource, _ *GlobalParametersClientCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataFactory/factories/{factoryName}/globalParameters/{globalParameterName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if factoryName == "" {
		return nil, errors.New("parameter factoryName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{factoryName}", url.PathEscape(factoryName))
	if globalParameterName == "" {
		return nil, errors.New("parameter globalParameterName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{globalParameterName}", url.PathEscape(globalParameterName))
	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20180601)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, defaultParam); err != nil {
		return nil, err
	}
	return req, nil
}

// createOrUpdateHandleResponse handles the CreateOrUpdate response.
func (client *GlobalParametersClient) createOrUpdateHandleResponse(resp *http.Response) (GlobalParametersClientCreateOrUpdateResponse, error) {
	result := GlobalParametersClientCreateOrUpdateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.GlobalParameterResource); err != nil {
		return GlobalParametersClientCreateOrUpdateResponse{}, err
	}
	return result, nil
}

// Delete - Deletes a Global parameter
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - factoryName - The factory name.
//   - globalParameterName - The global parameter name.
//   - options - GlobalParametersClientDeleteOptions contains the optional parameters for the GlobalParametersClient.Delete method.
func (client *GlobalParametersClient) Delete(ctx context.Context, resourceGroupName string, factoryName string, globalParameterName string, options *GlobalParametersClientDeleteOptions) (GlobalParametersClientDeleteResponse, error) {
	var err error
	const operationName = "GlobalParametersClient.Delete"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.deleteCreateRequest(ctx, resourceGroupName, factoryName, globalParameterName, options)
	if err != nil {
		return GlobalParametersClientDeleteResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return GlobalParametersClientDeleteResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return GlobalParametersClientDeleteResponse{}, err
	}
	return GlobalParametersClientDeleteResponse{}, nil
}

// deleteCreateRequest creates the Delete request.
func (client *GlobalParametersClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, factoryName string, globalParameterName string, _ *GlobalParametersClientDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataFactory/factories/{factoryName}/globalParameters/{globalParameterName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if factoryName == "" {
		return nil, errors.New("parameter factoryName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{factoryName}", url.PathEscape(factoryName))
	if globalParameterName == "" {
		return nil, errors.New("parameter globalParameterName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{globalParameterName}", url.PathEscape(globalParameterName))
	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20180601)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	return req, nil
}

// Get - Gets a Global parameter
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - factoryName - The factory name.
//   - globalParameterName - The global parameter name.
//   - options - GlobalParametersClientGetOptions contains the optional parameters for the GlobalParametersClient.Get method.
func (client *GlobalParametersClient) Get(ctx context.Context, resourceGroupName string, factoryName string, globalParameterName string, options *GlobalParametersClientGetOptions) (GlobalParametersClientGetResponse, error) {
	var err error
	const operationName = "GlobalParametersClient.Get"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getCreateRequest(ctx, resourceGroupName, factoryName, globalParameterName, options)
	if err != nil {
		return GlobalParametersClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return GlobalParametersClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return GlobalParametersClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *GlobalParametersClient) getCreateRequest(ctx context.Context, resourceGroupName string, factoryName string, globalParameterName string, _ *GlobalParametersClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataFactory/factories/{factoryName}/globalParameters/{globalParameterName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if factoryName == "" {
		return nil, errors.New("parameter factoryName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{factoryName}", url.PathEscape(factoryName))
	if globalParameterName == "" {
		return nil, errors.New("parameter globalParameterName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{globalParameterName}", url.PathEscape(globalParameterName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20180601)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getHandleResponse handles the Get response.
func (client *GlobalParametersClient) getHandleResponse(resp *http.Response) (GlobalParametersClientGetResponse, error) {
	result := GlobalParametersClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.GlobalParameterResource); err != nil {
		return GlobalParametersClientGetResponse{}, err
	}
	return result, nil
}

// NewListByFactoryPager - Lists Global parameters
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - factoryName - The factory name.
//   - options - GlobalParametersClientListByFactoryOptions contains the optional parameters for the GlobalParametersClient.NewListByFactoryPager
//     method.
func (client *GlobalParametersClient) NewListByFactoryPager(resourceGroupName string, factoryName string, options *GlobalParametersClientListByFactoryOptions) *runtime.Pager[GlobalParametersClientListByFactoryResponse] {
	return runtime.NewPager(runtime.PagingHandler[GlobalParametersClientListByFactoryResponse]{
		More: func(page GlobalParametersClientListByFactoryResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *GlobalParametersClientListByFactoryResponse) (GlobalParametersClientListByFactoryResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "GlobalParametersClient.NewListByFactoryPager")
			nextLink := ""
			if page != nil {
				nextLink = *page.NextLink
			}
			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
				return client.listByFactoryCreateRequest(ctx, resourceGroupName, factoryName, options)
			}, nil)
			if err != nil {
				return GlobalParametersClientListByFactoryResponse{}, err
			}
			return client.listByFactoryHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByFactoryCreateRequest creates the ListByFactory request.
func (client *GlobalParametersClient) listByFactoryCreateRequest(ctx context.Context, resourceGroupName string, factoryName string, _ *GlobalParametersClientListByFactoryOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataFactory/factories/{factoryName}/globalParameters"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if factoryName == "" {
		return nil, errors.New("parameter factoryName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{factoryName}", url.PathEscape(factoryName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20180601)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByFactoryHandleResponse handles the ListByFactory response.
func (client *GlobalParametersClient) listByFactoryHandleResponse(resp *http.Response) (GlobalParametersClientListByFactoryResponse, error) {
	result := GlobalParametersClientListByFactoryResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.GlobalParameterListResponse); err != nil {
		return GlobalParametersClientListByFactoryResponse{}, err
	}
	return result, nil
}
