// 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/workloadorchestration/armworkloadorchestration"
	"net/http"
	"net/url"
	"regexp"
)

// DynamicSchemaVersionsServer is a fake server for instances of the armworkloadorchestration.DynamicSchemaVersionsClient type.
type DynamicSchemaVersionsServer struct {
	// BeginCreateOrUpdate is the fake for method DynamicSchemaVersionsClient.BeginCreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, schemaName string, dynamicSchemaName string, dynamicSchemaVersionName string, resource armworkloadorchestration.DynamicSchemaVersion, options *armworkloadorchestration.DynamicSchemaVersionsClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armworkloadorchestration.DynamicSchemaVersionsClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method DynamicSchemaVersionsClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, schemaName string, dynamicSchemaName string, dynamicSchemaVersionName string, options *armworkloadorchestration.DynamicSchemaVersionsClientBeginDeleteOptions) (resp azfake.PollerResponder[armworkloadorchestration.DynamicSchemaVersionsClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method DynamicSchemaVersionsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, schemaName string, dynamicSchemaName string, dynamicSchemaVersionName string, options *armworkloadorchestration.DynamicSchemaVersionsClientGetOptions) (resp azfake.Responder[armworkloadorchestration.DynamicSchemaVersionsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByDynamicSchemaPager is the fake for method DynamicSchemaVersionsClient.NewListByDynamicSchemaPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByDynamicSchemaPager func(resourceGroupName string, schemaName string, dynamicSchemaName string, options *armworkloadorchestration.DynamicSchemaVersionsClientListByDynamicSchemaOptions) (resp azfake.PagerResponder[armworkloadorchestration.DynamicSchemaVersionsClientListByDynamicSchemaResponse])

	// Update is the fake for method DynamicSchemaVersionsClient.Update
	// HTTP status codes to indicate success: http.StatusOK
	Update func(ctx context.Context, resourceGroupName string, schemaName string, dynamicSchemaName string, dynamicSchemaVersionName string, properties armworkloadorchestration.DynamicSchemaVersion, options *armworkloadorchestration.DynamicSchemaVersionsClientUpdateOptions) (resp azfake.Responder[armworkloadorchestration.DynamicSchemaVersionsClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewDynamicSchemaVersionsServerTransport creates a new instance of DynamicSchemaVersionsServerTransport with the provided implementation.
// The returned DynamicSchemaVersionsServerTransport instance is connected to an instance of armworkloadorchestration.DynamicSchemaVersionsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewDynamicSchemaVersionsServerTransport(srv *DynamicSchemaVersionsServer) *DynamicSchemaVersionsServerTransport {
	return &DynamicSchemaVersionsServerTransport{
		srv:                         srv,
		beginCreateOrUpdate:         newTracker[azfake.PollerResponder[armworkloadorchestration.DynamicSchemaVersionsClientCreateOrUpdateResponse]](),
		beginDelete:                 newTracker[azfake.PollerResponder[armworkloadorchestration.DynamicSchemaVersionsClientDeleteResponse]](),
		newListByDynamicSchemaPager: newTracker[azfake.PagerResponder[armworkloadorchestration.DynamicSchemaVersionsClientListByDynamicSchemaResponse]](),
	}
}

// DynamicSchemaVersionsServerTransport connects instances of armworkloadorchestration.DynamicSchemaVersionsClient to instances of DynamicSchemaVersionsServer.
// Don't use this type directly, use NewDynamicSchemaVersionsServerTransport instead.
type DynamicSchemaVersionsServerTransport struct {
	srv                         *DynamicSchemaVersionsServer
	beginCreateOrUpdate         *tracker[azfake.PollerResponder[armworkloadorchestration.DynamicSchemaVersionsClientCreateOrUpdateResponse]]
	beginDelete                 *tracker[azfake.PollerResponder[armworkloadorchestration.DynamicSchemaVersionsClientDeleteResponse]]
	newListByDynamicSchemaPager *tracker[azfake.PagerResponder[armworkloadorchestration.DynamicSchemaVersionsClientListByDynamicSchemaResponse]]
}

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

func (d *DynamicSchemaVersionsServerTransport) 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 dynamicSchemaVersionsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = dynamicSchemaVersionsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "DynamicSchemaVersionsClient.BeginCreateOrUpdate":
				res.resp, res.err = d.dispatchBeginCreateOrUpdate(req)
			case "DynamicSchemaVersionsClient.BeginDelete":
				res.resp, res.err = d.dispatchBeginDelete(req)
			case "DynamicSchemaVersionsClient.Get":
				res.resp, res.err = d.dispatchGet(req)
			case "DynamicSchemaVersionsClient.NewListByDynamicSchemaPager":
				res.resp, res.err = d.dispatchNewListByDynamicSchemaPager(req)
			case "DynamicSchemaVersionsClient.Update":
				res.resp, res.err = d.dispatchUpdate(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 (d *DynamicSchemaVersionsServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if d.srv.BeginCreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")}
	}
	beginCreateOrUpdate := d.beginCreateOrUpdate.get(req)
	if beginCreateOrUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/schemas/(?P<schemaName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/dynamicSchemas/(?P<dynamicSchemaName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<dynamicSchemaVersionName>[!#&$-;=?-\[\]_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[armworkloadorchestration.DynamicSchemaVersion](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		schemaNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("schemaName")])
		if err != nil {
			return nil, err
		}
		dynamicSchemaNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dynamicSchemaName")])
		if err != nil {
			return nil, err
		}
		dynamicSchemaVersionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dynamicSchemaVersionName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := d.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, schemaNameParam, dynamicSchemaNameParam, dynamicSchemaVersionNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOrUpdate = &respr
		d.beginCreateOrUpdate.add(req, beginCreateOrUpdate)
	}

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

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

	return resp, nil
}

func (d *DynamicSchemaVersionsServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if d.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := d.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/schemas/(?P<schemaName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/dynamicSchemas/(?P<dynamicSchemaName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<dynamicSchemaVersionName>[!#&$-;=?-\[\]_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
		}
		schemaNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("schemaName")])
		if err != nil {
			return nil, err
		}
		dynamicSchemaNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dynamicSchemaName")])
		if err != nil {
			return nil, err
		}
		dynamicSchemaVersionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dynamicSchemaVersionName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := d.srv.BeginDelete(req.Context(), resourceGroupNameParam, schemaNameParam, dynamicSchemaNameParam, dynamicSchemaVersionNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		d.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) {
		d.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) {
		d.beginDelete.remove(req)
	}

	return resp, nil
}

func (d *DynamicSchemaVersionsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if d.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\.Edge/schemas/(?P<schemaName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/dynamicSchemas/(?P<dynamicSchemaName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<dynamicSchemaVersionName>[!#&$-;=?-\[\]_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
	}
	schemaNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("schemaName")])
	if err != nil {
		return nil, err
	}
	dynamicSchemaNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dynamicSchemaName")])
	if err != nil {
		return nil, err
	}
	dynamicSchemaVersionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dynamicSchemaVersionName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := d.srv.Get(req.Context(), resourceGroupNameParam, schemaNameParam, dynamicSchemaNameParam, dynamicSchemaVersionNameParam, 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).DynamicSchemaVersion, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (d *DynamicSchemaVersionsServerTransport) dispatchNewListByDynamicSchemaPager(req *http.Request) (*http.Response, error) {
	if d.srv.NewListByDynamicSchemaPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByDynamicSchemaPager not implemented")}
	}
	newListByDynamicSchemaPager := d.newListByDynamicSchemaPager.get(req)
	if newListByDynamicSchemaPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/schemas/(?P<schemaName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/dynamicSchemas/(?P<dynamicSchemaName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions`
		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
		}
		schemaNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("schemaName")])
		if err != nil {
			return nil, err
		}
		dynamicSchemaNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dynamicSchemaName")])
		if err != nil {
			return nil, err
		}
		resp := d.srv.NewListByDynamicSchemaPager(resourceGroupNameParam, schemaNameParam, dynamicSchemaNameParam, nil)
		newListByDynamicSchemaPager = &resp
		d.newListByDynamicSchemaPager.add(req, newListByDynamicSchemaPager)
		server.PagerResponderInjectNextLinks(newListByDynamicSchemaPager, req, func(page *armworkloadorchestration.DynamicSchemaVersionsClientListByDynamicSchemaResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByDynamicSchemaPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		d.newListByDynamicSchemaPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByDynamicSchemaPager) {
		d.newListByDynamicSchemaPager.remove(req)
	}
	return resp, nil
}

func (d *DynamicSchemaVersionsServerTransport) dispatchUpdate(req *http.Request) (*http.Response, error) {
	if d.srv.Update == nil {
		return nil, &nonRetriableError{errors.New("fake for method Update not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/schemas/(?P<schemaName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/dynamicSchemas/(?P<dynamicSchemaName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<dynamicSchemaVersionName>[!#&$-;=?-\[\]_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[armworkloadorchestration.DynamicSchemaVersion](req)
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	schemaNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("schemaName")])
	if err != nil {
		return nil, err
	}
	dynamicSchemaNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dynamicSchemaName")])
	if err != nil {
		return nil, err
	}
	dynamicSchemaVersionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dynamicSchemaVersionName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := d.srv.Update(req.Context(), resourceGroupNameParam, schemaNameParam, dynamicSchemaNameParam, dynamicSchemaVersionNameParam, body, 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).DynamicSchemaVersion, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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