// 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"
	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/migrationassessment/armmigrationassessment"
	"net/http"
	"net/url"
	"regexp"
)

// AvsAssessmentOptionsOperationsServer is a fake server for instances of the armmigrationassessment.AvsAssessmentOptionsOperationsClient type.
type AvsAssessmentOptionsOperationsServer struct {
	// Get is the fake for method AvsAssessmentOptionsOperationsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, projectName string, avsAssessmentOptionsName string, options *armmigrationassessment.AvsAssessmentOptionsOperationsClientGetOptions) (resp azfake.Responder[armmigrationassessment.AvsAssessmentOptionsOperationsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByAssessmentProjectPager is the fake for method AvsAssessmentOptionsOperationsClient.NewListByAssessmentProjectPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByAssessmentProjectPager func(resourceGroupName string, projectName string, options *armmigrationassessment.AvsAssessmentOptionsOperationsClientListByAssessmentProjectOptions) (resp azfake.PagerResponder[armmigrationassessment.AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse])
}

// NewAvsAssessmentOptionsOperationsServerTransport creates a new instance of AvsAssessmentOptionsOperationsServerTransport with the provided implementation.
// The returned AvsAssessmentOptionsOperationsServerTransport instance is connected to an instance of armmigrationassessment.AvsAssessmentOptionsOperationsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewAvsAssessmentOptionsOperationsServerTransport(srv *AvsAssessmentOptionsOperationsServer) *AvsAssessmentOptionsOperationsServerTransport {
	return &AvsAssessmentOptionsOperationsServerTransport{
		srv:                             srv,
		newListByAssessmentProjectPager: newTracker[azfake.PagerResponder[armmigrationassessment.AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse]](),
	}
}

// AvsAssessmentOptionsOperationsServerTransport connects instances of armmigrationassessment.AvsAssessmentOptionsOperationsClient to instances of AvsAssessmentOptionsOperationsServer.
// Don't use this type directly, use NewAvsAssessmentOptionsOperationsServerTransport instead.
type AvsAssessmentOptionsOperationsServerTransport struct {
	srv                             *AvsAssessmentOptionsOperationsServer
	newListByAssessmentProjectPager *tracker[azfake.PagerResponder[armmigrationassessment.AvsAssessmentOptionsOperationsClientListByAssessmentProjectResponse]]
}

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

func (a *AvsAssessmentOptionsOperationsServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result)
	defer close(resultChan)

	go func() {
		var intercepted bool
		var res result
		if avsAssessmentOptionsOperationsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = avsAssessmentOptionsOperationsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "AvsAssessmentOptionsOperationsClient.Get":
				res.resp, res.err = a.dispatchGet(req)
			case "AvsAssessmentOptionsOperationsClient.NewListByAssessmentProjectPager":
				res.resp, res.err = a.dispatchNewListByAssessmentProjectPager(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		select {
		case resultChan <- res:
		case <-req.Context().Done():
		}
	}()

	select {
	case <-req.Context().Done():
		return nil, req.Context().Err()
	case res := <-resultChan:
		return res.resp, res.err
	}
}

func (a *AvsAssessmentOptionsOperationsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if a.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Migrate/assessmentProjects/(?P<projectName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/avsAssessmentOptions/(?P<avsAssessmentOptionsName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || 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
	}
	projectNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("projectName")])
	if err != nil {
		return nil, err
	}
	avsAssessmentOptionsNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("avsAssessmentOptionsName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := a.srv.Get(req.Context(), resourceGroupNameParam, projectNameParam, avsAssessmentOptionsNameParam, 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).AvsAssessmentOptions, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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

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