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

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

// EnrollmentAccountsServer is a fake server for instances of the armbilling.EnrollmentAccountsClient type.
type EnrollmentAccountsServer struct {
	// Get is the fake for method EnrollmentAccountsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, billingAccountName string, enrollmentAccountName string, options *armbilling.EnrollmentAccountsClientGetOptions) (resp azfake.Responder[armbilling.EnrollmentAccountsClientGetResponse], errResp azfake.ErrorResponder)

	// GetByDepartment is the fake for method EnrollmentAccountsClient.GetByDepartment
	// HTTP status codes to indicate success: http.StatusOK
	GetByDepartment func(ctx context.Context, billingAccountName string, departmentName string, enrollmentAccountName string, options *armbilling.EnrollmentAccountsClientGetByDepartmentOptions) (resp azfake.Responder[armbilling.EnrollmentAccountsClientGetByDepartmentResponse], errResp azfake.ErrorResponder)

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

	// NewListByDepartmentPager is the fake for method EnrollmentAccountsClient.NewListByDepartmentPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByDepartmentPager func(billingAccountName string, departmentName string, options *armbilling.EnrollmentAccountsClientListByDepartmentOptions) (resp azfake.PagerResponder[armbilling.EnrollmentAccountsClientListByDepartmentResponse])
}

// NewEnrollmentAccountsServerTransport creates a new instance of EnrollmentAccountsServerTransport with the provided implementation.
// The returned EnrollmentAccountsServerTransport instance is connected to an instance of armbilling.EnrollmentAccountsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewEnrollmentAccountsServerTransport(srv *EnrollmentAccountsServer) *EnrollmentAccountsServerTransport {
	return &EnrollmentAccountsServerTransport{
		srv:                          srv,
		newListByBillingAccountPager: newTracker[azfake.PagerResponder[armbilling.EnrollmentAccountsClientListByBillingAccountResponse]](),
		newListByDepartmentPager:     newTracker[azfake.PagerResponder[armbilling.EnrollmentAccountsClientListByDepartmentResponse]](),
	}
}

// EnrollmentAccountsServerTransport connects instances of armbilling.EnrollmentAccountsClient to instances of EnrollmentAccountsServer.
// Don't use this type directly, use NewEnrollmentAccountsServerTransport instead.
type EnrollmentAccountsServerTransport struct {
	srv                          *EnrollmentAccountsServer
	newListByBillingAccountPager *tracker[azfake.PagerResponder[armbilling.EnrollmentAccountsClientListByBillingAccountResponse]]
	newListByDepartmentPager     *tracker[azfake.PagerResponder[armbilling.EnrollmentAccountsClientListByDepartmentResponse]]
}

// Do implements the policy.Transporter interface for EnrollmentAccountsServerTransport.
func (e *EnrollmentAccountsServerTransport) 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 "EnrollmentAccountsClient.Get":
		resp, err = e.dispatchGet(req)
	case "EnrollmentAccountsClient.GetByDepartment":
		resp, err = e.dispatchGetByDepartment(req)
	case "EnrollmentAccountsClient.NewListByBillingAccountPager":
		resp, err = e.dispatchNewListByBillingAccountPager(req)
	case "EnrollmentAccountsClient.NewListByDepartmentPager":
		resp, err = e.dispatchNewListByDepartmentPager(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (e *EnrollmentAccountsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if e.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Billing/billingAccounts/(?P<billingAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/enrollmentAccounts/(?P<enrollmentAccountName>[!#&$-;=?-\[\]_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
	}
	enrollmentAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("enrollmentAccountName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := e.srv.Get(req.Context(), billingAccountNameParam, enrollmentAccountNameParam, 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).EnrollmentAccount, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (e *EnrollmentAccountsServerTransport) dispatchGetByDepartment(req *http.Request) (*http.Response, error) {
	if e.srv.GetByDepartment == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetByDepartment not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Billing/billingAccounts/(?P<billingAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/departments/(?P<departmentName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/enrollmentAccounts/(?P<enrollmentAccountName>[!#&$-;=?-\[\]_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
	}
	departmentNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("departmentName")])
	if err != nil {
		return nil, err
	}
	enrollmentAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("enrollmentAccountName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := e.srv.GetByDepartment(req.Context(), billingAccountNameParam, departmentNameParam, enrollmentAccountNameParam, 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).EnrollmentAccount, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (e *EnrollmentAccountsServerTransport) dispatchNewListByBillingAccountPager(req *http.Request) (*http.Response, error) {
	if e.srv.NewListByBillingAccountPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByBillingAccountPager not implemented")}
	}
	newListByBillingAccountPager := e.newListByBillingAccountPager.get(req)
	if newListByBillingAccountPager == nil {
		const regexStr = `/providers/Microsoft\.Billing/billingAccounts/(?P<billingAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/enrollmentAccounts`
		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)
		}
		qp := req.URL.Query()
		billingAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("billingAccountName")])
		if err != nil {
			return nil, err
		}
		filterUnescaped, err := url.QueryUnescape(qp.Get("filter"))
		if err != nil {
			return nil, err
		}
		filterParam := getOptional(filterUnescaped)
		orderByUnescaped, err := url.QueryUnescape(qp.Get("orderBy"))
		if err != nil {
			return nil, err
		}
		orderByParam := getOptional(orderByUnescaped)
		topUnescaped, err := url.QueryUnescape(qp.Get("top"))
		if err != nil {
			return nil, err
		}
		topParam, err := parseOptional(topUnescaped, func(v string) (int64, error) {
			p, parseErr := strconv.ParseInt(v, 10, 64)
			if parseErr != nil {
				return 0, parseErr
			}
			return p, nil
		})
		if err != nil {
			return nil, err
		}
		skipUnescaped, err := url.QueryUnescape(qp.Get("skip"))
		if err != nil {
			return nil, err
		}
		skipParam, err := parseOptional(skipUnescaped, func(v string) (int64, error) {
			p, parseErr := strconv.ParseInt(v, 10, 64)
			if parseErr != nil {
				return 0, parseErr
			}
			return p, nil
		})
		if err != nil {
			return nil, err
		}
		countUnescaped, err := url.QueryUnescape(qp.Get("count"))
		if err != nil {
			return nil, err
		}
		countParam, err := parseOptional(countUnescaped, strconv.ParseBool)
		if err != nil {
			return nil, err
		}
		searchUnescaped, err := url.QueryUnescape(qp.Get("search"))
		if err != nil {
			return nil, err
		}
		searchParam := getOptional(searchUnescaped)
		var options *armbilling.EnrollmentAccountsClientListByBillingAccountOptions
		if filterParam != nil || orderByParam != nil || topParam != nil || skipParam != nil || countParam != nil || searchParam != nil {
			options = &armbilling.EnrollmentAccountsClientListByBillingAccountOptions{
				Filter:  filterParam,
				OrderBy: orderByParam,
				Top:     topParam,
				Skip:    skipParam,
				Count:   countParam,
				Search:  searchParam,
			}
		}
		resp := e.srv.NewListByBillingAccountPager(billingAccountNameParam, options)
		newListByBillingAccountPager = &resp
		e.newListByBillingAccountPager.add(req, newListByBillingAccountPager)
		server.PagerResponderInjectNextLinks(newListByBillingAccountPager, req, func(page *armbilling.EnrollmentAccountsClientListByBillingAccountResponse, 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) {
		e.newListByBillingAccountPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByBillingAccountPager) {
		e.newListByBillingAccountPager.remove(req)
	}
	return resp, nil
}

func (e *EnrollmentAccountsServerTransport) dispatchNewListByDepartmentPager(req *http.Request) (*http.Response, error) {
	if e.srv.NewListByDepartmentPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByDepartmentPager not implemented")}
	}
	newListByDepartmentPager := e.newListByDepartmentPager.get(req)
	if newListByDepartmentPager == nil {
		const regexStr = `/providers/Microsoft\.Billing/billingAccounts/(?P<billingAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/departments/(?P<departmentName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/enrollmentAccounts`
		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)
		}
		qp := req.URL.Query()
		billingAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("billingAccountName")])
		if err != nil {
			return nil, err
		}
		departmentNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("departmentName")])
		if err != nil {
			return nil, err
		}
		filterUnescaped, err := url.QueryUnescape(qp.Get("filter"))
		if err != nil {
			return nil, err
		}
		filterParam := getOptional(filterUnescaped)
		orderByUnescaped, err := url.QueryUnescape(qp.Get("orderBy"))
		if err != nil {
			return nil, err
		}
		orderByParam := getOptional(orderByUnescaped)
		topUnescaped, err := url.QueryUnescape(qp.Get("top"))
		if err != nil {
			return nil, err
		}
		topParam, err := parseOptional(topUnescaped, func(v string) (int64, error) {
			p, parseErr := strconv.ParseInt(v, 10, 64)
			if parseErr != nil {
				return 0, parseErr
			}
			return p, nil
		})
		if err != nil {
			return nil, err
		}
		skipUnescaped, err := url.QueryUnescape(qp.Get("skip"))
		if err != nil {
			return nil, err
		}
		skipParam, err := parseOptional(skipUnescaped, func(v string) (int64, error) {
			p, parseErr := strconv.ParseInt(v, 10, 64)
			if parseErr != nil {
				return 0, parseErr
			}
			return p, nil
		})
		if err != nil {
			return nil, err
		}
		countUnescaped, err := url.QueryUnescape(qp.Get("count"))
		if err != nil {
			return nil, err
		}
		countParam, err := parseOptional(countUnescaped, strconv.ParseBool)
		if err != nil {
			return nil, err
		}
		searchUnescaped, err := url.QueryUnescape(qp.Get("search"))
		if err != nil {
			return nil, err
		}
		searchParam := getOptional(searchUnescaped)
		var options *armbilling.EnrollmentAccountsClientListByDepartmentOptions
		if filterParam != nil || orderByParam != nil || topParam != nil || skipParam != nil || countParam != nil || searchParam != nil {
			options = &armbilling.EnrollmentAccountsClientListByDepartmentOptions{
				Filter:  filterParam,
				OrderBy: orderByParam,
				Top:     topParam,
				Skip:    skipParam,
				Count:   countParam,
				Search:  searchParam,
			}
		}
		resp := e.srv.NewListByDepartmentPager(billingAccountNameParam, departmentNameParam, options)
		newListByDepartmentPager = &resp
		e.newListByDepartmentPager.add(req, newListByDepartmentPager)
		server.PagerResponderInjectNextLinks(newListByDepartmentPager, req, func(page *armbilling.EnrollmentAccountsClientListByDepartmentResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByDepartmentPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		e.newListByDepartmentPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByDepartmentPager) {
		e.newListByDepartmentPager.remove(req)
	}
	return resp, nil
}
