//go:build go1.18
// +build go1.18

// 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) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package fake

import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"net/url"
	"regexp"

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

// PaymentMethodsServer is a fake server for instances of the armbilling.PaymentMethodsClient type.
type PaymentMethodsServer struct {
	// DeleteByUser is the fake for method PaymentMethodsClient.DeleteByUser
	// HTTP status codes to indicate success: http.StatusOK, http.StatusNoContent
	DeleteByUser func(ctx context.Context, paymentMethodName string, options *armbilling.PaymentMethodsClientDeleteByUserOptions) (resp azfake.Responder[armbilling.PaymentMethodsClientDeleteByUserResponse], errResp azfake.ErrorResponder)

	// GetByBillingAccount is the fake for method PaymentMethodsClient.GetByBillingAccount
	// HTTP status codes to indicate success: http.StatusOK
	GetByBillingAccount func(ctx context.Context, billingAccountName string, paymentMethodName string, options *armbilling.PaymentMethodsClientGetByBillingAccountOptions) (resp azfake.Responder[armbilling.PaymentMethodsClientGetByBillingAccountResponse], errResp azfake.ErrorResponder)

	// GetByBillingProfile is the fake for method PaymentMethodsClient.GetByBillingProfile
	// HTTP status codes to indicate success: http.StatusOK
	GetByBillingProfile func(ctx context.Context, billingAccountName string, billingProfileName string, paymentMethodName string, options *armbilling.PaymentMethodsClientGetByBillingProfileOptions) (resp azfake.Responder[armbilling.PaymentMethodsClientGetByBillingProfileResponse], errResp azfake.ErrorResponder)

	// GetByUser is the fake for method PaymentMethodsClient.GetByUser
	// HTTP status codes to indicate success: http.StatusOK
	GetByUser func(ctx context.Context, paymentMethodName string, options *armbilling.PaymentMethodsClientGetByUserOptions) (resp azfake.Responder[armbilling.PaymentMethodsClientGetByUserResponse], errResp azfake.ErrorResponder)

	// NewListByBillingAccountPager is the fake for method PaymentMethodsClient.NewListByBillingAccountPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByBillingAccountPager func(billingAccountName string, options *armbilling.PaymentMethodsClientListByBillingAccountOptions) (resp azfake.PagerResponder[armbilling.PaymentMethodsClientListByBillingAccountResponse])

	// NewListByBillingProfilePager is the fake for method PaymentMethodsClient.NewListByBillingProfilePager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByBillingProfilePager func(billingAccountName string, billingProfileName string, options *armbilling.PaymentMethodsClientListByBillingProfileOptions) (resp azfake.PagerResponder[armbilling.PaymentMethodsClientListByBillingProfileResponse])

	// NewListByUserPager is the fake for method PaymentMethodsClient.NewListByUserPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByUserPager func(options *armbilling.PaymentMethodsClientListByUserOptions) (resp azfake.PagerResponder[armbilling.PaymentMethodsClientListByUserResponse])
}

// NewPaymentMethodsServerTransport creates a new instance of PaymentMethodsServerTransport with the provided implementation.
// The returned PaymentMethodsServerTransport instance is connected to an instance of armbilling.PaymentMethodsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewPaymentMethodsServerTransport(srv *PaymentMethodsServer) *PaymentMethodsServerTransport {
	return &PaymentMethodsServerTransport{
		srv:                          srv,
		newListByBillingAccountPager: newTracker[azfake.PagerResponder[armbilling.PaymentMethodsClientListByBillingAccountResponse]](),
		newListByBillingProfilePager: newTracker[azfake.PagerResponder[armbilling.PaymentMethodsClientListByBillingProfileResponse]](),
		newListByUserPager:           newTracker[azfake.PagerResponder[armbilling.PaymentMethodsClientListByUserResponse]](),
	}
}

// PaymentMethodsServerTransport connects instances of armbilling.PaymentMethodsClient to instances of PaymentMethodsServer.
// Don't use this type directly, use NewPaymentMethodsServerTransport instead.
type PaymentMethodsServerTransport struct {
	srv                          *PaymentMethodsServer
	newListByBillingAccountPager *tracker[azfake.PagerResponder[armbilling.PaymentMethodsClientListByBillingAccountResponse]]
	newListByBillingProfilePager *tracker[azfake.PagerResponder[armbilling.PaymentMethodsClientListByBillingProfileResponse]]
	newListByUserPager           *tracker[azfake.PagerResponder[armbilling.PaymentMethodsClientListByUserResponse]]
}

// Do implements the policy.Transporter interface for PaymentMethodsServerTransport.
func (p *PaymentMethodsServerTransport) 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")}
	}

	var resp *http.Response
	var err error

	switch method {
	case "PaymentMethodsClient.DeleteByUser":
		resp, err = p.dispatchDeleteByUser(req)
	case "PaymentMethodsClient.GetByBillingAccount":
		resp, err = p.dispatchGetByBillingAccount(req)
	case "PaymentMethodsClient.GetByBillingProfile":
		resp, err = p.dispatchGetByBillingProfile(req)
	case "PaymentMethodsClient.GetByUser":
		resp, err = p.dispatchGetByUser(req)
	case "PaymentMethodsClient.NewListByBillingAccountPager":
		resp, err = p.dispatchNewListByBillingAccountPager(req)
	case "PaymentMethodsClient.NewListByBillingProfilePager":
		resp, err = p.dispatchNewListByBillingProfilePager(req)
	case "PaymentMethodsClient.NewListByUserPager":
		resp, err = p.dispatchNewListByUserPager(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (p *PaymentMethodsServerTransport) dispatchDeleteByUser(req *http.Request) (*http.Response, error) {
	if p.srv.DeleteByUser == nil {
		return nil, &nonRetriableError{errors.New("fake for method DeleteByUser not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Billing/paymentMethods/(?P<paymentMethodName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 1 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	paymentMethodNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("paymentMethodName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.DeleteByUser(req.Context(), paymentMethodNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK, http.StatusNoContent}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusNoContent", respContent.HTTPStatus)}
	}
	resp, err := server.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PaymentMethodsServerTransport) dispatchGetByBillingAccount(req *http.Request) (*http.Response, error) {
	if p.srv.GetByBillingAccount == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetByBillingAccount not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Billing/billingAccounts/(?P<billingAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/paymentMethods/(?P<paymentMethodName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 2 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	billingAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("billingAccountName")])
	if err != nil {
		return nil, err
	}
	paymentMethodNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("paymentMethodName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.GetByBillingAccount(req.Context(), billingAccountNameParam, paymentMethodNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !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).PaymentMethod, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PaymentMethodsServerTransport) dispatchGetByBillingProfile(req *http.Request) (*http.Response, error) {
	if p.srv.GetByBillingProfile == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetByBillingProfile not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Billing/billingAccounts/(?P<billingAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/billingProfiles/(?P<billingProfileName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/paymentMethodLinks/(?P<paymentMethodName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 3 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	billingAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("billingAccountName")])
	if err != nil {
		return nil, err
	}
	billingProfileNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("billingProfileName")])
	if err != nil {
		return nil, err
	}
	paymentMethodNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("paymentMethodName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.GetByBillingProfile(req.Context(), billingAccountNameParam, billingProfileNameParam, paymentMethodNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !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).PaymentMethodLink, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PaymentMethodsServerTransport) dispatchGetByUser(req *http.Request) (*http.Response, error) {
	if p.srv.GetByUser == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetByUser not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Billing/paymentMethods/(?P<paymentMethodName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 1 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	paymentMethodNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("paymentMethodName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := p.srv.GetByUser(req.Context(), paymentMethodNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !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).PaymentMethod, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (p *PaymentMethodsServerTransport) dispatchNewListByBillingAccountPager(req *http.Request) (*http.Response, error) {
	if p.srv.NewListByBillingAccountPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByBillingAccountPager not implemented")}
	}
	newListByBillingAccountPager := p.newListByBillingAccountPager.get(req)
	if newListByBillingAccountPager == nil {
		const regexStr = `/providers/Microsoft\.Billing/billingAccounts/(?P<billingAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/paymentMethods`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 1 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		billingAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("billingAccountName")])
		if err != nil {
			return nil, err
		}
		resp := p.srv.NewListByBillingAccountPager(billingAccountNameParam, nil)
		newListByBillingAccountPager = &resp
		p.newListByBillingAccountPager.add(req, newListByBillingAccountPager)
		server.PagerResponderInjectNextLinks(newListByBillingAccountPager, req, func(page *armbilling.PaymentMethodsClientListByBillingAccountResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByBillingAccountPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		p.newListByBillingAccountPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByBillingAccountPager) {
		p.newListByBillingAccountPager.remove(req)
	}
	return resp, nil
}

func (p *PaymentMethodsServerTransport) dispatchNewListByBillingProfilePager(req *http.Request) (*http.Response, error) {
	if p.srv.NewListByBillingProfilePager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByBillingProfilePager not implemented")}
	}
	newListByBillingProfilePager := p.newListByBillingProfilePager.get(req)
	if newListByBillingProfilePager == nil {
		const regexStr = `/providers/Microsoft\.Billing/billingAccounts/(?P<billingAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/billingProfiles/(?P<billingProfileName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/paymentMethodLinks`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 2 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		billingAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("billingAccountName")])
		if err != nil {
			return nil, err
		}
		billingProfileNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("billingProfileName")])
		if err != nil {
			return nil, err
		}
		resp := p.srv.NewListByBillingProfilePager(billingAccountNameParam, billingProfileNameParam, nil)
		newListByBillingProfilePager = &resp
		p.newListByBillingProfilePager.add(req, newListByBillingProfilePager)
		server.PagerResponderInjectNextLinks(newListByBillingProfilePager, req, func(page *armbilling.PaymentMethodsClientListByBillingProfileResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByBillingProfilePager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		p.newListByBillingProfilePager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByBillingProfilePager) {
		p.newListByBillingProfilePager.remove(req)
	}
	return resp, nil
}

func (p *PaymentMethodsServerTransport) dispatchNewListByUserPager(req *http.Request) (*http.Response, error) {
	if p.srv.NewListByUserPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByUserPager not implemented")}
	}
	newListByUserPager := p.newListByUserPager.get(req)
	if newListByUserPager == nil {
		resp := p.srv.NewListByUserPager(nil)
		newListByUserPager = &resp
		p.newListByUserPager.add(req, newListByUserPager)
		server.PagerResponderInjectNextLinks(newListByUserPager, req, func(page *armbilling.PaymentMethodsClientListByUserResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByUserPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		p.newListByUserPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByUserPager) {
		p.newListByUserPager.remove(req)
	}
	return resp, nil
}
