// 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/billingbenefits/armbillingbenefits/v3"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// ContributorsServer is a fake server for instances of the armbillingbenefits.ContributorsClient type.
type ContributorsServer struct {
	// GetFromPrimary is the fake for method ContributorsClient.GetFromPrimary
	// HTTP status codes to indicate success: http.StatusOK
	GetFromPrimary func(ctx context.Context, resourceGroupName string, maccName string, contributorName string, options *armbillingbenefits.ContributorsClientGetFromPrimaryOptions) (resp azfake.Responder[armbillingbenefits.ContributorsClientGetFromPrimaryResponse], errResp azfake.ErrorResponder)

	// NewListFromApplicableMaccPager is the fake for method ContributorsClient.NewListFromApplicableMaccPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListFromApplicableMaccPager func(billingAccountID string, systemID string, options *armbillingbenefits.ContributorsClientListFromApplicableMaccOptions) (resp azfake.PagerResponder[armbillingbenefits.ContributorsClientListFromApplicableMaccResponse])

	// NewListFromPrimaryPager is the fake for method ContributorsClient.NewListFromPrimaryPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListFromPrimaryPager func(resourceGroupName string, maccName string, options *armbillingbenefits.ContributorsClientListFromPrimaryOptions) (resp azfake.PagerResponder[armbillingbenefits.ContributorsClientListFromPrimaryResponse])
}

// NewContributorsServerTransport creates a new instance of ContributorsServerTransport with the provided implementation.
// The returned ContributorsServerTransport instance is connected to an instance of armbillingbenefits.ContributorsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewContributorsServerTransport(srv *ContributorsServer) *ContributorsServerTransport {
	return &ContributorsServerTransport{
		srv:                            srv,
		newListFromApplicableMaccPager: newTracker[azfake.PagerResponder[armbillingbenefits.ContributorsClientListFromApplicableMaccResponse]](),
		newListFromPrimaryPager:        newTracker[azfake.PagerResponder[armbillingbenefits.ContributorsClientListFromPrimaryResponse]](),
	}
}

// ContributorsServerTransport connects instances of armbillingbenefits.ContributorsClient to instances of ContributorsServer.
// Don't use this type directly, use NewContributorsServerTransport instead.
type ContributorsServerTransport struct {
	srv                            *ContributorsServer
	newListFromApplicableMaccPager *tracker[azfake.PagerResponder[armbillingbenefits.ContributorsClientListFromApplicableMaccResponse]]
	newListFromPrimaryPager        *tracker[azfake.PagerResponder[armbillingbenefits.ContributorsClientListFromPrimaryResponse]]
}

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

func (c *ContributorsServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if contributorsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = contributorsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "ContributorsClient.GetFromPrimary":
				res.resp, res.err = c.dispatchGetFromPrimary(req)
			case "ContributorsClient.NewListFromApplicableMaccPager":
				res.resp, res.err = c.dispatchNewListFromApplicableMaccPager(req)
			case "ContributorsClient.NewListFromPrimaryPager":
				res.resp, res.err = c.dispatchNewListFromPrimaryPager(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 (c *ContributorsServerTransport) dispatchGetFromPrimary(req *http.Request) (*http.Response, error) {
	if c.srv.GetFromPrimary == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetFromPrimary not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.BillingBenefits/maccs/(?P<maccName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/contributors/(?P<contributorName>[!#&$-;=?-\[\]_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
	}
	maccNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("maccName")])
	if err != nil {
		return nil, err
	}
	contributorNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("contributorName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := c.srv.GetFromPrimary(req.Context(), resourceGroupNameParam, maccNameParam, contributorNameParam, 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).Contributor, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (c *ContributorsServerTransport) dispatchNewListFromApplicableMaccPager(req *http.Request) (*http.Response, error) {
	if c.srv.NewListFromApplicableMaccPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListFromApplicableMaccPager not implemented")}
	}
	newListFromApplicableMaccPager := c.newListFromApplicableMaccPager.get(req)
	if newListFromApplicableMaccPager == nil {
		const regexStr = `/providers/microsoft\.Billing/billingAccounts/(?P<billingAccountId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.BillingBenefits/applicableMaccs/(?P<systemId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/microsoft\.BillingBenefits/applicableContributors`
		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)
		}
		billingAccountIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("billingAccountId")])
		if err != nil {
			return nil, err
		}
		systemIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("systemId")])
		if err != nil {
			return nil, err
		}
		resp := c.srv.NewListFromApplicableMaccPager(billingAccountIDParam, systemIDParam, nil)
		newListFromApplicableMaccPager = &resp
		c.newListFromApplicableMaccPager.add(req, newListFromApplicableMaccPager)
		server.PagerResponderInjectNextLinks(newListFromApplicableMaccPager, req, func(page *armbillingbenefits.ContributorsClientListFromApplicableMaccResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListFromApplicableMaccPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		c.newListFromApplicableMaccPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListFromApplicableMaccPager) {
		c.newListFromApplicableMaccPager.remove(req)
	}
	return resp, nil
}

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

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