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

// OccurrencesServer is a fake server for instances of the armcomputeschedule.OccurrencesClient type.
type OccurrencesServer struct {
	// Cancel is the fake for method OccurrencesClient.Cancel
	// HTTP status codes to indicate success: http.StatusOK
	Cancel func(ctx context.Context, resourceGroupName string, scheduledActionName string, occurrenceID string, body armcomputeschedule.CancelOccurrenceRequest, options *armcomputeschedule.OccurrencesClientCancelOptions) (resp azfake.Responder[armcomputeschedule.OccurrencesClientCancelResponse], errResp azfake.ErrorResponder)

	// BeginDelay is the fake for method OccurrencesClient.BeginDelay
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginDelay func(ctx context.Context, resourceGroupName string, scheduledActionName string, occurrenceID string, body armcomputeschedule.DelayRequest, options *armcomputeschedule.OccurrencesClientBeginDelayOptions) (resp azfake.PollerResponder[armcomputeschedule.OccurrencesClientDelayResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method OccurrencesClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, scheduledActionName string, occurrenceID string, options *armcomputeschedule.OccurrencesClientGetOptions) (resp azfake.Responder[armcomputeschedule.OccurrencesClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByScheduledActionPager is the fake for method OccurrencesClient.NewListByScheduledActionPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByScheduledActionPager func(resourceGroupName string, scheduledActionName string, options *armcomputeschedule.OccurrencesClientListByScheduledActionOptions) (resp azfake.PagerResponder[armcomputeschedule.OccurrencesClientListByScheduledActionResponse])

	// NewListResourcesPager is the fake for method OccurrencesClient.NewListResourcesPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListResourcesPager func(resourceGroupName string, scheduledActionName string, occurrenceID string, options *armcomputeschedule.OccurrencesClientListResourcesOptions) (resp azfake.PagerResponder[armcomputeschedule.OccurrencesClientListResourcesResponse])
}

// NewOccurrencesServerTransport creates a new instance of OccurrencesServerTransport with the provided implementation.
// The returned OccurrencesServerTransport instance is connected to an instance of armcomputeschedule.OccurrencesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewOccurrencesServerTransport(srv *OccurrencesServer) *OccurrencesServerTransport {
	return &OccurrencesServerTransport{
		srv:                           srv,
		beginDelay:                    newTracker[azfake.PollerResponder[armcomputeschedule.OccurrencesClientDelayResponse]](),
		newListByScheduledActionPager: newTracker[azfake.PagerResponder[armcomputeschedule.OccurrencesClientListByScheduledActionResponse]](),
		newListResourcesPager:         newTracker[azfake.PagerResponder[armcomputeschedule.OccurrencesClientListResourcesResponse]](),
	}
}

// OccurrencesServerTransport connects instances of armcomputeschedule.OccurrencesClient to instances of OccurrencesServer.
// Don't use this type directly, use NewOccurrencesServerTransport instead.
type OccurrencesServerTransport struct {
	srv                           *OccurrencesServer
	beginDelay                    *tracker[azfake.PollerResponder[armcomputeschedule.OccurrencesClientDelayResponse]]
	newListByScheduledActionPager *tracker[azfake.PagerResponder[armcomputeschedule.OccurrencesClientListByScheduledActionResponse]]
	newListResourcesPager         *tracker[azfake.PagerResponder[armcomputeschedule.OccurrencesClientListResourcesResponse]]
}

// Do implements the policy.Transporter interface for OccurrencesServerTransport.
func (o *OccurrencesServerTransport) 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 *OccurrencesServerTransport) 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 occurrencesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = occurrencesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "OccurrencesClient.Cancel":
				res.resp, res.err = o.dispatchCancel(req)
			case "OccurrencesClient.BeginDelay":
				res.resp, res.err = o.dispatchBeginDelay(req)
			case "OccurrencesClient.Get":
				res.resp, res.err = o.dispatchGet(req)
			case "OccurrencesClient.NewListByScheduledActionPager":
				res.resp, res.err = o.dispatchNewListByScheduledActionPager(req)
			case "OccurrencesClient.NewListResourcesPager":
				res.resp, res.err = o.dispatchNewListResourcesPager(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 *OccurrencesServerTransport) dispatchCancel(req *http.Request) (*http.Response, error) {
	if o.srv.Cancel == nil {
		return nil, &nonRetriableError{errors.New("fake for method Cancel not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ComputeSchedule/scheduledActions/(?P<scheduledActionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/occurrences/(?P<occurrenceId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/cancel`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 5 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	body, err := server.UnmarshalRequestAsJSON[armcomputeschedule.CancelOccurrenceRequest](req)
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	scheduledActionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("scheduledActionName")])
	if err != nil {
		return nil, err
	}
	occurrenceIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("occurrenceId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := o.srv.Cancel(req.Context(), resourceGroupNameParam, scheduledActionNameParam, occurrenceIDParam, body, 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).RecurringActionsResourceOperationResult, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (o *OccurrencesServerTransport) dispatchBeginDelay(req *http.Request) (*http.Response, error) {
	if o.srv.BeginDelay == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelay not implemented")}
	}
	beginDelay := o.beginDelay.get(req)
	if beginDelay == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ComputeSchedule/scheduledActions/(?P<scheduledActionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/occurrences/(?P<occurrenceId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/delay`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 5 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		body, err := server.UnmarshalRequestAsJSON[armcomputeschedule.DelayRequest](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		scheduledActionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("scheduledActionName")])
		if err != nil {
			return nil, err
		}
		occurrenceIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("occurrenceId")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := o.srv.BeginDelay(req.Context(), resourceGroupNameParam, scheduledActionNameParam, occurrenceIDParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelay = &respr
		o.beginDelay.add(req, beginDelay)
	}

	resp, err := server.PollerResponderNext(beginDelay, req)
	if err != nil {
		return nil, err
	}

	if !contains([]int{http.StatusOK, http.StatusAccepted}, resp.StatusCode) {
		o.beginDelay.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginDelay) {
		o.beginDelay.remove(req)
	}

	return resp, nil
}

func (o *OccurrencesServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if o.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\.ComputeSchedule/scheduledActions/(?P<scheduledActionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/occurrences/(?P<occurrenceId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 5 {
		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
	}
	scheduledActionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("scheduledActionName")])
	if err != nil {
		return nil, err
	}
	occurrenceIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("occurrenceId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := o.srv.Get(req.Context(), resourceGroupNameParam, scheduledActionNameParam, occurrenceIDParam, 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).Occurrence, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (o *OccurrencesServerTransport) dispatchNewListByScheduledActionPager(req *http.Request) (*http.Response, error) {
	if o.srv.NewListByScheduledActionPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByScheduledActionPager not implemented")}
	}
	newListByScheduledActionPager := o.newListByScheduledActionPager.get(req)
	if newListByScheduledActionPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ComputeSchedule/scheduledActions/(?P<scheduledActionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/occurrences`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if 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
		}
		scheduledActionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("scheduledActionName")])
		if err != nil {
			return nil, err
		}
		resp := o.srv.NewListByScheduledActionPager(resourceGroupNameParam, scheduledActionNameParam, nil)
		newListByScheduledActionPager = &resp
		o.newListByScheduledActionPager.add(req, newListByScheduledActionPager)
		server.PagerResponderInjectNextLinks(newListByScheduledActionPager, req, func(page *armcomputeschedule.OccurrencesClientListByScheduledActionResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByScheduledActionPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		o.newListByScheduledActionPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByScheduledActionPager) {
		o.newListByScheduledActionPager.remove(req)
	}
	return resp, nil
}

func (o *OccurrencesServerTransport) dispatchNewListResourcesPager(req *http.Request) (*http.Response, error) {
	if o.srv.NewListResourcesPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListResourcesPager not implemented")}
	}
	newListResourcesPager := o.newListResourcesPager.get(req)
	if newListResourcesPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ComputeSchedule/scheduledActions/(?P<scheduledActionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/occurrences/(?P<occurrenceId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resources`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 5 {
			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
		}
		scheduledActionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("scheduledActionName")])
		if err != nil {
			return nil, err
		}
		occurrenceIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("occurrenceId")])
		if err != nil {
			return nil, err
		}
		resp := o.srv.NewListResourcesPager(resourceGroupNameParam, scheduledActionNameParam, occurrenceIDParam, nil)
		newListResourcesPager = &resp
		o.newListResourcesPager.add(req, newListResourcesPager)
		server.PagerResponderInjectNextLinks(newListResourcesPager, req, func(page *armcomputeschedule.OccurrencesClientListResourcesResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListResourcesPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		o.newListResourcesPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListResourcesPager) {
		o.newListResourcesPager.remove(req)
	}
	return resp, nil
}

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