//go:build go1.18
// +build go1.18

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

import (
	"context"
	"errors"
	"fmt"
	azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake/server"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/scvmm/armscvmm"
	"net/http"
	"net/url"
	"regexp"
)

// VMInstanceHybridIdentityMetadatasServer is a fake server for instances of the armscvmm.VMInstanceHybridIdentityMetadatasClient type.
type VMInstanceHybridIdentityMetadatasServer struct {
	// Get is the fake for method VMInstanceHybridIdentityMetadatasClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceURI string, options *armscvmm.VMInstanceHybridIdentityMetadatasClientGetOptions) (resp azfake.Responder[armscvmm.VMInstanceHybridIdentityMetadatasClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByVirtualMachineInstancePager is the fake for method VMInstanceHybridIdentityMetadatasClient.NewListByVirtualMachineInstancePager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByVirtualMachineInstancePager func(resourceURI string, options *armscvmm.VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceOptions) (resp azfake.PagerResponder[armscvmm.VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse])
}

// NewVMInstanceHybridIdentityMetadatasServerTransport creates a new instance of VMInstanceHybridIdentityMetadatasServerTransport with the provided implementation.
// The returned VMInstanceHybridIdentityMetadatasServerTransport instance is connected to an instance of armscvmm.VMInstanceHybridIdentityMetadatasClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewVMInstanceHybridIdentityMetadatasServerTransport(srv *VMInstanceHybridIdentityMetadatasServer) *VMInstanceHybridIdentityMetadatasServerTransport {
	return &VMInstanceHybridIdentityMetadatasServerTransport{
		srv:                                  srv,
		newListByVirtualMachineInstancePager: newTracker[azfake.PagerResponder[armscvmm.VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse]](),
	}
}

// VMInstanceHybridIdentityMetadatasServerTransport connects instances of armscvmm.VMInstanceHybridIdentityMetadatasClient to instances of VMInstanceHybridIdentityMetadatasServer.
// Don't use this type directly, use NewVMInstanceHybridIdentityMetadatasServerTransport instead.
type VMInstanceHybridIdentityMetadatasServerTransport struct {
	srv                                  *VMInstanceHybridIdentityMetadatasServer
	newListByVirtualMachineInstancePager *tracker[azfake.PagerResponder[armscvmm.VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse]]
}

// Do implements the policy.Transporter interface for VMInstanceHybridIdentityMetadatasServerTransport.
func (v *VMInstanceHybridIdentityMetadatasServerTransport) Do(req *http.Request) (*http.Response, error) {
	rawMethod := req.Context().Value(runtime.CtxAPINameKey{})
	method, ok := rawMethod.(string)
	if !ok {
		return nil, nonRetriableError{errors.New("unable to dispatch request, missing value for CtxAPINameKey")}
	}

	var resp *http.Response
	var err error

	switch method {
	case "VMInstanceHybridIdentityMetadatasClient.Get":
		resp, err = v.dispatchGet(req)
	case "VMInstanceHybridIdentityMetadatasClient.NewListByVirtualMachineInstancePager":
		resp, err = v.dispatchNewListByVirtualMachineInstancePager(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (v *VMInstanceHybridIdentityMetadatasServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if v.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/(?P<resourceUri>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ScVmm/virtualMachineInstances/default/hybridIdentityMetadata/default`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 1 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	resourceURIParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceUri")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := v.srv.Get(req.Context(), resourceURIParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).VMInstanceHybridIdentityMetadata, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (v *VMInstanceHybridIdentityMetadatasServerTransport) dispatchNewListByVirtualMachineInstancePager(req *http.Request) (*http.Response, error) {
	if v.srv.NewListByVirtualMachineInstancePager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByVirtualMachineInstancePager not implemented")}
	}
	newListByVirtualMachineInstancePager := v.newListByVirtualMachineInstancePager.get(req)
	if newListByVirtualMachineInstancePager == nil {
		const regexStr = `/(?P<resourceUri>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ScVmm/virtualMachineInstances/default/hybridIdentityMetadata`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 1 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		resourceURIParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceUri")])
		if err != nil {
			return nil, err
		}
		resp := v.srv.NewListByVirtualMachineInstancePager(resourceURIParam, nil)
		newListByVirtualMachineInstancePager = &resp
		v.newListByVirtualMachineInstancePager.add(req, newListByVirtualMachineInstancePager)
		server.PagerResponderInjectNextLinks(newListByVirtualMachineInstancePager, req, func(page *armscvmm.VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByVirtualMachineInstancePager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		v.newListByVirtualMachineInstancePager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByVirtualMachineInstancePager) {
		v.newListByVirtualMachineInstancePager.remove(req)
	}
	return resp, nil
}
