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

// AppLinkMembersServer is a fake server for instances of the armappnetwork.AppLinkMembersClient type.
type AppLinkMembersServer struct {
	// BeginCreateOrUpdate is the fake for method AppLinkMembersClient.BeginCreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, appLinkName string, appLinkMemberName string, resource armappnetwork.AppLinkMember, options *armappnetwork.AppLinkMembersClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armappnetwork.AppLinkMembersClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method AppLinkMembersClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, appLinkName string, appLinkMemberName string, options *armappnetwork.AppLinkMembersClientBeginDeleteOptions) (resp azfake.PollerResponder[armappnetwork.AppLinkMembersClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method AppLinkMembersClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, appLinkName string, appLinkMemberName string, options *armappnetwork.AppLinkMembersClientGetOptions) (resp azfake.Responder[armappnetwork.AppLinkMembersClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByAppLinkPager is the fake for method AppLinkMembersClient.NewListByAppLinkPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByAppLinkPager func(resourceGroupName string, appLinkName string, options *armappnetwork.AppLinkMembersClientListByAppLinkOptions) (resp azfake.PagerResponder[armappnetwork.AppLinkMembersClientListByAppLinkResponse])

	// BeginUpdate is the fake for method AppLinkMembersClient.BeginUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginUpdate func(ctx context.Context, resourceGroupName string, appLinkName string, appLinkMemberName string, properties armappnetwork.AppLinkMemberUpdate, options *armappnetwork.AppLinkMembersClientBeginUpdateOptions) (resp azfake.PollerResponder[armappnetwork.AppLinkMembersClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewAppLinkMembersServerTransport creates a new instance of AppLinkMembersServerTransport with the provided implementation.
// The returned AppLinkMembersServerTransport instance is connected to an instance of armappnetwork.AppLinkMembersClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewAppLinkMembersServerTransport(srv *AppLinkMembersServer) *AppLinkMembersServerTransport {
	return &AppLinkMembersServerTransport{
		srv:                   srv,
		beginCreateOrUpdate:   newTracker[azfake.PollerResponder[armappnetwork.AppLinkMembersClientCreateOrUpdateResponse]](),
		beginDelete:           newTracker[azfake.PollerResponder[armappnetwork.AppLinkMembersClientDeleteResponse]](),
		newListByAppLinkPager: newTracker[azfake.PagerResponder[armappnetwork.AppLinkMembersClientListByAppLinkResponse]](),
		beginUpdate:           newTracker[azfake.PollerResponder[armappnetwork.AppLinkMembersClientUpdateResponse]](),
	}
}

// AppLinkMembersServerTransport connects instances of armappnetwork.AppLinkMembersClient to instances of AppLinkMembersServer.
// Don't use this type directly, use NewAppLinkMembersServerTransport instead.
type AppLinkMembersServerTransport struct {
	srv                   *AppLinkMembersServer
	beginCreateOrUpdate   *tracker[azfake.PollerResponder[armappnetwork.AppLinkMembersClientCreateOrUpdateResponse]]
	beginDelete           *tracker[azfake.PollerResponder[armappnetwork.AppLinkMembersClientDeleteResponse]]
	newListByAppLinkPager *tracker[azfake.PagerResponder[armappnetwork.AppLinkMembersClientListByAppLinkResponse]]
	beginUpdate           *tracker[azfake.PollerResponder[armappnetwork.AppLinkMembersClientUpdateResponse]]
}

// Do implements the policy.Transporter interface for AppLinkMembersServerTransport.
func (a *AppLinkMembersServerTransport) 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 *AppLinkMembersServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if appLinkMembersServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = appLinkMembersServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "AppLinkMembersClient.BeginCreateOrUpdate":
				res.resp, res.err = a.dispatchBeginCreateOrUpdate(req)
			case "AppLinkMembersClient.BeginDelete":
				res.resp, res.err = a.dispatchBeginDelete(req)
			case "AppLinkMembersClient.Get":
				res.resp, res.err = a.dispatchGet(req)
			case "AppLinkMembersClient.NewListByAppLinkPager":
				res.resp, res.err = a.dispatchNewListByAppLinkPager(req)
			case "AppLinkMembersClient.BeginUpdate":
				res.resp, res.err = a.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 (a *AppLinkMembersServerTransport) 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\.AppLink/appLinks/(?P<appLinkName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/appLinkMembers/(?P<appLinkMemberName>[!#&$-;=?-\[\]_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[armappnetwork.AppLinkMember](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		appLinkNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("appLinkName")])
		if err != nil {
			return nil, err
		}
		appLinkMemberNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("appLinkMemberName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := a.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, appLinkNameParam, appLinkMemberNameParam, 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 !slices.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 *AppLinkMembersServerTransport) 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\.AppLink/appLinks/(?P<appLinkName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/appLinkMembers/(?P<appLinkMemberName>[!#&$-;=?-\[\]_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
		}
		appLinkNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("appLinkName")])
		if err != nil {
			return nil, err
		}
		appLinkMemberNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("appLinkMemberName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := a.srv.BeginDelete(req.Context(), resourceGroupNameParam, appLinkNameParam, appLinkMemberNameParam, 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 !slices.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 *AppLinkMembersServerTransport) 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\.AppLink/appLinks/(?P<appLinkName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/appLinkMembers/(?P<appLinkMemberName>[!#&$-;=?-\[\]_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
	}
	appLinkNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("appLinkName")])
	if err != nil {
		return nil, err
	}
	appLinkMemberNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("appLinkMemberName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := a.srv.Get(req.Context(), resourceGroupNameParam, appLinkNameParam, appLinkMemberNameParam, 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).AppLinkMember, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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

func (a *AppLinkMembersServerTransport) 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\.AppLink/appLinks/(?P<appLinkName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/appLinkMembers/(?P<appLinkMemberName>[!#&$-;=?-\[\]_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[armappnetwork.AppLinkMemberUpdate](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		appLinkNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("appLinkName")])
		if err != nil {
			return nil, err
		}
		appLinkMemberNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("appLinkMemberName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := a.srv.BeginUpdate(req.Context(), resourceGroupNameParam, appLinkNameParam, appLinkMemberNameParam, 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 !slices.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 AppLinkMembersServerTransport
var appLinkMembersServerTransportInterceptor interface {
	// Do returns true if the server transport should use the returned response/error
	Do(*http.Request) (*http.Response, error, bool)
}
