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

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

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

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

// Get - Get a SolutionTypeResource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-12-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - solutionType - Solution Type resource
//   - options - SolutionTypesClientGetOptions contains the optional parameters for the SolutionTypesClient.Get method.
func (client *SolutionTypesClient) Get(ctx context.Context, resourceGroupName string, solutionType string, options *SolutionTypesClientGetOptions) (SolutionTypesClientGetResponse, error) {
	var err error
	const operationName = "SolutionTypesClient.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, solutionType, options)
	if err != nil {
		return SolutionTypesClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SolutionTypesClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return SolutionTypesClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

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

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

// NewListByResourceGroupPager - List SolutionTypeResource resources by resource group
//
// Generated from API version 2024-12-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - options - SolutionTypesClientListByResourceGroupOptions contains the optional parameters for the SolutionTypesClient.NewListByResourceGroupPager
//     method.
func (client *SolutionTypesClient) NewListByResourceGroupPager(resourceGroupName string, options *SolutionTypesClientListByResourceGroupOptions) *runtime.Pager[SolutionTypesClientListByResourceGroupResponse] {
	return runtime.NewPager(runtime.PagingHandler[SolutionTypesClientListByResourceGroupResponse]{
		More: func(page SolutionTypesClientListByResourceGroupResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *SolutionTypesClientListByResourceGroupResponse) (SolutionTypesClientListByResourceGroupResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "SolutionTypesClient.NewListByResourceGroupPager")
			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.listByResourceGroupCreateRequest(ctx, resourceGroupName, options)
			}, nil)
			if err != nil {
				return SolutionTypesClientListByResourceGroupResponse{}, err
			}
			return client.listByResourceGroupHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByResourceGroupCreateRequest creates the ListByResourceGroup request.
func (client *SolutionTypesClient) listByResourceGroupCreateRequest(ctx context.Context, resourceGroupName string, _ *SolutionTypesClientListByResourceGroupOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.HybridConnectivity/solutionTypes"
	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))
	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-12-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByResourceGroupHandleResponse handles the ListByResourceGroup response.
func (client *SolutionTypesClient) listByResourceGroupHandleResponse(resp *http.Response) (SolutionTypesClientListByResourceGroupResponse, error) {
	result := SolutionTypesClientListByResourceGroupResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SolutionTypeResourceListResult); err != nil {
		return SolutionTypesClientListByResourceGroupResponse{}, err
	}
	return result, nil
}

// NewListBySubscriptionPager - List SolutionTypeResource resources by subscription ID
//
// Generated from API version 2024-12-01
//   - options - SolutionTypesClientListBySubscriptionOptions contains the optional parameters for the SolutionTypesClient.NewListBySubscriptionPager
//     method.
func (client *SolutionTypesClient) NewListBySubscriptionPager(options *SolutionTypesClientListBySubscriptionOptions) *runtime.Pager[SolutionTypesClientListBySubscriptionResponse] {
	return runtime.NewPager(runtime.PagingHandler[SolutionTypesClientListBySubscriptionResponse]{
		More: func(page SolutionTypesClientListBySubscriptionResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *SolutionTypesClientListBySubscriptionResponse) (SolutionTypesClientListBySubscriptionResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "SolutionTypesClient.NewListBySubscriptionPager")
			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.listBySubscriptionCreateRequest(ctx, options)
			}, nil)
			if err != nil {
				return SolutionTypesClientListBySubscriptionResponse{}, err
			}
			return client.listBySubscriptionHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listBySubscriptionCreateRequest creates the ListBySubscription request.
func (client *SolutionTypesClient) listBySubscriptionCreateRequest(ctx context.Context, _ *SolutionTypesClientListBySubscriptionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.HybridConnectivity/solutionTypes"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	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-12-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listBySubscriptionHandleResponse handles the ListBySubscription response.
func (client *SolutionTypesClient) listBySubscriptionHandleResponse(resp *http.Response) (SolutionTypesClientListBySubscriptionResponse, error) {
	result := SolutionTypesClientListBySubscriptionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SolutionTypeResourceListResult); err != nil {
		return SolutionTypesClientListBySubscriptionResponse{}, err
	}
	return result, nil
}
