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

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

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

	// NewListByAvsStorageContainerPager is the fake for method AvsStorageContainerVolumesClient.NewListByAvsStorageContainerPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByAvsStorageContainerPager func(resourceGroupName string, storagePoolName string, storageContainerName string, options *armpurestorageblock.AvsStorageContainerVolumesClientListByAvsStorageContainerOptions) (resp azfake.PagerResponder[armpurestorageblock.AvsStorageContainerVolumesClientListByAvsStorageContainerResponse])

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

// NewAvsStorageContainerVolumesServerTransport creates a new instance of AvsStorageContainerVolumesServerTransport with the provided implementation.
// The returned AvsStorageContainerVolumesServerTransport instance is connected to an instance of armpurestorageblock.AvsStorageContainerVolumesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewAvsStorageContainerVolumesServerTransport(srv *AvsStorageContainerVolumesServer) *AvsStorageContainerVolumesServerTransport {
	return &AvsStorageContainerVolumesServerTransport{
		srv:                               srv,
		beginDelete:                       newTracker[azfake.PollerResponder[armpurestorageblock.AvsStorageContainerVolumesClientDeleteResponse]](),
		newListByAvsStorageContainerPager: newTracker[azfake.PagerResponder[armpurestorageblock.AvsStorageContainerVolumesClientListByAvsStorageContainerResponse]](),
		beginUpdate:                       newTracker[azfake.PollerResponder[armpurestorageblock.AvsStorageContainerVolumesClientUpdateResponse]](),
	}
}

// AvsStorageContainerVolumesServerTransport connects instances of armpurestorageblock.AvsStorageContainerVolumesClient to instances of AvsStorageContainerVolumesServer.
// Don't use this type directly, use NewAvsStorageContainerVolumesServerTransport instead.
type AvsStorageContainerVolumesServerTransport struct {
	srv                               *AvsStorageContainerVolumesServer
	beginDelete                       *tracker[azfake.PollerResponder[armpurestorageblock.AvsStorageContainerVolumesClientDeleteResponse]]
	newListByAvsStorageContainerPager *tracker[azfake.PagerResponder[armpurestorageblock.AvsStorageContainerVolumesClientListByAvsStorageContainerResponse]]
	beginUpdate                       *tracker[azfake.PollerResponder[armpurestorageblock.AvsStorageContainerVolumesClientUpdateResponse]]
}

// Do implements the policy.Transporter interface for AvsStorageContainerVolumesServerTransport.
func (a *AvsStorageContainerVolumesServerTransport) 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 *AvsStorageContainerVolumesServerTransport) 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 avsStorageContainerVolumesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = avsStorageContainerVolumesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "AvsStorageContainerVolumesClient.BeginDelete":
				res.resp, res.err = a.dispatchBeginDelete(req)
			case "AvsStorageContainerVolumesClient.Get":
				res.resp, res.err = a.dispatchGet(req)
			case "AvsStorageContainerVolumesClient.NewListByAvsStorageContainerPager":
				res.resp, res.err = a.dispatchNewListByAvsStorageContainerPager(req)
			case "AvsStorageContainerVolumesClient.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 *AvsStorageContainerVolumesServerTransport) 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~%@]+)/avsStorageContainers/(?P<storageContainerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/volumes/(?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
		}
		storageContainerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageContainerName")])
		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, storageContainerNameParam, 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 *AvsStorageContainerVolumesServerTransport) 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~%@]+)/avsStorageContainers/(?P<storageContainerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/volumes/(?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
	}
	storageContainerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageContainerName")])
	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, storageContainerNameParam, 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).AvsStorageContainerVolume, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (a *AvsStorageContainerVolumesServerTransport) dispatchNewListByAvsStorageContainerPager(req *http.Request) (*http.Response, error) {
	if a.srv.NewListByAvsStorageContainerPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByAvsStorageContainerPager not implemented")}
	}
	newListByAvsStorageContainerPager := a.newListByAvsStorageContainerPager.get(req)
	if newListByAvsStorageContainerPager == 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~%@]+)/avsStorageContainers/(?P<storageContainerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/volumes`
		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
		}
		storageContainerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageContainerName")])
		if err != nil {
			return nil, err
		}
		resp := a.srv.NewListByAvsStorageContainerPager(resourceGroupNameParam, storagePoolNameParam, storageContainerNameParam, nil)
		newListByAvsStorageContainerPager = &resp
		a.newListByAvsStorageContainerPager.add(req, newListByAvsStorageContainerPager)
		server.PagerResponderInjectNextLinks(newListByAvsStorageContainerPager, req, func(page *armpurestorageblock.AvsStorageContainerVolumesClientListByAvsStorageContainerResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByAvsStorageContainerPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		a.newListByAvsStorageContainerPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByAvsStorageContainerPager) {
		a.newListByAvsStorageContainerPager.remove(req)
	}
	return resp, nil
}

func (a *AvsStorageContainerVolumesServerTransport) 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~%@]+)/avsStorageContainers/(?P<storageContainerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/volumes/(?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.AvsStorageContainerVolumeUpdate](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
		}
		storageContainerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageContainerName")])
		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, storageContainerNameParam, 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 AvsStorageContainerVolumesServerTransport
var avsStorageContainerVolumesServerTransportInterceptor interface {
	// Do returns true if the server transport should use the returned response/error
	Do(*http.Request) (*http.Response, error, bool)
}
