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

// SiteReferencesServer is a fake server for instances of the armworkloadorchestration.SiteReferencesClient type.
type SiteReferencesServer struct {
	// BeginCreateOrUpdate is the fake for method SiteReferencesClient.BeginCreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, contextName string, siteReferenceName string, resource armworkloadorchestration.SiteReference, options *armworkloadorchestration.SiteReferencesClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armworkloadorchestration.SiteReferencesClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method SiteReferencesClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, contextName string, siteReferenceName string, options *armworkloadorchestration.SiteReferencesClientBeginDeleteOptions) (resp azfake.PollerResponder[armworkloadorchestration.SiteReferencesClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method SiteReferencesClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, contextName string, siteReferenceName string, options *armworkloadorchestration.SiteReferencesClientGetOptions) (resp azfake.Responder[armworkloadorchestration.SiteReferencesClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByContextPager is the fake for method SiteReferencesClient.NewListByContextPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByContextPager func(resourceGroupName string, contextName string, options *armworkloadorchestration.SiteReferencesClientListByContextOptions) (resp azfake.PagerResponder[armworkloadorchestration.SiteReferencesClientListByContextResponse])

	// BeginUpdate is the fake for method SiteReferencesClient.BeginUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginUpdate func(ctx context.Context, resourceGroupName string, contextName string, siteReferenceName string, properties armworkloadorchestration.SiteReference, options *armworkloadorchestration.SiteReferencesClientBeginUpdateOptions) (resp azfake.PollerResponder[armworkloadorchestration.SiteReferencesClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewSiteReferencesServerTransport creates a new instance of SiteReferencesServerTransport with the provided implementation.
// The returned SiteReferencesServerTransport instance is connected to an instance of armworkloadorchestration.SiteReferencesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewSiteReferencesServerTransport(srv *SiteReferencesServer) *SiteReferencesServerTransport {
	return &SiteReferencesServerTransport{
		srv:                   srv,
		beginCreateOrUpdate:   newTracker[azfake.PollerResponder[armworkloadorchestration.SiteReferencesClientCreateOrUpdateResponse]](),
		beginDelete:           newTracker[azfake.PollerResponder[armworkloadorchestration.SiteReferencesClientDeleteResponse]](),
		newListByContextPager: newTracker[azfake.PagerResponder[armworkloadorchestration.SiteReferencesClientListByContextResponse]](),
		beginUpdate:           newTracker[azfake.PollerResponder[armworkloadorchestration.SiteReferencesClientUpdateResponse]](),
	}
}

// SiteReferencesServerTransport connects instances of armworkloadorchestration.SiteReferencesClient to instances of SiteReferencesServer.
// Don't use this type directly, use NewSiteReferencesServerTransport instead.
type SiteReferencesServerTransport struct {
	srv                   *SiteReferencesServer
	beginCreateOrUpdate   *tracker[azfake.PollerResponder[armworkloadorchestration.SiteReferencesClientCreateOrUpdateResponse]]
	beginDelete           *tracker[azfake.PollerResponder[armworkloadorchestration.SiteReferencesClientDeleteResponse]]
	newListByContextPager *tracker[azfake.PagerResponder[armworkloadorchestration.SiteReferencesClientListByContextResponse]]
	beginUpdate           *tracker[azfake.PollerResponder[armworkloadorchestration.SiteReferencesClientUpdateResponse]]
}

// Do implements the policy.Transporter interface for SiteReferencesServerTransport.
func (s *SiteReferencesServerTransport) 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 *SiteReferencesServerTransport) 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 siteReferencesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = siteReferencesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "SiteReferencesClient.BeginCreateOrUpdate":
				res.resp, res.err = s.dispatchBeginCreateOrUpdate(req)
			case "SiteReferencesClient.BeginDelete":
				res.resp, res.err = s.dispatchBeginDelete(req)
			case "SiteReferencesClient.Get":
				res.resp, res.err = s.dispatchGet(req)
			case "SiteReferencesClient.NewListByContextPager":
				res.resp, res.err = s.dispatchNewListByContextPager(req)
			case "SiteReferencesClient.BeginUpdate":
				res.resp, res.err = s.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 (s *SiteReferencesServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if s.srv.BeginCreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")}
	}
	beginCreateOrUpdate := s.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/contexts/(?P<contextName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/siteReferences/(?P<siteReferenceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		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)
		}
		body, err := server.UnmarshalRequestAsJSON[armworkloadorchestration.SiteReference](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		contextNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("contextName")])
		if err != nil {
			return nil, err
		}
		siteReferenceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("siteReferenceName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := s.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, contextNameParam, siteReferenceNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOrUpdate = &respr
		s.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) {
		s.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) {
		s.beginCreateOrUpdate.remove(req)
	}

	return resp, nil
}

func (s *SiteReferencesServerTransport) 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\.Edge/contexts/(?P<contextName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/siteReferences/(?P<siteReferenceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		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
		}
		contextNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("contextName")])
		if err != nil {
			return nil, err
		}
		siteReferenceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("siteReferenceName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := s.srv.BeginDelete(req.Context(), resourceGroupNameParam, contextNameParam, siteReferenceNameParam, 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 !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 *SiteReferencesServerTransport) 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\.Edge/contexts/(?P<contextName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/siteReferences/(?P<siteReferenceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	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
	}
	contextNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("contextName")])
	if err != nil {
		return nil, err
	}
	siteReferenceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("siteReferenceName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := s.srv.Get(req.Context(), resourceGroupNameParam, contextNameParam, siteReferenceNameParam, 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).SiteReference, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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

func (s *SiteReferencesServerTransport) 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\.Edge/contexts/(?P<contextName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/siteReferences/(?P<siteReferenceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		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)
		}
		body, err := server.UnmarshalRequestAsJSON[armworkloadorchestration.SiteReference](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		contextNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("contextName")])
		if err != nil {
			return nil, err
		}
		siteReferenceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("siteReferenceName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := s.srv.BeginUpdate(req.Context(), resourceGroupNameParam, contextNameParam, siteReferenceNameParam, 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 !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 SiteReferencesServerTransport
var siteReferencesServerTransportInterceptor interface {
	// Do returns true if the server transport should use the returned response/error
	Do(*http.Request) (*http.Response, error, bool)
}
