// 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 armstandbypool

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

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

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

// Get - Get a StandbyVirtualMachinePoolRuntimeViewResource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-03-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - standbyVirtualMachinePoolName - Name of the standby virtual machine pool
//   - runtimeView - The unique identifier for the runtime view. The input string should be the word 'latest', which will get
//     the latest runtime view of the pool, otherwise the request will fail with NotFound exception.
//   - options - StandbyVirtualMachinePoolRuntimeViewsClientGetOptions contains the optional parameters for the StandbyVirtualMachinePoolRuntimeViewsClient.Get
//     method.
func (client *StandbyVirtualMachinePoolRuntimeViewsClient) Get(ctx context.Context, resourceGroupName string, standbyVirtualMachinePoolName string, runtimeView string, options *StandbyVirtualMachinePoolRuntimeViewsClientGetOptions) (StandbyVirtualMachinePoolRuntimeViewsClientGetResponse, error) {
	var err error
	const operationName = "StandbyVirtualMachinePoolRuntimeViewsClient.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, standbyVirtualMachinePoolName, runtimeView, options)
	if err != nil {
		return StandbyVirtualMachinePoolRuntimeViewsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return StandbyVirtualMachinePoolRuntimeViewsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return StandbyVirtualMachinePoolRuntimeViewsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *StandbyVirtualMachinePoolRuntimeViewsClient) getCreateRequest(ctx context.Context, resourceGroupName string, standbyVirtualMachinePoolName string, runtimeView string, _ *StandbyVirtualMachinePoolRuntimeViewsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.StandbyPool/standbyVirtualMachinePools/{standbyVirtualMachinePoolName}/runtimeViews/{runtimeView}"
	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 standbyVirtualMachinePoolName == "" {
		return nil, errors.New("parameter standbyVirtualMachinePoolName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{standbyVirtualMachinePoolName}", url.PathEscape(standbyVirtualMachinePoolName))
	if runtimeView == "" {
		return nil, errors.New("parameter runtimeView cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{runtimeView}", url.PathEscape(runtimeView))
	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", "2025-03-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByStandbyPoolPager - List StandbyVirtualMachinePoolRuntimeViewResource resources by StandbyVirtualMachinePoolResource
//
// Generated from API version 2025-03-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - standbyVirtualMachinePoolName - Name of the standby virtual machine pool
//   - options - StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolOptions contains the optional parameters for the
//     StandbyVirtualMachinePoolRuntimeViewsClient.NewListByStandbyPoolPager method.
func (client *StandbyVirtualMachinePoolRuntimeViewsClient) NewListByStandbyPoolPager(resourceGroupName string, standbyVirtualMachinePoolName string, options *StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolOptions) *runtime.Pager[StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolResponse] {
	return runtime.NewPager(runtime.PagingHandler[StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolResponse]{
		More: func(page StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolResponse) (StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "StandbyVirtualMachinePoolRuntimeViewsClient.NewListByStandbyPoolPager")
			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.listByStandbyPoolCreateRequest(ctx, resourceGroupName, standbyVirtualMachinePoolName, options)
			}, nil)
			if err != nil {
				return StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolResponse{}, err
			}
			return client.listByStandbyPoolHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByStandbyPoolCreateRequest creates the ListByStandbyPool request.
func (client *StandbyVirtualMachinePoolRuntimeViewsClient) listByStandbyPoolCreateRequest(ctx context.Context, resourceGroupName string, standbyVirtualMachinePoolName string, _ *StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.StandbyPool/standbyVirtualMachinePools/{standbyVirtualMachinePoolName}/runtimeViews"
	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 standbyVirtualMachinePoolName == "" {
		return nil, errors.New("parameter standbyVirtualMachinePoolName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{standbyVirtualMachinePoolName}", url.PathEscape(standbyVirtualMachinePoolName))
	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", "2025-03-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByStandbyPoolHandleResponse handles the ListByStandbyPool response.
func (client *StandbyVirtualMachinePoolRuntimeViewsClient) listByStandbyPoolHandleResponse(resp *http.Response) (StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolResponse, error) {
	result := StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.StandbyVirtualMachinePoolRuntimeViewResourceListResult); err != nil {
		return StandbyVirtualMachinePoolRuntimeViewsClientListByStandbyPoolResponse{}, err
	}
	return result, nil
}
