// 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 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/azurestackhci/armazurestackhcivm"
	"net/http"
	"net/url"
	"regexp"
)

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

	// NewListByVirtualMachineInstancePager is the fake for method HybridIdentityMetadataClient.NewListByVirtualMachineInstancePager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByVirtualMachineInstancePager func(resourceURI string, options *armazurestackhcivm.HybridIdentityMetadataClientListByVirtualMachineInstanceOptions) (resp azfake.PagerResponder[armazurestackhcivm.HybridIdentityMetadataClientListByVirtualMachineInstanceResponse])
}

// NewHybridIdentityMetadataServerTransport creates a new instance of HybridIdentityMetadataServerTransport with the provided implementation.
// The returned HybridIdentityMetadataServerTransport instance is connected to an instance of armazurestackhcivm.HybridIdentityMetadataClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewHybridIdentityMetadataServerTransport(srv *HybridIdentityMetadataServer) *HybridIdentityMetadataServerTransport {
	return &HybridIdentityMetadataServerTransport{
		srv:                                  srv,
		newListByVirtualMachineInstancePager: newTracker[azfake.PagerResponder[armazurestackhcivm.HybridIdentityMetadataClientListByVirtualMachineInstanceResponse]](),
	}
}

// HybridIdentityMetadataServerTransport connects instances of armazurestackhcivm.HybridIdentityMetadataClient to instances of HybridIdentityMetadataServer.
// Don't use this type directly, use NewHybridIdentityMetadataServerTransport instead.
type HybridIdentityMetadataServerTransport struct {
	srv                                  *HybridIdentityMetadataServer
	newListByVirtualMachineInstancePager *tracker[azfake.PagerResponder[armazurestackhcivm.HybridIdentityMetadataClientListByVirtualMachineInstanceResponse]]
}

// Do implements the policy.Transporter interface for HybridIdentityMetadataServerTransport.
func (h *HybridIdentityMetadataServerTransport) 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")}
	}

	return h.dispatchToMethodFake(req, method)
}

func (h *HybridIdentityMetadataServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result)
	defer close(resultChan)

	go func() {
		var intercepted bool
		var res result
		if hybridIdentityMetadataServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = hybridIdentityMetadataServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "HybridIdentityMetadataClient.Get":
				res.resp, res.err = h.dispatchGet(req)
			case "HybridIdentityMetadataClient.NewListByVirtualMachineInstancePager":
				res.resp, res.err = h.dispatchNewListByVirtualMachineInstancePager(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		select {
		case resultChan <- res:
		case <-req.Context().Done():
		}
	}()

	select {
	case <-req.Context().Done():
		return nil, req.Context().Err()
	case res := <-resultChan:
		return res.resp, res.err
	}
}

func (h *HybridIdentityMetadataServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if h.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/(?P<resourceUri>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.AzureStackHCI/virtualMachineInstances/default/hybridIdentityMetadata/default`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 2 {
		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 := h.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).HybridIdentityMetadata, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (h *HybridIdentityMetadataServerTransport) dispatchNewListByVirtualMachineInstancePager(req *http.Request) (*http.Response, error) {
	if h.srv.NewListByVirtualMachineInstancePager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByVirtualMachineInstancePager not implemented")}
	}
	newListByVirtualMachineInstancePager := h.newListByVirtualMachineInstancePager.get(req)
	if newListByVirtualMachineInstancePager == nil {
		const regexStr = `/(?P<resourceUri>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.AzureStackHCI/virtualMachineInstances/default/hybridIdentityMetadata`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 2 {
			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 := h.srv.NewListByVirtualMachineInstancePager(resourceURIParam, nil)
		newListByVirtualMachineInstancePager = &resp
		h.newListByVirtualMachineInstancePager.add(req, newListByVirtualMachineInstancePager)
		server.PagerResponderInjectNextLinks(newListByVirtualMachineInstancePager, req, func(page *armazurestackhcivm.HybridIdentityMetadataClientListByVirtualMachineInstanceResponse, 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) {
		h.newListByVirtualMachineInstancePager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByVirtualMachineInstancePager) {
		h.newListByVirtualMachineInstancePager.remove(req)
	}
	return resp, nil
}

// set this to conditionally intercept incoming requests to HybridIdentityMetadataServerTransport
var hybridIdentityMetadataServerTransportInterceptor interface {
	// Do returns true if the server transport should use the returned response/error
	Do(*http.Request) (*http.Response, error, bool)
}
