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

// RecipientTransfersServer is a fake server for instances of the armbilling.RecipientTransfersClient type.
type RecipientTransfersServer struct {
	// Accept is the fake for method RecipientTransfersClient.Accept
	// HTTP status codes to indicate success: http.StatusOK
	Accept func(ctx context.Context, transferName string, parameters armbilling.AcceptTransferRequest, options *armbilling.RecipientTransfersClientAcceptOptions) (resp azfake.Responder[armbilling.RecipientTransfersClientAcceptResponse], errResp azfake.ErrorResponder)

	// Decline is the fake for method RecipientTransfersClient.Decline
	// HTTP status codes to indicate success: http.StatusOK
	Decline func(ctx context.Context, transferName string, options *armbilling.RecipientTransfersClientDeclineOptions) (resp azfake.Responder[armbilling.RecipientTransfersClientDeclineResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method RecipientTransfersClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, transferName string, options *armbilling.RecipientTransfersClientGetOptions) (resp azfake.Responder[armbilling.RecipientTransfersClientGetResponse], errResp azfake.ErrorResponder)

	// NewListPager is the fake for method RecipientTransfersClient.NewListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListPager func(options *armbilling.RecipientTransfersClientListOptions) (resp azfake.PagerResponder[armbilling.RecipientTransfersClientListResponse])

	// Validate is the fake for method RecipientTransfersClient.Validate
	// HTTP status codes to indicate success: http.StatusOK
	Validate func(ctx context.Context, transferName string, parameters armbilling.AcceptTransferRequest, options *armbilling.RecipientTransfersClientValidateOptions) (resp azfake.Responder[armbilling.RecipientTransfersClientValidateResponse], errResp azfake.ErrorResponder)
}

// NewRecipientTransfersServerTransport creates a new instance of RecipientTransfersServerTransport with the provided implementation.
// The returned RecipientTransfersServerTransport instance is connected to an instance of armbilling.RecipientTransfersClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewRecipientTransfersServerTransport(srv *RecipientTransfersServer) *RecipientTransfersServerTransport {
	return &RecipientTransfersServerTransport{
		srv:          srv,
		newListPager: newTracker[azfake.PagerResponder[armbilling.RecipientTransfersClientListResponse]](),
	}
}

// RecipientTransfersServerTransport connects instances of armbilling.RecipientTransfersClient to instances of RecipientTransfersServer.
// Don't use this type directly, use NewRecipientTransfersServerTransport instead.
type RecipientTransfersServerTransport struct {
	srv          *RecipientTransfersServer
	newListPager *tracker[azfake.PagerResponder[armbilling.RecipientTransfersClientListResponse]]
}

// Do implements the policy.Transporter interface for RecipientTransfersServerTransport.
func (r *RecipientTransfersServerTransport) 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 "RecipientTransfersClient.Accept":
		resp, err = r.dispatchAccept(req)
	case "RecipientTransfersClient.Decline":
		resp, err = r.dispatchDecline(req)
	case "RecipientTransfersClient.Get":
		resp, err = r.dispatchGet(req)
	case "RecipientTransfersClient.NewListPager":
		resp, err = r.dispatchNewListPager(req)
	case "RecipientTransfersClient.Validate":
		resp, err = r.dispatchValidate(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (r *RecipientTransfersServerTransport) dispatchAccept(req *http.Request) (*http.Response, error) {
	if r.srv.Accept == nil {
		return nil, &nonRetriableError{errors.New("fake for method Accept not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Billing/transfers/(?P<transferName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/accept`
	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)
	}
	body, err := server.UnmarshalRequestAsJSON[armbilling.AcceptTransferRequest](req)
	if err != nil {
		return nil, err
	}
	transferNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("transferName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := r.srv.Accept(req.Context(), transferNameParam, body, 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).RecipientTransferDetails, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (r *RecipientTransfersServerTransport) dispatchDecline(req *http.Request) (*http.Response, error) {
	if r.srv.Decline == nil {
		return nil, &nonRetriableError{errors.New("fake for method Decline not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Billing/transfers/(?P<transferName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/decline`
	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)
	}
	transferNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("transferName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := r.srv.Decline(req.Context(), transferNameParam, 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).RecipientTransferDetails, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (r *RecipientTransfersServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if r.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Billing/transfers/(?P<transferName>[!#&$-;=?-\[\]_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)
	}
	transferNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("transferName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := r.srv.Get(req.Context(), transferNameParam, 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).RecipientTransferDetails, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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

func (r *RecipientTransfersServerTransport) dispatchValidate(req *http.Request) (*http.Response, error) {
	if r.srv.Validate == nil {
		return nil, &nonRetriableError{errors.New("fake for method Validate not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Billing/transfers/(?P<transferName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/validate`
	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)
	}
	body, err := server.UnmarshalRequestAsJSON[armbilling.AcceptTransferRequest](req)
	if err != nil {
		return nil, err
	}
	transferNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("transferName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := r.srv.Validate(req.Context(), transferNameParam, body, 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).ValidateTransferListResponse, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
