// 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"
	"strconv"
	"strings"
)

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

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

// Get - Get a EvaluatedMachine
// 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
//   - businessCaseName - Business case ARM name
//   - evaluatedMachineName - Business case Evaluated machine ARM name
//   - options - EvaluatedMachinesOperationsClientGetOptions contains the optional parameters for the EvaluatedMachinesOperationsClient.Get
//     method.
func (client *EvaluatedMachinesOperationsClient) Get(ctx context.Context, resourceGroupName string, projectName string, businessCaseName string, evaluatedMachineName string, options *EvaluatedMachinesOperationsClientGetOptions) (EvaluatedMachinesOperationsClientGetResponse, error) {
	var err error
	const operationName = "EvaluatedMachinesOperationsClient.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, businessCaseName, evaluatedMachineName, options)
	if err != nil {
		return EvaluatedMachinesOperationsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return EvaluatedMachinesOperationsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return EvaluatedMachinesOperationsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *EvaluatedMachinesOperationsClient) getCreateRequest(ctx context.Context, resourceGroupName string, projectName string, businessCaseName string, evaluatedMachineName string, _ *EvaluatedMachinesOperationsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Migrate/assessmentProjects/{projectName}/businessCases/{businessCaseName}/evaluatedMachines/{evaluatedMachineName}"
	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 businessCaseName == "" {
		return nil, errors.New("parameter businessCaseName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{businessCaseName}", url.PathEscape(businessCaseName))
	if evaluatedMachineName == "" {
		return nil, errors.New("parameter evaluatedMachineName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{evaluatedMachineName}", url.PathEscape(evaluatedMachineName))
	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 *EvaluatedMachinesOperationsClient) getHandleResponse(resp *http.Response) (EvaluatedMachinesOperationsClientGetResponse, error) {
	result := EvaluatedMachinesOperationsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.EvaluatedMachine); err != nil {
		return EvaluatedMachinesOperationsClientGetResponse{}, err
	}
	return result, nil
}

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

// listByBusinessCaseCreateRequest creates the ListByBusinessCase request.
func (client *EvaluatedMachinesOperationsClient) listByBusinessCaseCreateRequest(ctx context.Context, resourceGroupName string, projectName string, businessCaseName string, options *EvaluatedMachinesOperationsClientListByBusinessCaseOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Migrate/assessmentProjects/{projectName}/businessCases/{businessCaseName}/evaluatedMachines"
	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 businessCaseName == "" {
		return nil, errors.New("parameter businessCaseName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{businessCaseName}", url.PathEscape(businessCaseName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	if options != nil && options.Filter != nil {
		reqQP.Set("$filter", *options.Filter)
	}
	reqQP.Set("api-version", "2024-01-01-preview")
	if options != nil && options.ContinuationToken != nil {
		reqQP.Set("continuationToken", *options.ContinuationToken)
	}
	if options != nil && options.PageSize != nil {
		reqQP.Set("pageSize", strconv.FormatInt(int64(*options.PageSize), 10))
	}
	if options != nil && options.TotalRecordCount != nil {
		reqQP.Set("totalRecordCount", strconv.FormatInt(int64(*options.TotalRecordCount), 10))
	}
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByBusinessCaseHandleResponse handles the ListByBusinessCase response.
func (client *EvaluatedMachinesOperationsClient) listByBusinessCaseHandleResponse(resp *http.Response) (EvaluatedMachinesOperationsClientListByBusinessCaseResponse, error) {
	result := EvaluatedMachinesOperationsClientListByBusinessCaseResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.EvaluatedMachineListResult); err != nil {
		return EvaluatedMachinesOperationsClientListByBusinessCaseResponse{}, err
	}
	return result, nil
}
