// 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/containerservice/armcontainerservice/v8"
	"net/http"
	"net/url"
	"regexp"
)

// ManagedNamespacesServer is a fake server for instances of the armcontainerservice.ManagedNamespacesClient type.
type ManagedNamespacesServer struct {
	// BeginCreateOrUpdate is the fake for method ManagedNamespacesClient.BeginCreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, resourceName string, managedNamespaceName string, parameters armcontainerservice.ManagedNamespace, options *armcontainerservice.ManagedNamespacesClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armcontainerservice.ManagedNamespacesClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method ManagedNamespacesClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, resourceName string, managedNamespaceName string, options *armcontainerservice.ManagedNamespacesClientBeginDeleteOptions) (resp azfake.PollerResponder[armcontainerservice.ManagedNamespacesClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method ManagedNamespacesClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, resourceName string, managedNamespaceName string, options *armcontainerservice.ManagedNamespacesClientGetOptions) (resp azfake.Responder[armcontainerservice.ManagedNamespacesClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByManagedClusterPager is the fake for method ManagedNamespacesClient.NewListByManagedClusterPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByManagedClusterPager func(resourceGroupName string, resourceName string, options *armcontainerservice.ManagedNamespacesClientListByManagedClusterOptions) (resp azfake.PagerResponder[armcontainerservice.ManagedNamespacesClientListByManagedClusterResponse])

	// ListCredential is the fake for method ManagedNamespacesClient.ListCredential
	// HTTP status codes to indicate success: http.StatusOK
	ListCredential func(ctx context.Context, resourceGroupName string, resourceName string, managedNamespaceName string, options *armcontainerservice.ManagedNamespacesClientListCredentialOptions) (resp azfake.Responder[armcontainerservice.ManagedNamespacesClientListCredentialResponse], errResp azfake.ErrorResponder)

	// Update is the fake for method ManagedNamespacesClient.Update
	// HTTP status codes to indicate success: http.StatusOK
	Update func(ctx context.Context, resourceGroupName string, resourceName string, managedNamespaceName string, parameters armcontainerservice.TagsObject, options *armcontainerservice.ManagedNamespacesClientUpdateOptions) (resp azfake.Responder[armcontainerservice.ManagedNamespacesClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewManagedNamespacesServerTransport creates a new instance of ManagedNamespacesServerTransport with the provided implementation.
// The returned ManagedNamespacesServerTransport instance is connected to an instance of armcontainerservice.ManagedNamespacesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewManagedNamespacesServerTransport(srv *ManagedNamespacesServer) *ManagedNamespacesServerTransport {
	return &ManagedNamespacesServerTransport{
		srv:                          srv,
		beginCreateOrUpdate:          newTracker[azfake.PollerResponder[armcontainerservice.ManagedNamespacesClientCreateOrUpdateResponse]](),
		beginDelete:                  newTracker[azfake.PollerResponder[armcontainerservice.ManagedNamespacesClientDeleteResponse]](),
		newListByManagedClusterPager: newTracker[azfake.PagerResponder[armcontainerservice.ManagedNamespacesClientListByManagedClusterResponse]](),
	}
}

// ManagedNamespacesServerTransport connects instances of armcontainerservice.ManagedNamespacesClient to instances of ManagedNamespacesServer.
// Don't use this type directly, use NewManagedNamespacesServerTransport instead.
type ManagedNamespacesServerTransport struct {
	srv                          *ManagedNamespacesServer
	beginCreateOrUpdate          *tracker[azfake.PollerResponder[armcontainerservice.ManagedNamespacesClientCreateOrUpdateResponse]]
	beginDelete                  *tracker[azfake.PollerResponder[armcontainerservice.ManagedNamespacesClientDeleteResponse]]
	newListByManagedClusterPager *tracker[azfake.PagerResponder[armcontainerservice.ManagedNamespacesClientListByManagedClusterResponse]]
}

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

func (m *ManagedNamespacesServerTransport) 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 managedNamespacesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = managedNamespacesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "ManagedNamespacesClient.BeginCreateOrUpdate":
				res.resp, res.err = m.dispatchBeginCreateOrUpdate(req)
			case "ManagedNamespacesClient.BeginDelete":
				res.resp, res.err = m.dispatchBeginDelete(req)
			case "ManagedNamespacesClient.Get":
				res.resp, res.err = m.dispatchGet(req)
			case "ManagedNamespacesClient.NewListByManagedClusterPager":
				res.resp, res.err = m.dispatchNewListByManagedClusterPager(req)
			case "ManagedNamespacesClient.ListCredential":
				res.resp, res.err = m.dispatchListCredential(req)
			case "ManagedNamespacesClient.Update":
				res.resp, res.err = m.dispatchUpdate(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 (m *ManagedNamespacesServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if m.srv.BeginCreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")}
	}
	beginCreateOrUpdate := m.beginCreateOrUpdate.get(req)
	if beginCreateOrUpdate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ContainerService/managedClusters/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/managedNamespaces/(?P<managedNamespaceName>[!#&$-;=?-\[\]_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[armcontainerservice.ManagedNamespace](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
		if err != nil {
			return nil, err
		}
		managedNamespaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("managedNamespaceName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := m.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, resourceNameParam, managedNamespaceNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOrUpdate = &respr
		m.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) {
		m.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) {
		m.beginCreateOrUpdate.remove(req)
	}

	return resp, nil
}

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

	return resp, nil
}

func (m *ManagedNamespacesServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if m.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\.ContainerService/managedClusters/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/managedNamespaces/(?P<managedNamespaceName>[!#&$-;=?-\[\]_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
	}
	resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
	if err != nil {
		return nil, err
	}
	managedNamespaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("managedNamespaceName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := m.srv.Get(req.Context(), resourceGroupNameParam, resourceNameParam, managedNamespaceNameParam, 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).ManagedNamespace, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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

func (m *ManagedNamespacesServerTransport) dispatchListCredential(req *http.Request) (*http.Response, error) {
	if m.srv.ListCredential == nil {
		return nil, &nonRetriableError{errors.New("fake for method ListCredential not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ContainerService/managedClusters/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/managedNamespaces/(?P<managedNamespaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/listCredential`
	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
	}
	resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
	if err != nil {
		return nil, err
	}
	managedNamespaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("managedNamespaceName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := m.srv.ListCredential(req.Context(), resourceGroupNameParam, resourceNameParam, managedNamespaceNameParam, 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).CredentialResults, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (m *ManagedNamespacesServerTransport) dispatchUpdate(req *http.Request) (*http.Response, error) {
	if m.srv.Update == nil {
		return nil, &nonRetriableError{errors.New("fake for method Update not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ContainerService/managedClusters/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/managedNamespaces/(?P<managedNamespaceName>[!#&$-;=?-\[\]_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[armcontainerservice.TagsObject](req)
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
	if err != nil {
		return nil, err
	}
	managedNamespaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("managedNamespaceName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := m.srv.Update(req.Context(), resourceGroupNameParam, resourceNameParam, managedNamespaceNameParam, body, 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).ManagedNamespace, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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