// 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/resourcemanager/cognitiveservices/armcognitiveservices/v4"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// OutboundRulesServer is a fake server for instances of the armcognitiveservices.OutboundRulesClient type.
type OutboundRulesServer struct {
	// BeginPost is the fake for method OutboundRulesClient.BeginPost
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginPost func(ctx context.Context, resourceGroupName string, accountName string, managedNetworkName string, body armcognitiveservices.ManagedNetworkSettingsBasicResource, options *armcognitiveservices.OutboundRulesClientBeginPostOptions) (resp azfake.PollerResponder[azfake.PagerResponder[armcognitiveservices.OutboundRulesClientPostResponse]], errResp azfake.ErrorResponder)
}

// NewOutboundRulesServerTransport creates a new instance of OutboundRulesServerTransport with the provided implementation.
// The returned OutboundRulesServerTransport instance is connected to an instance of armcognitiveservices.OutboundRulesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewOutboundRulesServerTransport(srv *OutboundRulesServer) *OutboundRulesServerTransport {
	return &OutboundRulesServerTransport{
		srv:       srv,
		beginPost: newTracker[azfake.PollerResponder[azfake.PagerResponder[armcognitiveservices.OutboundRulesClientPostResponse]]](),
	}
}

// OutboundRulesServerTransport connects instances of armcognitiveservices.OutboundRulesClient to instances of OutboundRulesServer.
// Don't use this type directly, use NewOutboundRulesServerTransport instead.
type OutboundRulesServerTransport struct {
	srv       *OutboundRulesServer
	beginPost *tracker[azfake.PollerResponder[azfake.PagerResponder[armcognitiveservices.OutboundRulesClientPostResponse]]]
}

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

func (o *OutboundRulesServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if outboundRulesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = outboundRulesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "OutboundRulesClient.BeginPost":
				res.resp, res.err = o.dispatchBeginPost(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 (o *OutboundRulesServerTransport) dispatchBeginPost(req *http.Request) (*http.Response, error) {
	if o.srv.BeginPost == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginPost not implemented")}
	}
	beginPost := o.beginPost.get(req)
	if beginPost == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.CognitiveServices/accounts/(?P<accountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/managedNetworks/(?P<managedNetworkName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/batchOutboundRules`
		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[armcognitiveservices.ManagedNetworkSettingsBasicResource](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		accountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("accountName")])
		if err != nil {
			return nil, err
		}
		managedNetworkNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("managedNetworkName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := o.srv.BeginPost(req.Context(), resourceGroupNameParam, accountNameParam, managedNetworkNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginPost = &respr
		o.beginPost.add(req, beginPost)
	}

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

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

	return resp, nil
}

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