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

// AvailableBalancesServer is a fake server for instances of the armbilling.AvailableBalancesClient type.
type AvailableBalancesServer struct {
	// GetByBillingAccount is the fake for method AvailableBalancesClient.GetByBillingAccount
	// HTTP status codes to indicate success: http.StatusOK
	GetByBillingAccount func(ctx context.Context, billingAccountName string, options *armbilling.AvailableBalancesClientGetByBillingAccountOptions) (resp azfake.Responder[armbilling.AvailableBalancesClientGetByBillingAccountResponse], errResp azfake.ErrorResponder)

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

// NewAvailableBalancesServerTransport creates a new instance of AvailableBalancesServerTransport with the provided implementation.
// The returned AvailableBalancesServerTransport instance is connected to an instance of armbilling.AvailableBalancesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewAvailableBalancesServerTransport(srv *AvailableBalancesServer) *AvailableBalancesServerTransport {
	return &AvailableBalancesServerTransport{srv: srv}
}

// AvailableBalancesServerTransport connects instances of armbilling.AvailableBalancesClient to instances of AvailableBalancesServer.
// Don't use this type directly, use NewAvailableBalancesServerTransport instead.
type AvailableBalancesServerTransport struct {
	srv *AvailableBalancesServer
}

// Do implements the policy.Transporter interface for AvailableBalancesServerTransport.
func (a *AvailableBalancesServerTransport) 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 "AvailableBalancesClient.GetByBillingAccount":
		resp, err = a.dispatchGetByBillingAccount(req)
	case "AvailableBalancesClient.GetByBillingProfile":
		resp, err = a.dispatchGetByBillingProfile(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (a *AvailableBalancesServerTransport) dispatchGetByBillingAccount(req *http.Request) (*http.Response, error) {
	if a.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~%@]+)/availableBalance/default`
	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
	}
	respr, errRespr := a.srv.GetByBillingAccount(req.Context(), billingAccountNameParam, 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).AvailableBalance, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (a *AvailableBalancesServerTransport) dispatchGetByBillingProfile(req *http.Request) (*http.Response, error) {
	if a.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~%@]+)/availableBalance/default`
	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
	}
	respr, errRespr := a.srv.GetByBillingProfile(req.Context(), billingAccountNameParam, billingProfileNameParam, 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).AvailableBalance, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
