// 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/discovery/armdiscovery"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// StorageAssetsServer is a fake server for instances of the armdiscovery.StorageAssetsClient type.
type StorageAssetsServer struct {
	// BeginCreateOrUpdate is the fake for method StorageAssetsClient.BeginCreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, storageContainerName string, storageAssetName string, resource armdiscovery.StorageAsset, options *armdiscovery.StorageAssetsClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armdiscovery.StorageAssetsClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method StorageAssetsClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, storageContainerName string, storageAssetName string, options *armdiscovery.StorageAssetsClientBeginDeleteOptions) (resp azfake.PollerResponder[armdiscovery.StorageAssetsClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method StorageAssetsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, storageContainerName string, storageAssetName string, options *armdiscovery.StorageAssetsClientGetOptions) (resp azfake.Responder[armdiscovery.StorageAssetsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByStorageContainerPager is the fake for method StorageAssetsClient.NewListByStorageContainerPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByStorageContainerPager func(resourceGroupName string, storageContainerName string, options *armdiscovery.StorageAssetsClientListByStorageContainerOptions) (resp azfake.PagerResponder[armdiscovery.StorageAssetsClientListByStorageContainerResponse])

	// BeginUpdate is the fake for method StorageAssetsClient.BeginUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginUpdate func(ctx context.Context, resourceGroupName string, storageContainerName string, storageAssetName string, properties armdiscovery.StorageAsset, options *armdiscovery.StorageAssetsClientBeginUpdateOptions) (resp azfake.PollerResponder[armdiscovery.StorageAssetsClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewStorageAssetsServerTransport creates a new instance of StorageAssetsServerTransport with the provided implementation.
// The returned StorageAssetsServerTransport instance is connected to an instance of armdiscovery.StorageAssetsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewStorageAssetsServerTransport(srv *StorageAssetsServer) *StorageAssetsServerTransport {
	return &StorageAssetsServerTransport{
		srv:                            srv,
		beginCreateOrUpdate:            newTracker[azfake.PollerResponder[armdiscovery.StorageAssetsClientCreateOrUpdateResponse]](),
		beginDelete:                    newTracker[azfake.PollerResponder[armdiscovery.StorageAssetsClientDeleteResponse]](),
		newListByStorageContainerPager: newTracker[azfake.PagerResponder[armdiscovery.StorageAssetsClientListByStorageContainerResponse]](),
		beginUpdate:                    newTracker[azfake.PollerResponder[armdiscovery.StorageAssetsClientUpdateResponse]](),
	}
}

// StorageAssetsServerTransport connects instances of armdiscovery.StorageAssetsClient to instances of StorageAssetsServer.
// Don't use this type directly, use NewStorageAssetsServerTransport instead.
type StorageAssetsServerTransport struct {
	srv                            *StorageAssetsServer
	beginCreateOrUpdate            *tracker[azfake.PollerResponder[armdiscovery.StorageAssetsClientCreateOrUpdateResponse]]
	beginDelete                    *tracker[azfake.PollerResponder[armdiscovery.StorageAssetsClientDeleteResponse]]
	newListByStorageContainerPager *tracker[azfake.PagerResponder[armdiscovery.StorageAssetsClientListByStorageContainerResponse]]
	beginUpdate                    *tracker[azfake.PollerResponder[armdiscovery.StorageAssetsClientUpdateResponse]]
}

// Do implements the policy.Transporter interface for StorageAssetsServerTransport.
func (s *StorageAssetsServerTransport) 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 *StorageAssetsServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if storageAssetsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = storageAssetsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "StorageAssetsClient.BeginCreateOrUpdate":
				res.resp, res.err = s.dispatchBeginCreateOrUpdate(req)
			case "StorageAssetsClient.BeginDelete":
				res.resp, res.err = s.dispatchBeginDelete(req)
			case "StorageAssetsClient.Get":
				res.resp, res.err = s.dispatchGet(req)
			case "StorageAssetsClient.NewListByStorageContainerPager":
				res.resp, res.err = s.dispatchNewListByStorageContainerPager(req)
			case "StorageAssetsClient.BeginUpdate":
				res.resp, res.err = s.dispatchBeginUpdate(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		resultChan <- res
	}()

	select {
	case <-req.Context().Done():
		return nil, req.Context().Err()
	case res := <-resultChan:
		return res.resp, res.err
	}
}

func (s *StorageAssetsServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if s.srv.BeginCreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")}
	}
	beginCreateOrUpdate := s.beginCreateOrUpdate.get(req)
	if beginCreateOrUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Discovery/storageContainers/(?P<storageContainerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/storageAssets/(?P<storageAssetName>[!#&$-;=?-\[\]_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[armdiscovery.StorageAsset](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		storageContainerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageContainerName")])
		if err != nil {
			return nil, err
		}
		storageAssetNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageAssetName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := s.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, storageContainerNameParam, storageAssetNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOrUpdate = &respr
		s.beginCreateOrUpdate.add(req, beginCreateOrUpdate)
	}

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

	if !slices.Contains([]int{http.StatusOK, http.StatusCreated}, resp.StatusCode) {
		s.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) {
		s.beginCreateOrUpdate.remove(req)
	}

	return resp, nil
}

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

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

	if !slices.Contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		s.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) {
		s.beginDelete.remove(req)
	}

	return resp, nil
}

func (s *StorageAssetsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if s.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\.Discovery/storageContainers/(?P<storageContainerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/storageAssets/(?P<storageAssetName>[!#&$-;=?-\[\]_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
	}
	storageContainerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageContainerName")])
	if err != nil {
		return nil, err
	}
	storageAssetNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageAssetName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := s.srv.Get(req.Context(), resourceGroupNameParam, storageContainerNameParam, storageAssetNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !slices.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).StorageAsset, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (s *StorageAssetsServerTransport) dispatchNewListByStorageContainerPager(req *http.Request) (*http.Response, error) {
	if s.srv.NewListByStorageContainerPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByStorageContainerPager not implemented")}
	}
	newListByStorageContainerPager := s.newListByStorageContainerPager.get(req)
	if newListByStorageContainerPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Discovery/storageContainers/(?P<storageContainerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/storageAssets`
		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
		}
		storageContainerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageContainerName")])
		if err != nil {
			return nil, err
		}
		resp := s.srv.NewListByStorageContainerPager(resourceGroupNameParam, storageContainerNameParam, nil)
		newListByStorageContainerPager = &resp
		s.newListByStorageContainerPager.add(req, newListByStorageContainerPager)
		server.PagerResponderInjectNextLinks(newListByStorageContainerPager, req, func(page *armdiscovery.StorageAssetsClientListByStorageContainerResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByStorageContainerPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		s.newListByStorageContainerPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByStorageContainerPager) {
		s.newListByStorageContainerPager.remove(req)
	}
	return resp, nil
}

func (s *StorageAssetsServerTransport) dispatchBeginUpdate(req *http.Request) (*http.Response, error) {
	if s.srv.BeginUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginUpdate not implemented")}
	}
	beginUpdate := s.beginUpdate.get(req)
	if beginUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Discovery/storageContainers/(?P<storageContainerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/storageAssets/(?P<storageAssetName>[!#&$-;=?-\[\]_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[armdiscovery.StorageAsset](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		storageContainerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageContainerName")])
		if err != nil {
			return nil, err
		}
		storageAssetNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageAssetName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := s.srv.BeginUpdate(req.Context(), resourceGroupNameParam, storageContainerNameParam, storageAssetNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginUpdate = &respr
		s.beginUpdate.add(req, beginUpdate)
	}

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

	if !slices.Contains([]int{http.StatusOK, http.StatusAccepted}, resp.StatusCode) {
		s.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) {
		s.beginUpdate.remove(req)
	}

	return resp, nil
}

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