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

// AksCostDetailOperationsServer is a fake server for instances of the armmigrationassessment.AksCostDetailOperationsClient type.
type AksCostDetailOperationsServer struct {
	// NewListByAksAssessmentPager is the fake for method AksCostDetailOperationsClient.NewListByAksAssessmentPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByAksAssessmentPager func(resourceGroupName string, projectName string, assessmentName string, options *armmigrationassessment.AksCostDetailOperationsClientListByAksAssessmentOptions) (resp azfake.PagerResponder[armmigrationassessment.AksCostDetailOperationsClientListByAksAssessmentResponse])
}

// NewAksCostDetailOperationsServerTransport creates a new instance of AksCostDetailOperationsServerTransport with the provided implementation.
// The returned AksCostDetailOperationsServerTransport instance is connected to an instance of armmigrationassessment.AksCostDetailOperationsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewAksCostDetailOperationsServerTransport(srv *AksCostDetailOperationsServer) *AksCostDetailOperationsServerTransport {
	return &AksCostDetailOperationsServerTransport{
		srv:                         srv,
		newListByAksAssessmentPager: newTracker[azfake.PagerResponder[armmigrationassessment.AksCostDetailOperationsClientListByAksAssessmentResponse]](),
	}
}

// AksCostDetailOperationsServerTransport connects instances of armmigrationassessment.AksCostDetailOperationsClient to instances of AksCostDetailOperationsServer.
// Don't use this type directly, use NewAksCostDetailOperationsServerTransport instead.
type AksCostDetailOperationsServerTransport struct {
	srv                         *AksCostDetailOperationsServer
	newListByAksAssessmentPager *tracker[azfake.PagerResponder[armmigrationassessment.AksCostDetailOperationsClientListByAksAssessmentResponse]]
}

// Do implements the policy.Transporter interface for AksCostDetailOperationsServerTransport.
func (a *AksCostDetailOperationsServerTransport) 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 *AksCostDetailOperationsServerTransport) 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 aksCostDetailOperationsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = aksCostDetailOperationsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "AksCostDetailOperationsClient.NewListByAksAssessmentPager":
				res.resp, res.err = a.dispatchNewListByAksAssessmentPager(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 *AksCostDetailOperationsServerTransport) dispatchNewListByAksAssessmentPager(req *http.Request) (*http.Response, error) {
	if a.srv.NewListByAksAssessmentPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByAksAssessmentPager not implemented")}
	}
	newListByAksAssessmentPager := a.newListByAksAssessmentPager.get(req)
	if newListByAksAssessmentPager == 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~%@]+)/aksAssessments/(?P<assessmentName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/costDetails`
		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)
		}
		qp := req.URL.Query()
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		filterUnescaped, err := url.QueryUnescape(qp.Get("$filter"))
		if err != nil {
			return nil, err
		}
		filterParam := getOptional(filterUnescaped)
		projectNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("projectName")])
		if err != nil {
			return nil, err
		}
		assessmentNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("assessmentName")])
		if err != nil {
			return nil, err
		}
		var options *armmigrationassessment.AksCostDetailOperationsClientListByAksAssessmentOptions
		if filterParam != nil {
			options = &armmigrationassessment.AksCostDetailOperationsClientListByAksAssessmentOptions{
				Filter: filterParam,
			}
		}
		resp := a.srv.NewListByAksAssessmentPager(resourceGroupNameParam, projectNameParam, assessmentNameParam, options)
		newListByAksAssessmentPager = &resp
		a.newListByAksAssessmentPager.add(req, newListByAksAssessmentPager)
		server.PagerResponderInjectNextLinks(newListByAksAssessmentPager, req, func(page *armmigrationassessment.AksCostDetailOperationsClientListByAksAssessmentResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByAksAssessmentPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		a.newListByAksAssessmentPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByAksAssessmentPager) {
		a.newListByAksAssessmentPager.remove(req)
	}
	return resp, nil
}

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