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

// ScheduledActionExtensionServer is a fake server for instances of the armcomputeschedule.ScheduledActionExtensionClient type.
type ScheduledActionExtensionServer struct {
	// NewListByVMsPager is the fake for method ScheduledActionExtensionClient.NewListByVMsPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByVMsPager func(resourceURI string, options *armcomputeschedule.ScheduledActionExtensionClientListByVMsOptions) (resp azfake.PagerResponder[armcomputeschedule.ScheduledActionExtensionClientListByVMsResponse])
}

// NewScheduledActionExtensionServerTransport creates a new instance of ScheduledActionExtensionServerTransport with the provided implementation.
// The returned ScheduledActionExtensionServerTransport instance is connected to an instance of armcomputeschedule.ScheduledActionExtensionClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewScheduledActionExtensionServerTransport(srv *ScheduledActionExtensionServer) *ScheduledActionExtensionServerTransport {
	return &ScheduledActionExtensionServerTransport{
		srv:               srv,
		newListByVMsPager: newTracker[azfake.PagerResponder[armcomputeschedule.ScheduledActionExtensionClientListByVMsResponse]](),
	}
}

// ScheduledActionExtensionServerTransport connects instances of armcomputeschedule.ScheduledActionExtensionClient to instances of ScheduledActionExtensionServer.
// Don't use this type directly, use NewScheduledActionExtensionServerTransport instead.
type ScheduledActionExtensionServerTransport struct {
	srv               *ScheduledActionExtensionServer
	newListByVMsPager *tracker[azfake.PagerResponder[armcomputeschedule.ScheduledActionExtensionClientListByVMsResponse]]
}

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

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

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