// 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/storage/armstorage/v4"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// DataSharesServer is a fake server for instances of the armstorage.DataSharesClient type.
type DataSharesServer struct {
	// BeginCreate is the fake for method DataSharesClient.BeginCreate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreate func(ctx context.Context, resourceGroupName string, accountName string, dataShareName string, resource armstorage.DataShare, options *armstorage.DataSharesClientBeginCreateOptions) (resp azfake.PollerResponder[armstorage.DataSharesClientCreateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method DataSharesClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, accountName string, dataShareName string, options *armstorage.DataSharesClientBeginDeleteOptions) (resp azfake.PollerResponder[armstorage.DataSharesClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method DataSharesClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, accountName string, dataShareName string, options *armstorage.DataSharesClientGetOptions) (resp azfake.Responder[armstorage.DataSharesClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByStorageAccountPager is the fake for method DataSharesClient.NewListByStorageAccountPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByStorageAccountPager func(resourceGroupName string, accountName string, options *armstorage.DataSharesClientListByStorageAccountOptions) (resp azfake.PagerResponder[armstorage.DataSharesClientListByStorageAccountResponse])

	// BeginUpdate is the fake for method DataSharesClient.BeginUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginUpdate func(ctx context.Context, resourceGroupName string, accountName string, dataShareName string, properties armstorage.DataShareUpdate, options *armstorage.DataSharesClientBeginUpdateOptions) (resp azfake.PollerResponder[armstorage.DataSharesClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewDataSharesServerTransport creates a new instance of DataSharesServerTransport with the provided implementation.
// The returned DataSharesServerTransport instance is connected to an instance of armstorage.DataSharesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewDataSharesServerTransport(srv *DataSharesServer) *DataSharesServerTransport {
	return &DataSharesServerTransport{
		srv:                          srv,
		beginCreate:                  newTracker[azfake.PollerResponder[armstorage.DataSharesClientCreateResponse]](),
		beginDelete:                  newTracker[azfake.PollerResponder[armstorage.DataSharesClientDeleteResponse]](),
		newListByStorageAccountPager: newTracker[azfake.PagerResponder[armstorage.DataSharesClientListByStorageAccountResponse]](),
		beginUpdate:                  newTracker[azfake.PollerResponder[armstorage.DataSharesClientUpdateResponse]](),
	}
}

// DataSharesServerTransport connects instances of armstorage.DataSharesClient to instances of DataSharesServer.
// Don't use this type directly, use NewDataSharesServerTransport instead.
type DataSharesServerTransport struct {
	srv                          *DataSharesServer
	beginCreate                  *tracker[azfake.PollerResponder[armstorage.DataSharesClientCreateResponse]]
	beginDelete                  *tracker[azfake.PollerResponder[armstorage.DataSharesClientDeleteResponse]]
	newListByStorageAccountPager *tracker[azfake.PagerResponder[armstorage.DataSharesClientListByStorageAccountResponse]]
	beginUpdate                  *tracker[azfake.PollerResponder[armstorage.DataSharesClientUpdateResponse]]
}

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

func (d *DataSharesServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if dataSharesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = dataSharesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "DataSharesClient.BeginCreate":
				res.resp, res.err = d.dispatchBeginCreate(req)
			case "DataSharesClient.BeginDelete":
				res.resp, res.err = d.dispatchBeginDelete(req)
			case "DataSharesClient.Get":
				res.resp, res.err = d.dispatchGet(req)
			case "DataSharesClient.NewListByStorageAccountPager":
				res.resp, res.err = d.dispatchNewListByStorageAccountPager(req)
			case "DataSharesClient.BeginUpdate":
				res.resp, res.err = d.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 (d *DataSharesServerTransport) dispatchBeginCreate(req *http.Request) (*http.Response, error) {
	if d.srv.BeginCreate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreate not implemented")}
	}
	beginCreate := d.beginCreate.get(req)
	if beginCreate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Storage/storageAccounts/(?P<accountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/dataShares/(?P<dataShareName>[!#&$-;=?-\[\]_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[armstorage.DataShare](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		accountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("accountName")])
		if err != nil {
			return nil, err
		}
		dataShareNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dataShareName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := d.srv.BeginCreate(req.Context(), resourceGroupNameParam, accountNameParam, dataShareNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreate = &respr
		d.beginCreate.add(req, beginCreate)
	}

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

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

	return resp, nil
}

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

	return resp, nil
}

func (d *DataSharesServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if d.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\.Storage/storageAccounts/(?P<accountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/dataShares/(?P<dataShareName>[!#&$-;=?-\[\]_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
	}
	accountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("accountName")])
	if err != nil {
		return nil, err
	}
	dataShareNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dataShareName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := d.srv.Get(req.Context(), resourceGroupNameParam, accountNameParam, dataShareNameParam, 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).DataShare, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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

func (d *DataSharesServerTransport) dispatchBeginUpdate(req *http.Request) (*http.Response, error) {
	if d.srv.BeginUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginUpdate not implemented")}
	}
	beginUpdate := d.beginUpdate.get(req)
	if beginUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Storage/storageAccounts/(?P<accountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/dataShares/(?P<dataShareName>[!#&$-;=?-\[\]_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[armstorage.DataShareUpdate](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		accountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("accountName")])
		if err != nil {
			return nil, err
		}
		dataShareNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("dataShareName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := d.srv.BeginUpdate(req.Context(), resourceGroupNameParam, accountNameParam, dataShareNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginUpdate = &respr
		d.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) {
		d.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) {
		d.beginUpdate.remove(req)
	}

	return resp, nil
}

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