// 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/purestorageblock/armpurestorageblock"
	"net/http"
	"net/url"
	"regexp"
)

// AvsVMVolumesServer is a fake server for instances of the armpurestorageblock.AvsVMVolumesClient type.
type AvsVMVolumesServer struct {
	// BeginDelete is the fake for method AvsVMVolumesClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, storagePoolName string, avsVMID string, volumeID string, options *armpurestorageblock.AvsVMVolumesClientBeginDeleteOptions) (resp azfake.PollerResponder[armpurestorageblock.AvsVMVolumesClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method AvsVMVolumesClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, storagePoolName string, avsVMID string, volumeID string, options *armpurestorageblock.AvsVMVolumesClientGetOptions) (resp azfake.Responder[armpurestorageblock.AvsVMVolumesClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByAvsVMPager is the fake for method AvsVMVolumesClient.NewListByAvsVMPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByAvsVMPager func(resourceGroupName string, storagePoolName string, avsVMID string, options *armpurestorageblock.AvsVMVolumesClientListByAvsVMOptions) (resp azfake.PagerResponder[armpurestorageblock.AvsVMVolumesClientListByAvsVMResponse])

	// BeginUpdate is the fake for method AvsVMVolumesClient.BeginUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginUpdate func(ctx context.Context, resourceGroupName string, storagePoolName string, avsVMID string, volumeID string, properties armpurestorageblock.AvsVMVolumeUpdate, options *armpurestorageblock.AvsVMVolumesClientBeginUpdateOptions) (resp azfake.PollerResponder[armpurestorageblock.AvsVMVolumesClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewAvsVMVolumesServerTransport creates a new instance of AvsVMVolumesServerTransport with the provided implementation.
// The returned AvsVMVolumesServerTransport instance is connected to an instance of armpurestorageblock.AvsVMVolumesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewAvsVMVolumesServerTransport(srv *AvsVMVolumesServer) *AvsVMVolumesServerTransport {
	return &AvsVMVolumesServerTransport{
		srv:                 srv,
		beginDelete:         newTracker[azfake.PollerResponder[armpurestorageblock.AvsVMVolumesClientDeleteResponse]](),
		newListByAvsVMPager: newTracker[azfake.PagerResponder[armpurestorageblock.AvsVMVolumesClientListByAvsVMResponse]](),
		beginUpdate:         newTracker[azfake.PollerResponder[armpurestorageblock.AvsVMVolumesClientUpdateResponse]](),
	}
}

// AvsVMVolumesServerTransport connects instances of armpurestorageblock.AvsVMVolumesClient to instances of AvsVMVolumesServer.
// Don't use this type directly, use NewAvsVMVolumesServerTransport instead.
type AvsVMVolumesServerTransport struct {
	srv                 *AvsVMVolumesServer
	beginDelete         *tracker[azfake.PollerResponder[armpurestorageblock.AvsVMVolumesClientDeleteResponse]]
	newListByAvsVMPager *tracker[azfake.PagerResponder[armpurestorageblock.AvsVMVolumesClientListByAvsVMResponse]]
	beginUpdate         *tracker[azfake.PollerResponder[armpurestorageblock.AvsVMVolumesClientUpdateResponse]]
}

// Do implements the policy.Transporter interface for AvsVMVolumesServerTransport.
func (a *AvsVMVolumesServerTransport) 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 a.dispatchToMethodFake(req, method)
}

func (a *AvsVMVolumesServerTransport) 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 avsVMVolumesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = avsVMVolumesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "AvsVMVolumesClient.BeginDelete":
				res.resp, res.err = a.dispatchBeginDelete(req)
			case "AvsVMVolumesClient.Get":
				res.resp, res.err = a.dispatchGet(req)
			case "AvsVMVolumesClient.NewListByAvsVMPager":
				res.resp, res.err = a.dispatchNewListByAvsVMPager(req)
			case "AvsVMVolumesClient.BeginUpdate":
				res.resp, res.err = a.dispatchBeginUpdate(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 (a *AvsVMVolumesServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if a.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := a.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/PureStorage\.Block/storagePools/(?P<storagePoolName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/avsVms/(?P<avsVmId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/avsVmVolumes/(?P<volumeId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 6 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		storagePoolNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storagePoolName")])
		if err != nil {
			return nil, err
		}
		avsVMIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("avsVmId")])
		if err != nil {
			return nil, err
		}
		volumeIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("volumeId")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := a.srv.BeginDelete(req.Context(), resourceGroupNameParam, storagePoolNameParam, avsVMIDParam, volumeIDParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		a.beginDelete.add(req, beginDelete)
	}

	resp, err := server.PollerResponderNext(beginDelete, req)
	if err != nil {
		return nil, err
	}

	if !contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		a.beginDelete.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted, http.StatusNoContent", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginDelete) {
		a.beginDelete.remove(req)
	}

	return resp, nil
}

func (a *AvsVMVolumesServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if a.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/PureStorage\.Block/storagePools/(?P<storagePoolName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/avsVms/(?P<avsVmId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/avsVmVolumes/(?P<volumeId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 6 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	storagePoolNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storagePoolName")])
	if err != nil {
		return nil, err
	}
	avsVMIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("avsVmId")])
	if err != nil {
		return nil, err
	}
	volumeIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("volumeId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := a.srv.Get(req.Context(), resourceGroupNameParam, storagePoolNameParam, avsVMIDParam, volumeIDParam, 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).AvsVMVolume, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (a *AvsVMVolumesServerTransport) dispatchNewListByAvsVMPager(req *http.Request) (*http.Response, error) {
	if a.srv.NewListByAvsVMPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByAvsVMPager not implemented")}
	}
	newListByAvsVMPager := a.newListByAvsVMPager.get(req)
	if newListByAvsVMPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/PureStorage\.Block/storagePools/(?P<storagePoolName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/avsVms/(?P<avsVmId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/avsVmVolumes`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 5 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		storagePoolNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storagePoolName")])
		if err != nil {
			return nil, err
		}
		avsVMIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("avsVmId")])
		if err != nil {
			return nil, err
		}
		resp := a.srv.NewListByAvsVMPager(resourceGroupNameParam, storagePoolNameParam, avsVMIDParam, nil)
		newListByAvsVMPager = &resp
		a.newListByAvsVMPager.add(req, newListByAvsVMPager)
		server.PagerResponderInjectNextLinks(newListByAvsVMPager, req, func(page *armpurestorageblock.AvsVMVolumesClientListByAvsVMResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByAvsVMPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		a.newListByAvsVMPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByAvsVMPager) {
		a.newListByAvsVMPager.remove(req)
	}
	return resp, nil
}

func (a *AvsVMVolumesServerTransport) dispatchBeginUpdate(req *http.Request) (*http.Response, error) {
	if a.srv.BeginUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginUpdate not implemented")}
	}
	beginUpdate := a.beginUpdate.get(req)
	if beginUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/PureStorage\.Block/storagePools/(?P<storagePoolName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/avsVms/(?P<avsVmId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/avsVmVolumes/(?P<volumeId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 6 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		body, err := server.UnmarshalRequestAsJSON[armpurestorageblock.AvsVMVolumeUpdate](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		storagePoolNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storagePoolName")])
		if err != nil {
			return nil, err
		}
		avsVMIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("avsVmId")])
		if err != nil {
			return nil, err
		}
		volumeIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("volumeId")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := a.srv.BeginUpdate(req.Context(), resourceGroupNameParam, storagePoolNameParam, avsVMIDParam, volumeIDParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginUpdate = &respr
		a.beginUpdate.add(req, beginUpdate)
	}

	resp, err := server.PollerResponderNext(beginUpdate, req)
	if err != nil {
		return nil, err
	}

	if !contains([]int{http.StatusOK, http.StatusAccepted}, resp.StatusCode) {
		a.beginUpdate.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginUpdate) {
		a.beginUpdate.remove(req)
	}

	return resp, nil
}

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