// 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/recoveryservices/armrecoveryservicessiterecovery/v2"
	"net/http"
	"net/url"
	"regexp"
)

// ReplicationStorageClassificationMappingsServer is a fake server for instances of the armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClient type.
type ReplicationStorageClassificationMappingsServer struct {
	// BeginCreate is the fake for method ReplicationStorageClassificationMappingsClient.BeginCreate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginCreate func(ctx context.Context, resourceGroupName string, resourceName string, fabricName string, storageClassificationName string, storageClassificationMappingName string, pairingInput armrecoveryservicessiterecovery.StorageClassificationMappingInput, options *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientBeginCreateOptions) (resp azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientCreateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method ReplicationStorageClassificationMappingsClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, resourceName string, fabricName string, storageClassificationName string, storageClassificationMappingName string, options *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientBeginDeleteOptions) (resp azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method ReplicationStorageClassificationMappingsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, resourceName string, fabricName string, storageClassificationName string, storageClassificationMappingName string, options *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientGetOptions) (resp azfake.Responder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListPager is the fake for method ReplicationStorageClassificationMappingsClient.NewListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListPager func(resourceGroupName string, resourceName string, options *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListOptions) (resp azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListResponse])

	// NewListByReplicationStorageClassificationsPager is the fake for method ReplicationStorageClassificationMappingsClient.NewListByReplicationStorageClassificationsPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByReplicationStorageClassificationsPager func(resourceGroupName string, resourceName string, fabricName string, storageClassificationName string, options *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListByReplicationStorageClassificationsOptions) (resp azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListByReplicationStorageClassificationsResponse])
}

// NewReplicationStorageClassificationMappingsServerTransport creates a new instance of ReplicationStorageClassificationMappingsServerTransport with the provided implementation.
// The returned ReplicationStorageClassificationMappingsServerTransport instance is connected to an instance of armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewReplicationStorageClassificationMappingsServerTransport(srv *ReplicationStorageClassificationMappingsServer) *ReplicationStorageClassificationMappingsServerTransport {
	return &ReplicationStorageClassificationMappingsServerTransport{
		srv:          srv,
		beginCreate:  newTracker[azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientCreateResponse]](),
		beginDelete:  newTracker[azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientDeleteResponse]](),
		newListPager: newTracker[azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListResponse]](),
		newListByReplicationStorageClassificationsPager: newTracker[azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListByReplicationStorageClassificationsResponse]](),
	}
}

// ReplicationStorageClassificationMappingsServerTransport connects instances of armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClient to instances of ReplicationStorageClassificationMappingsServer.
// Don't use this type directly, use NewReplicationStorageClassificationMappingsServerTransport instead.
type ReplicationStorageClassificationMappingsServerTransport struct {
	srv                                             *ReplicationStorageClassificationMappingsServer
	beginCreate                                     *tracker[azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientCreateResponse]]
	beginDelete                                     *tracker[azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientDeleteResponse]]
	newListPager                                    *tracker[azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListResponse]]
	newListByReplicationStorageClassificationsPager *tracker[azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListByReplicationStorageClassificationsResponse]]
}

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

func (r *ReplicationStorageClassificationMappingsServerTransport) 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 replicationStorageClassificationMappingsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = replicationStorageClassificationMappingsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "ReplicationStorageClassificationMappingsClient.BeginCreate":
				res.resp, res.err = r.dispatchBeginCreate(req)
			case "ReplicationStorageClassificationMappingsClient.BeginDelete":
				res.resp, res.err = r.dispatchBeginDelete(req)
			case "ReplicationStorageClassificationMappingsClient.Get":
				res.resp, res.err = r.dispatchGet(req)
			case "ReplicationStorageClassificationMappingsClient.NewListPager":
				res.resp, res.err = r.dispatchNewListPager(req)
			case "ReplicationStorageClassificationMappingsClient.NewListByReplicationStorageClassificationsPager":
				res.resp, res.err = r.dispatchNewListByReplicationStorageClassificationsPager(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 (r *ReplicationStorageClassificationMappingsServerTransport) dispatchBeginCreate(req *http.Request) (*http.Response, error) {
	if r.srv.BeginCreate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreate not implemented")}
	}
	beginCreate := r.beginCreate.get(req)
	if beginCreate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.RecoveryServices/vaults/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationFabrics/(?P<fabricName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassifications/(?P<storageClassificationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassificationMappings/(?P<storageClassificationMappingName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 6 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		body, err := server.UnmarshalRequestAsJSON[armrecoveryservicessiterecovery.StorageClassificationMappingInput](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
		if err != nil {
			return nil, err
		}
		fabricNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fabricName")])
		if err != nil {
			return nil, err
		}
		storageClassificationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationName")])
		if err != nil {
			return nil, err
		}
		storageClassificationMappingNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationMappingName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := r.srv.BeginCreate(req.Context(), resourceGroupNameParam, resourceNameParam, fabricNameParam, storageClassificationNameParam, storageClassificationMappingNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreate = &respr
		r.beginCreate.add(req, beginCreate)
	}

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

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

	return resp, nil
}

func (r *ReplicationStorageClassificationMappingsServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if r.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := r.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.RecoveryServices/vaults/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationFabrics/(?P<fabricName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassifications/(?P<storageClassificationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassificationMappings/(?P<storageClassificationMappingName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || 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
		}
		resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
		if err != nil {
			return nil, err
		}
		fabricNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fabricName")])
		if err != nil {
			return nil, err
		}
		storageClassificationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationName")])
		if err != nil {
			return nil, err
		}
		storageClassificationMappingNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationMappingName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := r.srv.BeginDelete(req.Context(), resourceGroupNameParam, resourceNameParam, fabricNameParam, storageClassificationNameParam, storageClassificationMappingNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		r.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) {
		r.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) {
		r.beginDelete.remove(req)
	}

	return resp, nil
}

func (r *ReplicationStorageClassificationMappingsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if r.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/Microsoft\.RecoveryServices/vaults/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationFabrics/(?P<fabricName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassifications/(?P<storageClassificationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassificationMappings/(?P<storageClassificationMappingName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || 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
	}
	resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
	if err != nil {
		return nil, err
	}
	fabricNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fabricName")])
	if err != nil {
		return nil, err
	}
	storageClassificationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationName")])
	if err != nil {
		return nil, err
	}
	storageClassificationMappingNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationMappingName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := r.srv.Get(req.Context(), resourceGroupNameParam, resourceNameParam, fabricNameParam, storageClassificationNameParam, storageClassificationMappingNameParam, 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).StorageClassificationMapping, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (r *ReplicationStorageClassificationMappingsServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
	if r.srv.NewListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
	}
	newListPager := r.newListPager.get(req)
	if newListPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.RecoveryServices/vaults/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassificationMappings`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 3 {
			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
		}
		resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
		if err != nil {
			return nil, err
		}
		resp := r.srv.NewListPager(resourceGroupNameParam, resourceNameParam, nil)
		newListPager = &resp
		r.newListPager.add(req, newListPager)
		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		r.newListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListPager) {
		r.newListPager.remove(req)
	}
	return resp, nil
}

func (r *ReplicationStorageClassificationMappingsServerTransport) dispatchNewListByReplicationStorageClassificationsPager(req *http.Request) (*http.Response, error) {
	if r.srv.NewListByReplicationStorageClassificationsPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByReplicationStorageClassificationsPager not implemented")}
	}
	newListByReplicationStorageClassificationsPager := r.newListByReplicationStorageClassificationsPager.get(req)
	if newListByReplicationStorageClassificationsPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.RecoveryServices/vaults/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationFabrics/(?P<fabricName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassifications/(?P<storageClassificationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassificationMappings`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || 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
		}
		resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
		if err != nil {
			return nil, err
		}
		fabricNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fabricName")])
		if err != nil {
			return nil, err
		}
		storageClassificationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationName")])
		if err != nil {
			return nil, err
		}
		resp := r.srv.NewListByReplicationStorageClassificationsPager(resourceGroupNameParam, resourceNameParam, fabricNameParam, storageClassificationNameParam, nil)
		newListByReplicationStorageClassificationsPager = &resp
		r.newListByReplicationStorageClassificationsPager.add(req, newListByReplicationStorageClassificationsPager)
		server.PagerResponderInjectNextLinks(newListByReplicationStorageClassificationsPager, req, func(page *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListByReplicationStorageClassificationsResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByReplicationStorageClassificationsPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		r.newListByReplicationStorageClassificationsPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByReplicationStorageClassificationsPager) {
		r.newListByReplicationStorageClassificationsPager.remove(req)
	}
	return resp, nil
}

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