// 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) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

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/storagecache/armstoragecache/v4"
	"net/http"
	"net/url"
	"regexp"
)

// AutoExportJobsServer is a fake server for instances of the armstoragecache.AutoExportJobsClient type.
type AutoExportJobsServer struct {
	// BeginCreateOrUpdate is the fake for method AutoExportJobsClient.BeginCreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, autoExportJob armstoragecache.AutoExportJob, options *armstoragecache.AutoExportJobsClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armstoragecache.AutoExportJobsClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method AutoExportJobsClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, options *armstoragecache.AutoExportJobsClientBeginDeleteOptions) (resp azfake.PollerResponder[armstoragecache.AutoExportJobsClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method AutoExportJobsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, options *armstoragecache.AutoExportJobsClientGetOptions) (resp azfake.Responder[armstoragecache.AutoExportJobsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByAmlFilesystemPager is the fake for method AutoExportJobsClient.NewListByAmlFilesystemPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByAmlFilesystemPager func(resourceGroupName string, amlFilesystemName string, options *armstoragecache.AutoExportJobsClientListByAmlFilesystemOptions) (resp azfake.PagerResponder[armstoragecache.AutoExportJobsClientListByAmlFilesystemResponse])

	// BeginUpdate is the fake for method AutoExportJobsClient.BeginUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginUpdate func(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, autoExportJob armstoragecache.AutoExportJobUpdate, options *armstoragecache.AutoExportJobsClientBeginUpdateOptions) (resp azfake.PollerResponder[armstoragecache.AutoExportJobsClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewAutoExportJobsServerTransport creates a new instance of AutoExportJobsServerTransport with the provided implementation.
// The returned AutoExportJobsServerTransport instance is connected to an instance of armstoragecache.AutoExportJobsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewAutoExportJobsServerTransport(srv *AutoExportJobsServer) *AutoExportJobsServerTransport {
	return &AutoExportJobsServerTransport{
		srv:                         srv,
		beginCreateOrUpdate:         newTracker[azfake.PollerResponder[armstoragecache.AutoExportJobsClientCreateOrUpdateResponse]](),
		beginDelete:                 newTracker[azfake.PollerResponder[armstoragecache.AutoExportJobsClientDeleteResponse]](),
		newListByAmlFilesystemPager: newTracker[azfake.PagerResponder[armstoragecache.AutoExportJobsClientListByAmlFilesystemResponse]](),
		beginUpdate:                 newTracker[azfake.PollerResponder[armstoragecache.AutoExportJobsClientUpdateResponse]](),
	}
}

// AutoExportJobsServerTransport connects instances of armstoragecache.AutoExportJobsClient to instances of AutoExportJobsServer.
// Don't use this type directly, use NewAutoExportJobsServerTransport instead.
type AutoExportJobsServerTransport struct {
	srv                         *AutoExportJobsServer
	beginCreateOrUpdate         *tracker[azfake.PollerResponder[armstoragecache.AutoExportJobsClientCreateOrUpdateResponse]]
	beginDelete                 *tracker[azfake.PollerResponder[armstoragecache.AutoExportJobsClientDeleteResponse]]
	newListByAmlFilesystemPager *tracker[azfake.PagerResponder[armstoragecache.AutoExportJobsClientListByAmlFilesystemResponse]]
	beginUpdate                 *tracker[azfake.PollerResponder[armstoragecache.AutoExportJobsClientUpdateResponse]]
}

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

func (a *AutoExportJobsServerTransport) 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 autoExportJobsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = autoExportJobsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "AutoExportJobsClient.BeginCreateOrUpdate":
				res.resp, res.err = a.dispatchBeginCreateOrUpdate(req)
			case "AutoExportJobsClient.BeginDelete":
				res.resp, res.err = a.dispatchBeginDelete(req)
			case "AutoExportJobsClient.Get":
				res.resp, res.err = a.dispatchGet(req)
			case "AutoExportJobsClient.NewListByAmlFilesystemPager":
				res.resp, res.err = a.dispatchNewListByAmlFilesystemPager(req)
			case "AutoExportJobsClient.BeginUpdate":
				res.resp, res.err = a.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 (a *AutoExportJobsServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if a.srv.BeginCreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")}
	}
	beginCreateOrUpdate := a.beginCreateOrUpdate.get(req)
	if beginCreateOrUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.StorageCache/amlFilesystems/(?P<amlFilesystemName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/autoExportJobs/(?P<autoExportJobName>[!#&$-;=?-\[\]_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)
		}
		body, err := server.UnmarshalRequestAsJSON[armstoragecache.AutoExportJob](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		amlFilesystemNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("amlFilesystemName")])
		if err != nil {
			return nil, err
		}
		autoExportJobNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("autoExportJobName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := a.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, amlFilesystemNameParam, autoExportJobNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOrUpdate = &respr
		a.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) {
		a.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) {
		a.beginCreateOrUpdate.remove(req)
	}

	return resp, nil
}

func (a *AutoExportJobsServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if a.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := a.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.StorageCache/amlFilesystems/(?P<amlFilesystemName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/autoExportJobs/(?P<autoExportJobName>[!#&$-;=?-\[\]_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
		}
		amlFilesystemNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("amlFilesystemName")])
		if err != nil {
			return nil, err
		}
		autoExportJobNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("autoExportJobName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := a.srv.BeginDelete(req.Context(), resourceGroupNameParam, amlFilesystemNameParam, autoExportJobNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		a.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) {
		a.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) {
		a.beginDelete.remove(req)
	}

	return resp, nil
}

func (a *AutoExportJobsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if a.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\.StorageCache/amlFilesystems/(?P<amlFilesystemName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/autoExportJobs/(?P<autoExportJobName>[!#&$-;=?-\[\]_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
	}
	amlFilesystemNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("amlFilesystemName")])
	if err != nil {
		return nil, err
	}
	autoExportJobNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("autoExportJobName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := a.srv.Get(req.Context(), resourceGroupNameParam, amlFilesystemNameParam, autoExportJobNameParam, 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).AutoExportJob, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (a *AutoExportJobsServerTransport) dispatchNewListByAmlFilesystemPager(req *http.Request) (*http.Response, error) {
	if a.srv.NewListByAmlFilesystemPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByAmlFilesystemPager not implemented")}
	}
	newListByAmlFilesystemPager := a.newListByAmlFilesystemPager.get(req)
	if newListByAmlFilesystemPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.StorageCache/amlFilesystems/(?P<amlFilesystemName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/autoExportJobs`
		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
		}
		amlFilesystemNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("amlFilesystemName")])
		if err != nil {
			return nil, err
		}
		resp := a.srv.NewListByAmlFilesystemPager(resourceGroupNameParam, amlFilesystemNameParam, nil)
		newListByAmlFilesystemPager = &resp
		a.newListByAmlFilesystemPager.add(req, newListByAmlFilesystemPager)
		server.PagerResponderInjectNextLinks(newListByAmlFilesystemPager, req, func(page *armstoragecache.AutoExportJobsClientListByAmlFilesystemResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByAmlFilesystemPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		a.newListByAmlFilesystemPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByAmlFilesystemPager) {
		a.newListByAmlFilesystemPager.remove(req)
	}
	return resp, nil
}

func (a *AutoExportJobsServerTransport) dispatchBeginUpdate(req *http.Request) (*http.Response, error) {
	if a.srv.BeginUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginUpdate not implemented")}
	}
	beginUpdate := a.beginUpdate.get(req)
	if beginUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.StorageCache/amlFilesystems/(?P<amlFilesystemName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/autoExportJobs/(?P<autoExportJobName>[!#&$-;=?-\[\]_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)
		}
		body, err := server.UnmarshalRequestAsJSON[armstoragecache.AutoExportJobUpdate](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		amlFilesystemNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("amlFilesystemName")])
		if err != nil {
			return nil, err
		}
		autoExportJobNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("autoExportJobName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := a.srv.BeginUpdate(req.Context(), resourceGroupNameParam, amlFilesystemNameParam, autoExportJobNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginUpdate = &respr
		a.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) {
		a.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) {
		a.beginUpdate.remove(req)
	}

	return resp, nil
}

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