// 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/computeschedule/armcomputeschedule"
	"net/http"
	"net/url"
	"regexp"
)

// OccurrenceExtensionServer is a fake server for instances of the armcomputeschedule.OccurrenceExtensionClient type.
type OccurrenceExtensionServer struct {
	// NewListOccurrenceByVMsPager is the fake for method OccurrenceExtensionClient.NewListOccurrenceByVMsPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListOccurrenceByVMsPager func(resourceURI string, options *armcomputeschedule.OccurrenceExtensionClientListOccurrenceByVMsOptions) (resp azfake.PagerResponder[armcomputeschedule.OccurrenceExtensionClientListOccurrenceByVMsResponse])
}

// NewOccurrenceExtensionServerTransport creates a new instance of OccurrenceExtensionServerTransport with the provided implementation.
// The returned OccurrenceExtensionServerTransport instance is connected to an instance of armcomputeschedule.OccurrenceExtensionClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewOccurrenceExtensionServerTransport(srv *OccurrenceExtensionServer) *OccurrenceExtensionServerTransport {
	return &OccurrenceExtensionServerTransport{
		srv:                         srv,
		newListOccurrenceByVMsPager: newTracker[azfake.PagerResponder[armcomputeschedule.OccurrenceExtensionClientListOccurrenceByVMsResponse]](),
	}
}

// OccurrenceExtensionServerTransport connects instances of armcomputeschedule.OccurrenceExtensionClient to instances of OccurrenceExtensionServer.
// Don't use this type directly, use NewOccurrenceExtensionServerTransport instead.
type OccurrenceExtensionServerTransport struct {
	srv                         *OccurrenceExtensionServer
	newListOccurrenceByVMsPager *tracker[azfake.PagerResponder[armcomputeschedule.OccurrenceExtensionClientListOccurrenceByVMsResponse]]
}

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

func (o *OccurrenceExtensionServerTransport) 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 occurrenceExtensionServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = occurrenceExtensionServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "OccurrenceExtensionClient.NewListOccurrenceByVMsPager":
				res.resp, res.err = o.dispatchNewListOccurrenceByVMsPager(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 (o *OccurrenceExtensionServerTransport) dispatchNewListOccurrenceByVMsPager(req *http.Request) (*http.Response, error) {
	if o.srv.NewListOccurrenceByVMsPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListOccurrenceByVMsPager not implemented")}
	}
	newListOccurrenceByVMsPager := o.newListOccurrenceByVMsPager.get(req)
	if newListOccurrenceByVMsPager == nil {
		const regexStr = `/(?P<resourceUri>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ComputeSchedule/associatedOccurrences`
		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)
		}
		resourceURIParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceUri")])
		if err != nil {
			return nil, err
		}
		resp := o.srv.NewListOccurrenceByVMsPager(resourceURIParam, nil)
		newListOccurrenceByVMsPager = &resp
		o.newListOccurrenceByVMsPager.add(req, newListOccurrenceByVMsPager)
		server.PagerResponderInjectNextLinks(newListOccurrenceByVMsPager, req, func(page *armcomputeschedule.OccurrenceExtensionClientListOccurrenceByVMsResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListOccurrenceByVMsPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		o.newListOccurrenceByVMsPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListOccurrenceByVMsPager) {
		o.newListOccurrenceByVMsPager.remove(req)
	}
	return resp, nil
}

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