// 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) Go Code Generator. DO NOT EDIT.

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/monitor/armmonitor"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// BaselinesServer is a fake server for instances of the armmonitor.BaselinesClient type.
type BaselinesServer struct {
	// NewListPager is the fake for method BaselinesClient.NewListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListPager func(resourceURI string, options *armmonitor.BaselinesClientListOptions) (resp azfake.PagerResponder[armmonitor.BaselinesClientListResponse])
}

// NewBaselinesServerTransport creates a new instance of BaselinesServerTransport with the provided implementation.
// The returned BaselinesServerTransport instance is connected to an instance of armmonitor.BaselinesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewBaselinesServerTransport(srv *BaselinesServer) *BaselinesServerTransport {
	return &BaselinesServerTransport{
		srv:          srv,
		newListPager: newTracker[azfake.PagerResponder[armmonitor.BaselinesClientListResponse]](),
	}
}

// BaselinesServerTransport connects instances of armmonitor.BaselinesClient to instances of BaselinesServer.
// Don't use this type directly, use NewBaselinesServerTransport instead.
type BaselinesServerTransport struct {
	srv          *BaselinesServer
	newListPager *tracker[azfake.PagerResponder[armmonitor.BaselinesClientListResponse]]
}

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

func (b *BaselinesServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if baselinesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = baselinesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "BaselinesClient.NewListPager":
				res.resp, res.err = b.dispatchNewListPager(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		resultChan <- res
	}()

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

func (b *BaselinesServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
	if b.srv.NewListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
	}
	newListPager := b.newListPager.get(req)
	if newListPager == nil {
		const regexStr = `/(?P<resourceUri>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Insights/metricBaselines`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 2 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		qp := req.URL.Query()
		resourceURIParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceUri")])
		if err != nil {
			return nil, err
		}
		metricnamesParam := getOptional(qp.Get("metricnames"))
		metricnamespaceParam := getOptional(qp.Get("metricnamespace"))
		timespanParam := getOptional(qp.Get("timespan"))
		intervalParam := getOptional(qp.Get("interval"))
		aggregationParam := getOptional(qp.Get("aggregation"))
		sensitivitiesParam := getOptional(qp.Get("sensitivities"))
		filterParam := getOptional(qp.Get("$filter"))
		resultTypeParam := getOptional(armmonitor.ResultType(qp.Get("resultType")))
		var options *armmonitor.BaselinesClientListOptions
		if metricnamesParam != nil || metricnamespaceParam != nil || timespanParam != nil || intervalParam != nil || aggregationParam != nil || sensitivitiesParam != nil || filterParam != nil || resultTypeParam != nil {
			options = &armmonitor.BaselinesClientListOptions{
				Metricnames:     metricnamesParam,
				Metricnamespace: metricnamespaceParam,
				Timespan:        timespanParam,
				Interval:        intervalParam,
				Aggregation:     aggregationParam,
				Sensitivities:   sensitivitiesParam,
				Filter:          filterParam,
				ResultType:      resultTypeParam,
			}
		}
		resp := b.srv.NewListPager(resourceURIParam, options)
		newListPager = &resp
		b.newListPager.add(req, newListPager)
		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armmonitor.BaselinesClientListResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		b.newListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListPager) {
		b.newListPager.remove(req)
	}
	return resp, nil
}

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