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

// DiscountsServer is a fake server for instances of the armbillingbenefits.DiscountsClient type.
type DiscountsServer struct {
	// BeginCancel is the fake for method DiscountsClient.BeginCancel
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginCancel func(ctx context.Context, resourceGroupName string, discountName string, options *armbillingbenefits.DiscountsClientBeginCancelOptions) (resp azfake.PollerResponder[armbillingbenefits.DiscountsClientCancelResponse], errResp azfake.ErrorResponder)

	// BeginCreate is the fake for method DiscountsClient.BeginCreate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreate func(ctx context.Context, resourceGroupName string, discountName string, body armbillingbenefits.Discount, options *armbillingbenefits.DiscountsClientBeginCreateOptions) (resp azfake.PollerResponder[armbillingbenefits.DiscountsClientCreateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method DiscountsClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, discountName string, options *armbillingbenefits.DiscountsClientBeginDeleteOptions) (resp azfake.PollerResponder[armbillingbenefits.DiscountsClientDeleteResponse], errResp azfake.ErrorResponder)

	// NewResourceGroupListPager is the fake for method DiscountsClient.NewResourceGroupListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewResourceGroupListPager func(resourceGroupName string, options *armbillingbenefits.DiscountsClientResourceGroupListOptions) (resp azfake.PagerResponder[armbillingbenefits.DiscountsClientResourceGroupListResponse])

	// NewScopeListPager is the fake for method DiscountsClient.NewScopeListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewScopeListPager func(scope string, options *armbillingbenefits.DiscountsClientScopeListOptions) (resp azfake.PagerResponder[armbillingbenefits.DiscountsClientScopeListResponse])

	// NewSubscriptionListPager is the fake for method DiscountsClient.NewSubscriptionListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewSubscriptionListPager func(options *armbillingbenefits.DiscountsClientSubscriptionListOptions) (resp azfake.PagerResponder[armbillingbenefits.DiscountsClientSubscriptionListResponse])
}

// NewDiscountsServerTransport creates a new instance of DiscountsServerTransport with the provided implementation.
// The returned DiscountsServerTransport instance is connected to an instance of armbillingbenefits.DiscountsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewDiscountsServerTransport(srv *DiscountsServer) *DiscountsServerTransport {
	return &DiscountsServerTransport{
		srv:                       srv,
		beginCancel:               newTracker[azfake.PollerResponder[armbillingbenefits.DiscountsClientCancelResponse]](),
		beginCreate:               newTracker[azfake.PollerResponder[armbillingbenefits.DiscountsClientCreateResponse]](),
		beginDelete:               newTracker[azfake.PollerResponder[armbillingbenefits.DiscountsClientDeleteResponse]](),
		newResourceGroupListPager: newTracker[azfake.PagerResponder[armbillingbenefits.DiscountsClientResourceGroupListResponse]](),
		newScopeListPager:         newTracker[azfake.PagerResponder[armbillingbenefits.DiscountsClientScopeListResponse]](),
		newSubscriptionListPager:  newTracker[azfake.PagerResponder[armbillingbenefits.DiscountsClientSubscriptionListResponse]](),
	}
}

// DiscountsServerTransport connects instances of armbillingbenefits.DiscountsClient to instances of DiscountsServer.
// Don't use this type directly, use NewDiscountsServerTransport instead.
type DiscountsServerTransport struct {
	srv                       *DiscountsServer
	beginCancel               *tracker[azfake.PollerResponder[armbillingbenefits.DiscountsClientCancelResponse]]
	beginCreate               *tracker[azfake.PollerResponder[armbillingbenefits.DiscountsClientCreateResponse]]
	beginDelete               *tracker[azfake.PollerResponder[armbillingbenefits.DiscountsClientDeleteResponse]]
	newResourceGroupListPager *tracker[azfake.PagerResponder[armbillingbenefits.DiscountsClientResourceGroupListResponse]]
	newScopeListPager         *tracker[azfake.PagerResponder[armbillingbenefits.DiscountsClientScopeListResponse]]
	newSubscriptionListPager  *tracker[azfake.PagerResponder[armbillingbenefits.DiscountsClientSubscriptionListResponse]]
}

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

func (d *DiscountsServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if discountsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = discountsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "DiscountsClient.BeginCancel":
				res.resp, res.err = d.dispatchBeginCancel(req)
			case "DiscountsClient.BeginCreate":
				res.resp, res.err = d.dispatchBeginCreate(req)
			case "DiscountsClient.BeginDelete":
				res.resp, res.err = d.dispatchBeginDelete(req)
			case "DiscountsClient.NewResourceGroupListPager":
				res.resp, res.err = d.dispatchNewResourceGroupListPager(req)
			case "DiscountsClient.NewScopeListPager":
				res.resp, res.err = d.dispatchNewScopeListPager(req)
			case "DiscountsClient.NewSubscriptionListPager":
				res.resp, res.err = d.dispatchNewSubscriptionListPager(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 (d *DiscountsServerTransport) dispatchBeginCancel(req *http.Request) (*http.Response, error) {
	if d.srv.BeginCancel == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCancel not implemented")}
	}
	beginCancel := d.beginCancel.get(req)
	if beginCancel == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.BillingBenefits/discounts/(?P<discountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/cancel`
		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
		}
		discountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("discountName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := d.srv.BeginCancel(req.Context(), resourceGroupNameParam, discountNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCancel = &respr
		d.beginCancel.add(req, beginCancel)
	}

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

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

	return resp, nil
}

func (d *DiscountsServerTransport) dispatchBeginCreate(req *http.Request) (*http.Response, error) {
	if d.srv.BeginCreate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreate not implemented")}
	}
	beginCreate := d.beginCreate.get(req)
	if beginCreate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.BillingBenefits/discounts/(?P<discountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		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)
		}
		body, err := server.UnmarshalRequestAsJSON[armbillingbenefits.Discount](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		discountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("discountName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := d.srv.BeginCreate(req.Context(), resourceGroupNameParam, discountNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreate = &respr
		d.beginCreate.add(req, beginCreate)
	}

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

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

	return resp, nil
}

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

	return resp, nil
}

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

func (d *DiscountsServerTransport) dispatchNewScopeListPager(req *http.Request) (*http.Response, error) {
	if d.srv.NewScopeListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewScopeListPager not implemented")}
	}
	newScopeListPager := d.newScopeListPager.get(req)
	if newScopeListPager == nil {
		const regexStr = `/(?P<scope>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.BillingBenefits/applicableDiscounts`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 2 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		scopeParam, err := url.PathUnescape(matches[regex.SubexpIndex("scope")])
		if err != nil {
			return nil, err
		}
		resp := d.srv.NewScopeListPager(scopeParam, nil)
		newScopeListPager = &resp
		d.newScopeListPager.add(req, newScopeListPager)
		server.PagerResponderInjectNextLinks(newScopeListPager, req, func(page *armbillingbenefits.DiscountsClientScopeListResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newScopeListPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		d.newScopeListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newScopeListPager) {
		d.newScopeListPager.remove(req)
	}
	return resp, nil
}

func (d *DiscountsServerTransport) dispatchNewSubscriptionListPager(req *http.Request) (*http.Response, error) {
	if d.srv.NewSubscriptionListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewSubscriptionListPager not implemented")}
	}
	newSubscriptionListPager := d.newSubscriptionListPager.get(req)
	if newSubscriptionListPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.BillingBenefits/discounts`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 2 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		resp := d.srv.NewSubscriptionListPager(nil)
		newSubscriptionListPager = &resp
		d.newSubscriptionListPager.add(req, newSubscriptionListPager)
		server.PagerResponderInjectNextLinks(newSubscriptionListPager, req, func(page *armbillingbenefits.DiscountsClientSubscriptionListResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newSubscriptionListPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		d.newSubscriptionListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newSubscriptionListPager) {
		d.newSubscriptionListPager.remove(req)
	}
	return resp, nil
}

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