// 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/deviceregistry/armdeviceregistry/v2"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// SchemaRegistriesServer is a fake server for instances of the armdeviceregistry.SchemaRegistriesClient type.
type SchemaRegistriesServer struct {
	// BeginCreateOrReplace is the fake for method SchemaRegistriesClient.BeginCreateOrReplace
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrReplace func(ctx context.Context, resourceGroupName string, schemaRegistryName string, resource armdeviceregistry.SchemaRegistry, options *armdeviceregistry.SchemaRegistriesClientBeginCreateOrReplaceOptions) (resp azfake.PollerResponder[armdeviceregistry.SchemaRegistriesClientCreateOrReplaceResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method SchemaRegistriesClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, schemaRegistryName string, options *armdeviceregistry.SchemaRegistriesClientBeginDeleteOptions) (resp azfake.PollerResponder[armdeviceregistry.SchemaRegistriesClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method SchemaRegistriesClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, schemaRegistryName string, options *armdeviceregistry.SchemaRegistriesClientGetOptions) (resp azfake.Responder[armdeviceregistry.SchemaRegistriesClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByResourceGroupPager is the fake for method SchemaRegistriesClient.NewListByResourceGroupPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByResourceGroupPager func(resourceGroupName string, options *armdeviceregistry.SchemaRegistriesClientListByResourceGroupOptions) (resp azfake.PagerResponder[armdeviceregistry.SchemaRegistriesClientListByResourceGroupResponse])

	// NewListBySubscriptionPager is the fake for method SchemaRegistriesClient.NewListBySubscriptionPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListBySubscriptionPager func(options *armdeviceregistry.SchemaRegistriesClientListBySubscriptionOptions) (resp azfake.PagerResponder[armdeviceregistry.SchemaRegistriesClientListBySubscriptionResponse])

	// BeginUpdate is the fake for method SchemaRegistriesClient.BeginUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginUpdate func(ctx context.Context, resourceGroupName string, schemaRegistryName string, properties armdeviceregistry.SchemaRegistryUpdate, options *armdeviceregistry.SchemaRegistriesClientBeginUpdateOptions) (resp azfake.PollerResponder[armdeviceregistry.SchemaRegistriesClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewSchemaRegistriesServerTransport creates a new instance of SchemaRegistriesServerTransport with the provided implementation.
// The returned SchemaRegistriesServerTransport instance is connected to an instance of armdeviceregistry.SchemaRegistriesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewSchemaRegistriesServerTransport(srv *SchemaRegistriesServer) *SchemaRegistriesServerTransport {
	return &SchemaRegistriesServerTransport{
		srv:                         srv,
		beginCreateOrReplace:        newTracker[azfake.PollerResponder[armdeviceregistry.SchemaRegistriesClientCreateOrReplaceResponse]](),
		beginDelete:                 newTracker[azfake.PollerResponder[armdeviceregistry.SchemaRegistriesClientDeleteResponse]](),
		newListByResourceGroupPager: newTracker[azfake.PagerResponder[armdeviceregistry.SchemaRegistriesClientListByResourceGroupResponse]](),
		newListBySubscriptionPager:  newTracker[azfake.PagerResponder[armdeviceregistry.SchemaRegistriesClientListBySubscriptionResponse]](),
		beginUpdate:                 newTracker[azfake.PollerResponder[armdeviceregistry.SchemaRegistriesClientUpdateResponse]](),
	}
}

// SchemaRegistriesServerTransport connects instances of armdeviceregistry.SchemaRegistriesClient to instances of SchemaRegistriesServer.
// Don't use this type directly, use NewSchemaRegistriesServerTransport instead.
type SchemaRegistriesServerTransport struct {
	srv                         *SchemaRegistriesServer
	beginCreateOrReplace        *tracker[azfake.PollerResponder[armdeviceregistry.SchemaRegistriesClientCreateOrReplaceResponse]]
	beginDelete                 *tracker[azfake.PollerResponder[armdeviceregistry.SchemaRegistriesClientDeleteResponse]]
	newListByResourceGroupPager *tracker[azfake.PagerResponder[armdeviceregistry.SchemaRegistriesClientListByResourceGroupResponse]]
	newListBySubscriptionPager  *tracker[azfake.PagerResponder[armdeviceregistry.SchemaRegistriesClientListBySubscriptionResponse]]
	beginUpdate                 *tracker[azfake.PollerResponder[armdeviceregistry.SchemaRegistriesClientUpdateResponse]]
}

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

func (s *SchemaRegistriesServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if schemaRegistriesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = schemaRegistriesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "SchemaRegistriesClient.BeginCreateOrReplace":
				res.resp, res.err = s.dispatchBeginCreateOrReplace(req)
			case "SchemaRegistriesClient.BeginDelete":
				res.resp, res.err = s.dispatchBeginDelete(req)
			case "SchemaRegistriesClient.Get":
				res.resp, res.err = s.dispatchGet(req)
			case "SchemaRegistriesClient.NewListByResourceGroupPager":
				res.resp, res.err = s.dispatchNewListByResourceGroupPager(req)
			case "SchemaRegistriesClient.NewListBySubscriptionPager":
				res.resp, res.err = s.dispatchNewListBySubscriptionPager(req)
			case "SchemaRegistriesClient.BeginUpdate":
				res.resp, res.err = s.dispatchBeginUpdate(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		resultChan <- res
	}()

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

func (s *SchemaRegistriesServerTransport) dispatchBeginCreateOrReplace(req *http.Request) (*http.Response, error) {
	if s.srv.BeginCreateOrReplace == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrReplace not implemented")}
	}
	beginCreateOrReplace := s.beginCreateOrReplace.get(req)
	if beginCreateOrReplace == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.DeviceRegistry/schemaRegistries/(?P<schemaRegistryName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 4 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		body, err := server.UnmarshalRequestAsJSON[armdeviceregistry.SchemaRegistry](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		schemaRegistryNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("schemaRegistryName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := s.srv.BeginCreateOrReplace(req.Context(), resourceGroupNameParam, schemaRegistryNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOrReplace = &respr
		s.beginCreateOrReplace.add(req, beginCreateOrReplace)
	}

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

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

	return resp, nil
}

func (s *SchemaRegistriesServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if s.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := s.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.DeviceRegistry/schemaRegistries/(?P<schemaRegistryName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 4 {
			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
		}
		schemaRegistryNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("schemaRegistryName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := s.srv.BeginDelete(req.Context(), resourceGroupNameParam, schemaRegistryNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		s.beginDelete.add(req, beginDelete)
	}

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

	if !slices.Contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		s.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) {
		s.beginDelete.remove(req)
	}

	return resp, nil
}

func (s *SchemaRegistriesServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if s.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\.DeviceRegistry/schemaRegistries/(?P<schemaRegistryName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 4 {
		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
	}
	schemaRegistryNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("schemaRegistryName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := s.srv.Get(req.Context(), resourceGroupNameParam, schemaRegistryNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !slices.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).SchemaRegistry, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (s *SchemaRegistriesServerTransport) dispatchNewListByResourceGroupPager(req *http.Request) (*http.Response, error) {
	if s.srv.NewListByResourceGroupPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByResourceGroupPager not implemented")}
	}
	newListByResourceGroupPager := s.newListByResourceGroupPager.get(req)
	if newListByResourceGroupPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.DeviceRegistry/schemaRegistries`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if 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
		}
		resp := s.srv.NewListByResourceGroupPager(resourceGroupNameParam, nil)
		newListByResourceGroupPager = &resp
		s.newListByResourceGroupPager.add(req, newListByResourceGroupPager)
		server.PagerResponderInjectNextLinks(newListByResourceGroupPager, req, func(page *armdeviceregistry.SchemaRegistriesClientListByResourceGroupResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByResourceGroupPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		s.newListByResourceGroupPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByResourceGroupPager) {
		s.newListByResourceGroupPager.remove(req)
	}
	return resp, nil
}

func (s *SchemaRegistriesServerTransport) dispatchNewListBySubscriptionPager(req *http.Request) (*http.Response, error) {
	if s.srv.NewListBySubscriptionPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListBySubscriptionPager not implemented")}
	}
	newListBySubscriptionPager := s.newListBySubscriptionPager.get(req)
	if newListBySubscriptionPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.DeviceRegistry/schemaRegistries`
		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)
		}
		resp := s.srv.NewListBySubscriptionPager(nil)
		newListBySubscriptionPager = &resp
		s.newListBySubscriptionPager.add(req, newListBySubscriptionPager)
		server.PagerResponderInjectNextLinks(newListBySubscriptionPager, req, func(page *armdeviceregistry.SchemaRegistriesClientListBySubscriptionResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListBySubscriptionPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		s.newListBySubscriptionPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListBySubscriptionPager) {
		s.newListBySubscriptionPager.remove(req)
	}
	return resp, nil
}

func (s *SchemaRegistriesServerTransport) dispatchBeginUpdate(req *http.Request) (*http.Response, error) {
	if s.srv.BeginUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginUpdate not implemented")}
	}
	beginUpdate := s.beginUpdate.get(req)
	if beginUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.DeviceRegistry/schemaRegistries/(?P<schemaRegistryName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 4 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		body, err := server.UnmarshalRequestAsJSON[armdeviceregistry.SchemaRegistryUpdate](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		schemaRegistryNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("schemaRegistryName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := s.srv.BeginUpdate(req.Context(), resourceGroupNameParam, schemaRegistryNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginUpdate = &respr
		s.beginUpdate.add(req, beginUpdate)
	}

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

	if !slices.Contains([]int{http.StatusOK, http.StatusAccepted}, resp.StatusCode) {
		s.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) {
		s.beginUpdate.remove(req)
	}

	return resp, nil
}

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