// 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/enclave/armenclave"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// TransitHubServer is a fake server for instances of the armenclave.TransitHubClient type.
type TransitHubServer struct {
	// BeginCreateOrUpdate is the fake for method TransitHubClient.BeginCreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, communityName string, transitHubName string, resource armenclave.TransitHubResource, options *armenclave.TransitHubClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armenclave.TransitHubClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method TransitHubClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, communityName string, transitHubName string, options *armenclave.TransitHubClientBeginDeleteOptions) (resp azfake.PollerResponder[armenclave.TransitHubClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method TransitHubClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, communityName string, transitHubName string, options *armenclave.TransitHubClientGetOptions) (resp azfake.Responder[armenclave.TransitHubClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByCommunityResourcePager is the fake for method TransitHubClient.NewListByCommunityResourcePager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByCommunityResourcePager func(resourceGroupName string, communityName string, options *armenclave.TransitHubClientListByCommunityResourceOptions) (resp azfake.PagerResponder[armenclave.TransitHubClientListByCommunityResourceResponse])

	// NewListBySubscriptionPager is the fake for method TransitHubClient.NewListBySubscriptionPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListBySubscriptionPager func(communityName string, options *armenclave.TransitHubClientListBySubscriptionOptions) (resp azfake.PagerResponder[armenclave.TransitHubClientListBySubscriptionResponse])

	// BeginUpdate is the fake for method TransitHubClient.BeginUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginUpdate func(ctx context.Context, resourceGroupName string, communityName string, transitHubName string, properties armenclave.TransitHubPatchModel, options *armenclave.TransitHubClientBeginUpdateOptions) (resp azfake.PollerResponder[armenclave.TransitHubClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewTransitHubServerTransport creates a new instance of TransitHubServerTransport with the provided implementation.
// The returned TransitHubServerTransport instance is connected to an instance of armenclave.TransitHubClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewTransitHubServerTransport(srv *TransitHubServer) *TransitHubServerTransport {
	return &TransitHubServerTransport{
		srv:                             srv,
		beginCreateOrUpdate:             newTracker[azfake.PollerResponder[armenclave.TransitHubClientCreateOrUpdateResponse]](),
		beginDelete:                     newTracker[azfake.PollerResponder[armenclave.TransitHubClientDeleteResponse]](),
		newListByCommunityResourcePager: newTracker[azfake.PagerResponder[armenclave.TransitHubClientListByCommunityResourceResponse]](),
		newListBySubscriptionPager:      newTracker[azfake.PagerResponder[armenclave.TransitHubClientListBySubscriptionResponse]](),
		beginUpdate:                     newTracker[azfake.PollerResponder[armenclave.TransitHubClientUpdateResponse]](),
	}
}

// TransitHubServerTransport connects instances of armenclave.TransitHubClient to instances of TransitHubServer.
// Don't use this type directly, use NewTransitHubServerTransport instead.
type TransitHubServerTransport struct {
	srv                             *TransitHubServer
	beginCreateOrUpdate             *tracker[azfake.PollerResponder[armenclave.TransitHubClientCreateOrUpdateResponse]]
	beginDelete                     *tracker[azfake.PollerResponder[armenclave.TransitHubClientDeleteResponse]]
	newListByCommunityResourcePager *tracker[azfake.PagerResponder[armenclave.TransitHubClientListByCommunityResourceResponse]]
	newListBySubscriptionPager      *tracker[azfake.PagerResponder[armenclave.TransitHubClientListBySubscriptionResponse]]
	beginUpdate                     *tracker[azfake.PollerResponder[armenclave.TransitHubClientUpdateResponse]]
}

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

func (t *TransitHubServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if transitHubServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = transitHubServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "TransitHubClient.BeginCreateOrUpdate":
				res.resp, res.err = t.dispatchBeginCreateOrUpdate(req)
			case "TransitHubClient.BeginDelete":
				res.resp, res.err = t.dispatchBeginDelete(req)
			case "TransitHubClient.Get":
				res.resp, res.err = t.dispatchGet(req)
			case "TransitHubClient.NewListByCommunityResourcePager":
				res.resp, res.err = t.dispatchNewListByCommunityResourcePager(req)
			case "TransitHubClient.NewListBySubscriptionPager":
				res.resp, res.err = t.dispatchNewListBySubscriptionPager(req)
			case "TransitHubClient.BeginUpdate":
				res.resp, res.err = t.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 (t *TransitHubServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if t.srv.BeginCreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")}
	}
	beginCreateOrUpdate := t.beginCreateOrUpdate.get(req)
	if beginCreateOrUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Mission/communities/(?P<communityName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/transitHubs/(?P<transitHubName>[!#&$-;=?-\[\]_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[armenclave.TransitHubResource](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		communityNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("communityName")])
		if err != nil {
			return nil, err
		}
		transitHubNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("transitHubName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := t.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, communityNameParam, transitHubNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOrUpdate = &respr
		t.beginCreateOrUpdate.add(req, beginCreateOrUpdate)
	}

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

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

	return resp, nil
}

func (t *TransitHubServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if t.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := t.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Mission/communities/(?P<communityName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/transitHubs/(?P<transitHubName>[!#&$-;=?-\[\]_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
		}
		communityNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("communityName")])
		if err != nil {
			return nil, err
		}
		transitHubNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("transitHubName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := t.srv.BeginDelete(req.Context(), resourceGroupNameParam, communityNameParam, transitHubNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		t.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) {
		t.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) {
		t.beginDelete.remove(req)
	}

	return resp, nil
}

func (t *TransitHubServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if t.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\.Mission/communities/(?P<communityName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/transitHubs/(?P<transitHubName>[!#&$-;=?-\[\]_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
	}
	communityNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("communityName")])
	if err != nil {
		return nil, err
	}
	transitHubNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("transitHubName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.Get(req.Context(), resourceGroupNameParam, communityNameParam, transitHubNameParam, 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).TransitHubResource, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (t *TransitHubServerTransport) dispatchNewListByCommunityResourcePager(req *http.Request) (*http.Response, error) {
	if t.srv.NewListByCommunityResourcePager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByCommunityResourcePager not implemented")}
	}
	newListByCommunityResourcePager := t.newListByCommunityResourcePager.get(req)
	if newListByCommunityResourcePager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Mission/communities/(?P<communityName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/transitHubs`
		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
		}
		communityNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("communityName")])
		if err != nil {
			return nil, err
		}
		resp := t.srv.NewListByCommunityResourcePager(resourceGroupNameParam, communityNameParam, nil)
		newListByCommunityResourcePager = &resp
		t.newListByCommunityResourcePager.add(req, newListByCommunityResourcePager)
		server.PagerResponderInjectNextLinks(newListByCommunityResourcePager, req, func(page *armenclave.TransitHubClientListByCommunityResourceResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByCommunityResourcePager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		t.newListByCommunityResourcePager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByCommunityResourcePager) {
		t.newListByCommunityResourcePager.remove(req)
	}
	return resp, nil
}

func (t *TransitHubServerTransport) dispatchNewListBySubscriptionPager(req *http.Request) (*http.Response, error) {
	if t.srv.NewListBySubscriptionPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListBySubscriptionPager not implemented")}
	}
	newListBySubscriptionPager := t.newListBySubscriptionPager.get(req)
	if newListBySubscriptionPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Mission/communities/(?P<communityName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/transitHubs`
		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)
		}
		communityNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("communityName")])
		if err != nil {
			return nil, err
		}
		resp := t.srv.NewListBySubscriptionPager(communityNameParam, nil)
		newListBySubscriptionPager = &resp
		t.newListBySubscriptionPager.add(req, newListBySubscriptionPager)
		server.PagerResponderInjectNextLinks(newListBySubscriptionPager, req, func(page *armenclave.TransitHubClientListBySubscriptionResponse, 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) {
		t.newListBySubscriptionPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListBySubscriptionPager) {
		t.newListBySubscriptionPager.remove(req)
	}
	return resp, nil
}

func (t *TransitHubServerTransport) dispatchBeginUpdate(req *http.Request) (*http.Response, error) {
	if t.srv.BeginUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginUpdate not implemented")}
	}
	beginUpdate := t.beginUpdate.get(req)
	if beginUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Mission/communities/(?P<communityName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/transitHubs/(?P<transitHubName>[!#&$-;=?-\[\]_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[armenclave.TransitHubPatchModel](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		communityNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("communityName")])
		if err != nil {
			return nil, err
		}
		transitHubNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("transitHubName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := t.srv.BeginUpdate(req.Context(), resourceGroupNameParam, communityNameParam, transitHubNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginUpdate = &respr
		t.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) {
		t.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) {
		t.beginUpdate.remove(req)
	}

	return resp, nil
}

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