// 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/workloadorchestration/armworkloadorchestration"
	"net/http"
	"net/url"
	"regexp"
)

// WorkflowVersionsServer is a fake server for instances of the armworkloadorchestration.WorkflowVersionsClient type.
type WorkflowVersionsServer struct {
	// BeginCreateOrUpdate is the fake for method WorkflowVersionsClient.BeginCreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, resource armworkloadorchestration.WorkflowVersion, options *armworkloadorchestration.WorkflowVersionsClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armworkloadorchestration.WorkflowVersionsClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method WorkflowVersionsClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, options *armworkloadorchestration.WorkflowVersionsClientBeginDeleteOptions) (resp azfake.PollerResponder[armworkloadorchestration.WorkflowVersionsClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method WorkflowVersionsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, options *armworkloadorchestration.WorkflowVersionsClientGetOptions) (resp azfake.Responder[armworkloadorchestration.WorkflowVersionsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByWorkflowPager is the fake for method WorkflowVersionsClient.NewListByWorkflowPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByWorkflowPager func(resourceGroupName string, contextName string, workflowName string, options *armworkloadorchestration.WorkflowVersionsClientListByWorkflowOptions) (resp azfake.PagerResponder[armworkloadorchestration.WorkflowVersionsClientListByWorkflowResponse])

	// BeginUpdate is the fake for method WorkflowVersionsClient.BeginUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginUpdate func(ctx context.Context, resourceGroupName string, contextName string, workflowName string, versionName string, properties armworkloadorchestration.WorkflowVersion, options *armworkloadorchestration.WorkflowVersionsClientBeginUpdateOptions) (resp azfake.PollerResponder[armworkloadorchestration.WorkflowVersionsClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewWorkflowVersionsServerTransport creates a new instance of WorkflowVersionsServerTransport with the provided implementation.
// The returned WorkflowVersionsServerTransport instance is connected to an instance of armworkloadorchestration.WorkflowVersionsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewWorkflowVersionsServerTransport(srv *WorkflowVersionsServer) *WorkflowVersionsServerTransport {
	return &WorkflowVersionsServerTransport{
		srv:                    srv,
		beginCreateOrUpdate:    newTracker[azfake.PollerResponder[armworkloadorchestration.WorkflowVersionsClientCreateOrUpdateResponse]](),
		beginDelete:            newTracker[azfake.PollerResponder[armworkloadorchestration.WorkflowVersionsClientDeleteResponse]](),
		newListByWorkflowPager: newTracker[azfake.PagerResponder[armworkloadorchestration.WorkflowVersionsClientListByWorkflowResponse]](),
		beginUpdate:            newTracker[azfake.PollerResponder[armworkloadorchestration.WorkflowVersionsClientUpdateResponse]](),
	}
}

// WorkflowVersionsServerTransport connects instances of armworkloadorchestration.WorkflowVersionsClient to instances of WorkflowVersionsServer.
// Don't use this type directly, use NewWorkflowVersionsServerTransport instead.
type WorkflowVersionsServerTransport struct {
	srv                    *WorkflowVersionsServer
	beginCreateOrUpdate    *tracker[azfake.PollerResponder[armworkloadorchestration.WorkflowVersionsClientCreateOrUpdateResponse]]
	beginDelete            *tracker[azfake.PollerResponder[armworkloadorchestration.WorkflowVersionsClientDeleteResponse]]
	newListByWorkflowPager *tracker[azfake.PagerResponder[armworkloadorchestration.WorkflowVersionsClientListByWorkflowResponse]]
	beginUpdate            *tracker[azfake.PollerResponder[armworkloadorchestration.WorkflowVersionsClientUpdateResponse]]
}

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

func (w *WorkflowVersionsServerTransport) 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 workflowVersionsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = workflowVersionsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "WorkflowVersionsClient.BeginCreateOrUpdate":
				res.resp, res.err = w.dispatchBeginCreateOrUpdate(req)
			case "WorkflowVersionsClient.BeginDelete":
				res.resp, res.err = w.dispatchBeginDelete(req)
			case "WorkflowVersionsClient.Get":
				res.resp, res.err = w.dispatchGet(req)
			case "WorkflowVersionsClient.NewListByWorkflowPager":
				res.resp, res.err = w.dispatchNewListByWorkflowPager(req)
			case "WorkflowVersionsClient.BeginUpdate":
				res.resp, res.err = w.dispatchBeginUpdate(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 (w *WorkflowVersionsServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if w.srv.BeginCreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")}
	}
	beginCreateOrUpdate := w.beginCreateOrUpdate.get(req)
	if beginCreateOrUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/contexts/(?P<contextName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/workflows/(?P<workflowName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<versionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 6 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		body, err := server.UnmarshalRequestAsJSON[armworkloadorchestration.WorkflowVersion](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		contextNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("contextName")])
		if err != nil {
			return nil, err
		}
		workflowNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workflowName")])
		if err != nil {
			return nil, err
		}
		versionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("versionName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := w.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, contextNameParam, workflowNameParam, versionNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOrUpdate = &respr
		w.beginCreateOrUpdate.add(req, beginCreateOrUpdate)
	}

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

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

	return resp, nil
}

func (w *WorkflowVersionsServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if w.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := w.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/contexts/(?P<contextName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/workflows/(?P<workflowName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<versionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 6 {
			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
		}
		contextNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("contextName")])
		if err != nil {
			return nil, err
		}
		workflowNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workflowName")])
		if err != nil {
			return nil, err
		}
		versionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("versionName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := w.srv.BeginDelete(req.Context(), resourceGroupNameParam, contextNameParam, workflowNameParam, versionNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		w.beginDelete.add(req, beginDelete)
	}

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

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

	return resp, nil
}

func (w *WorkflowVersionsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if w.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\.Edge/contexts/(?P<contextName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/workflows/(?P<workflowName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<versionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 6 {
		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
	}
	contextNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("contextName")])
	if err != nil {
		return nil, err
	}
	workflowNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workflowName")])
	if err != nil {
		return nil, err
	}
	versionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("versionName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := w.srv.Get(req.Context(), resourceGroupNameParam, contextNameParam, workflowNameParam, versionNameParam, 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).WorkflowVersion, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (w *WorkflowVersionsServerTransport) dispatchNewListByWorkflowPager(req *http.Request) (*http.Response, error) {
	if w.srv.NewListByWorkflowPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByWorkflowPager not implemented")}
	}
	newListByWorkflowPager := w.newListByWorkflowPager.get(req)
	if newListByWorkflowPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/contexts/(?P<contextName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/workflows/(?P<workflowName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions`
		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
		}
		contextNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("contextName")])
		if err != nil {
			return nil, err
		}
		workflowNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workflowName")])
		if err != nil {
			return nil, err
		}
		resp := w.srv.NewListByWorkflowPager(resourceGroupNameParam, contextNameParam, workflowNameParam, nil)
		newListByWorkflowPager = &resp
		w.newListByWorkflowPager.add(req, newListByWorkflowPager)
		server.PagerResponderInjectNextLinks(newListByWorkflowPager, req, func(page *armworkloadorchestration.WorkflowVersionsClientListByWorkflowResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByWorkflowPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		w.newListByWorkflowPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByWorkflowPager) {
		w.newListByWorkflowPager.remove(req)
	}
	return resp, nil
}

func (w *WorkflowVersionsServerTransport) dispatchBeginUpdate(req *http.Request) (*http.Response, error) {
	if w.srv.BeginUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginUpdate not implemented")}
	}
	beginUpdate := w.beginUpdate.get(req)
	if beginUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Edge/contexts/(?P<contextName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/workflows/(?P<workflowName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<versionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 6 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		body, err := server.UnmarshalRequestAsJSON[armworkloadorchestration.WorkflowVersion](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		contextNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("contextName")])
		if err != nil {
			return nil, err
		}
		workflowNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workflowName")])
		if err != nil {
			return nil, err
		}
		versionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("versionName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := w.srv.BeginUpdate(req.Context(), resourceGroupNameParam, contextNameParam, workflowNameParam, versionNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginUpdate = &respr
		w.beginUpdate.add(req, beginUpdate)
	}

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

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

	return resp, nil
}

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