// 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) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package armmigrationassessment

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"
)

// AvsAssessmentOptionsOperationsClient contains the methods for the AvsAssessmentOptionsOperations group.
// Don't use this type directly, use NewAvsAssessmentOptionsOperationsClient() instead.
type AvsAssessmentOptionsOperationsClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewAvsAssessmentOptionsOperationsClient creates a new instance of AvsAssessmentOptionsOperationsClient 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 - pass nil to accept the default values.
func NewAvsAssessmentOptionsOperationsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*AvsAssessmentOptionsOperationsClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &AvsAssessmentOptionsOperationsClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// Get - Get a AvsAssessmentOptions
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-01-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - projectName - Assessment Project Name
//   - avsAssessmentOptionsName - AVS Assessment options ARM name. Accepted value is 'default'
//   - options - AvsAssessmentOptionsOperationsClientGetOptions contains the optional parameters for the AvsAssessmentOptionsOperationsClient.Get
//     method.
func (client *AvsAssessmentOptionsOperationsClient) Get(ctx context.Context, resourceGroupName string, projectName string, avsAssessmentOptionsName string, options *AvsAssessmentOptionsOperationsClientGetOptions) (AvsAssessmentOptionsOperationsClientGetResponse, error) {
	var err error
	const operationName = "AvsAssessmentOptionsOperationsClient.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, projectName, avsAssessmentOptionsName, options)
	if err != nil {
		return AvsAssessmentOptionsOperationsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return AvsAssessmentOptionsOperationsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return AvsAssessmentOptionsOperationsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *AvsAssessmentOptionsOperationsClient) getCreateRequest(ctx context.Context, resourceGroupName string, projectName string, avsAssessmentOptionsName string, _ *AvsAssessmentOptionsOperationsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Migrate/assessmentProjects/{projectName}/avsAssessmentOptions/{avsAssessmentOptionsName}"
	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 projectName == "" {
		return nil, errors.New("parameter projectName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{projectName}", url.PathEscape(projectName))
	if avsAssessmentOptionsName == "" {
		return nil, errors.New("parameter avsAssessmentOptionsName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{avsAssessmentOptionsName}", url.PathEscape(avsAssessmentOptionsName))
	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", "2024-01-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByAssessmentProjectPager - List AvsAssessmentOptions resources by AssessmentProject
//
// Generated from API version 2024-01-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - projectName - Assessment Project Name
//   - options - AvsAssessmentOptionsOperationsClientListByAssessmentProjectOptions contains the optional parameters for the AvsAssessmentOptionsOperationsClient.NewListByAssessmentProjectPager
//     method.
func (client *AvsAssessmentOptionsOperationsClient) NewListByAssessmentProjectPager(resourceGroupName string, projectName string, options *AvsAssessmentOptionsOperationsClientListByAssessmentProjectOptions) *runtime.Pager[AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse] {
	return runtime.NewPager(runtime.PagingHandler[AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse]{
		More: func(page AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse) (AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "AvsAssessmentOptionsOperationsClient.NewListByAssessmentProjectPager")
			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.listByAssessmentProjectCreateRequest(ctx, resourceGroupName, projectName, options)
			}, nil)
			if err != nil {
				return AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse{}, err
			}
			return client.listByAssessmentProjectHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByAssessmentProjectCreateRequest creates the ListByAssessmentProject request.
func (client *AvsAssessmentOptionsOperationsClient) listByAssessmentProjectCreateRequest(ctx context.Context, resourceGroupName string, projectName string, _ *AvsAssessmentOptionsOperationsClientListByAssessmentProjectOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Migrate/assessmentProjects/{projectName}/avsAssessmentOptions"
	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 projectName == "" {
		return nil, errors.New("parameter projectName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{projectName}", url.PathEscape(projectName))
	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", "2024-01-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByAssessmentProjectHandleResponse handles the ListByAssessmentProject response.
func (client *AvsAssessmentOptionsOperationsClient) listByAssessmentProjectHandleResponse(resp *http.Response) (AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse, error) {
	result := AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AvsAssessmentOptionsListResult); err != nil {
		return AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse{}, err
	}
	return result, nil
}
