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

// BrokerAuthenticationServer is a fake server for instances of the armiotoperations.BrokerAuthenticationClient type.
type BrokerAuthenticationServer struct {
	// BeginCreateOrUpdate is the fake for method BrokerAuthenticationClient.BeginCreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, instanceName string, brokerName string, authenticationName string, resource armiotoperations.BrokerAuthenticationResource, options *armiotoperations.BrokerAuthenticationClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armiotoperations.BrokerAuthenticationClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method BrokerAuthenticationClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, instanceName string, brokerName string, authenticationName string, options *armiotoperations.BrokerAuthenticationClientBeginDeleteOptions) (resp azfake.PollerResponder[armiotoperations.BrokerAuthenticationClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method BrokerAuthenticationClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, instanceName string, brokerName string, authenticationName string, options *armiotoperations.BrokerAuthenticationClientGetOptions) (resp azfake.Responder[armiotoperations.BrokerAuthenticationClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByResourceGroupPager is the fake for method BrokerAuthenticationClient.NewListByResourceGroupPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByResourceGroupPager func(resourceGroupName string, instanceName string, brokerName string, options *armiotoperations.BrokerAuthenticationClientListByResourceGroupOptions) (resp azfake.PagerResponder[armiotoperations.BrokerAuthenticationClientListByResourceGroupResponse])
}

// NewBrokerAuthenticationServerTransport creates a new instance of BrokerAuthenticationServerTransport with the provided implementation.
// The returned BrokerAuthenticationServerTransport instance is connected to an instance of armiotoperations.BrokerAuthenticationClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewBrokerAuthenticationServerTransport(srv *BrokerAuthenticationServer) *BrokerAuthenticationServerTransport {
	return &BrokerAuthenticationServerTransport{
		srv:                         srv,
		beginCreateOrUpdate:         newTracker[azfake.PollerResponder[armiotoperations.BrokerAuthenticationClientCreateOrUpdateResponse]](),
		beginDelete:                 newTracker[azfake.PollerResponder[armiotoperations.BrokerAuthenticationClientDeleteResponse]](),
		newListByResourceGroupPager: newTracker[azfake.PagerResponder[armiotoperations.BrokerAuthenticationClientListByResourceGroupResponse]](),
	}
}

// BrokerAuthenticationServerTransport connects instances of armiotoperations.BrokerAuthenticationClient to instances of BrokerAuthenticationServer.
// Don't use this type directly, use NewBrokerAuthenticationServerTransport instead.
type BrokerAuthenticationServerTransport struct {
	srv                         *BrokerAuthenticationServer
	beginCreateOrUpdate         *tracker[azfake.PollerResponder[armiotoperations.BrokerAuthenticationClientCreateOrUpdateResponse]]
	beginDelete                 *tracker[azfake.PollerResponder[armiotoperations.BrokerAuthenticationClientDeleteResponse]]
	newListByResourceGroupPager *tracker[azfake.PagerResponder[armiotoperations.BrokerAuthenticationClientListByResourceGroupResponse]]
}

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

func (b *BrokerAuthenticationServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if brokerAuthenticationServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = brokerAuthenticationServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "BrokerAuthenticationClient.BeginCreateOrUpdate":
				res.resp, res.err = b.dispatchBeginCreateOrUpdate(req)
			case "BrokerAuthenticationClient.BeginDelete":
				res.resp, res.err = b.dispatchBeginDelete(req)
			case "BrokerAuthenticationClient.Get":
				res.resp, res.err = b.dispatchGet(req)
			case "BrokerAuthenticationClient.NewListByResourceGroupPager":
				res.resp, res.err = b.dispatchNewListByResourceGroupPager(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 (b *BrokerAuthenticationServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if b.srv.BeginCreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")}
	}
	beginCreateOrUpdate := b.beginCreateOrUpdate.get(req)
	if beginCreateOrUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.IoTOperations/instances/(?P<instanceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/brokers/(?P<brokerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/authentications/(?P<authenticationName>[!#&$-;=?-\[\]_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[armiotoperations.BrokerAuthenticationResource](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		instanceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("instanceName")])
		if err != nil {
			return nil, err
		}
		brokerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("brokerName")])
		if err != nil {
			return nil, err
		}
		authenticationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("authenticationName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := b.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, instanceNameParam, brokerNameParam, authenticationNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOrUpdate = &respr
		b.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) {
		b.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) {
		b.beginCreateOrUpdate.remove(req)
	}

	return resp, nil
}

func (b *BrokerAuthenticationServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if b.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := b.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.IoTOperations/instances/(?P<instanceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/brokers/(?P<brokerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/authentications/(?P<authenticationName>[!#&$-;=?-\[\]_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
		}
		instanceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("instanceName")])
		if err != nil {
			return nil, err
		}
		brokerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("brokerName")])
		if err != nil {
			return nil, err
		}
		authenticationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("authenticationName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := b.srv.BeginDelete(req.Context(), resourceGroupNameParam, instanceNameParam, brokerNameParam, authenticationNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		b.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) {
		b.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) {
		b.beginDelete.remove(req)
	}

	return resp, nil
}

func (b *BrokerAuthenticationServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if b.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\.IoTOperations/instances/(?P<instanceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/brokers/(?P<brokerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/authentications/(?P<authenticationName>[!#&$-;=?-\[\]_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
	}
	instanceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("instanceName")])
	if err != nil {
		return nil, err
	}
	brokerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("brokerName")])
	if err != nil {
		return nil, err
	}
	authenticationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("authenticationName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := b.srv.Get(req.Context(), resourceGroupNameParam, instanceNameParam, brokerNameParam, authenticationNameParam, 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).BrokerAuthenticationResource, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (b *BrokerAuthenticationServerTransport) dispatchNewListByResourceGroupPager(req *http.Request) (*http.Response, error) {
	if b.srv.NewListByResourceGroupPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByResourceGroupPager not implemented")}
	}
	newListByResourceGroupPager := b.newListByResourceGroupPager.get(req)
	if newListByResourceGroupPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.IoTOperations/instances/(?P<instanceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/brokers/(?P<brokerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/authentications`
		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
		}
		instanceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("instanceName")])
		if err != nil {
			return nil, err
		}
		brokerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("brokerName")])
		if err != nil {
			return nil, err
		}
		resp := b.srv.NewListByResourceGroupPager(resourceGroupNameParam, instanceNameParam, brokerNameParam, nil)
		newListByResourceGroupPager = &resp
		b.newListByResourceGroupPager.add(req, newListByResourceGroupPager)
		server.PagerResponderInjectNextLinks(newListByResourceGroupPager, req, func(page *armiotoperations.BrokerAuthenticationClientListByResourceGroupResponse, 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) {
		b.newListByResourceGroupPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByResourceGroupPager) {
		b.newListByResourceGroupPager.remove(req)
	}
	return resp, nil
}

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