// 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/trustedsigning/armtrustedsigning"
	"net/http"
	"net/url"
	"regexp"
)

// CertificateProfilesServer is a fake server for instances of the armtrustedsigning.CertificateProfilesClient type.
type CertificateProfilesServer struct {
	// BeginCreate is the fake for method CertificateProfilesClient.BeginCreate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreate func(ctx context.Context, resourceGroupName string, accountName string, profileName string, resource armtrustedsigning.CertificateProfile, options *armtrustedsigning.CertificateProfilesClientBeginCreateOptions) (resp azfake.PollerResponder[armtrustedsigning.CertificateProfilesClientCreateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method CertificateProfilesClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, accountName string, profileName string, options *armtrustedsigning.CertificateProfilesClientBeginDeleteOptions) (resp azfake.PollerResponder[armtrustedsigning.CertificateProfilesClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method CertificateProfilesClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, accountName string, profileName string, options *armtrustedsigning.CertificateProfilesClientGetOptions) (resp azfake.Responder[armtrustedsigning.CertificateProfilesClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByCodeSigningAccountPager is the fake for method CertificateProfilesClient.NewListByCodeSigningAccountPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByCodeSigningAccountPager func(resourceGroupName string, accountName string, options *armtrustedsigning.CertificateProfilesClientListByCodeSigningAccountOptions) (resp azfake.PagerResponder[armtrustedsigning.CertificateProfilesClientListByCodeSigningAccountResponse])

	// RevokeCertificate is the fake for method CertificateProfilesClient.RevokeCertificate
	// HTTP status codes to indicate success: http.StatusNoContent
	RevokeCertificate func(ctx context.Context, resourceGroupName string, accountName string, profileName string, body armtrustedsigning.RevokeCertificate, options *armtrustedsigning.CertificateProfilesClientRevokeCertificateOptions) (resp azfake.Responder[armtrustedsigning.CertificateProfilesClientRevokeCertificateResponse], errResp azfake.ErrorResponder)
}

// NewCertificateProfilesServerTransport creates a new instance of CertificateProfilesServerTransport with the provided implementation.
// The returned CertificateProfilesServerTransport instance is connected to an instance of armtrustedsigning.CertificateProfilesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewCertificateProfilesServerTransport(srv *CertificateProfilesServer) *CertificateProfilesServerTransport {
	return &CertificateProfilesServerTransport{
		srv:                              srv,
		beginCreate:                      newTracker[azfake.PollerResponder[armtrustedsigning.CertificateProfilesClientCreateResponse]](),
		beginDelete:                      newTracker[azfake.PollerResponder[armtrustedsigning.CertificateProfilesClientDeleteResponse]](),
		newListByCodeSigningAccountPager: newTracker[azfake.PagerResponder[armtrustedsigning.CertificateProfilesClientListByCodeSigningAccountResponse]](),
	}
}

// CertificateProfilesServerTransport connects instances of armtrustedsigning.CertificateProfilesClient to instances of CertificateProfilesServer.
// Don't use this type directly, use NewCertificateProfilesServerTransport instead.
type CertificateProfilesServerTransport struct {
	srv                              *CertificateProfilesServer
	beginCreate                      *tracker[azfake.PollerResponder[armtrustedsigning.CertificateProfilesClientCreateResponse]]
	beginDelete                      *tracker[azfake.PollerResponder[armtrustedsigning.CertificateProfilesClientDeleteResponse]]
	newListByCodeSigningAccountPager *tracker[azfake.PagerResponder[armtrustedsigning.CertificateProfilesClientListByCodeSigningAccountResponse]]
}

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

func (c *CertificateProfilesServerTransport) 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 certificateProfilesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = certificateProfilesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "CertificateProfilesClient.BeginCreate":
				res.resp, res.err = c.dispatchBeginCreate(req)
			case "CertificateProfilesClient.BeginDelete":
				res.resp, res.err = c.dispatchBeginDelete(req)
			case "CertificateProfilesClient.Get":
				res.resp, res.err = c.dispatchGet(req)
			case "CertificateProfilesClient.NewListByCodeSigningAccountPager":
				res.resp, res.err = c.dispatchNewListByCodeSigningAccountPager(req)
			case "CertificateProfilesClient.RevokeCertificate":
				res.resp, res.err = c.dispatchRevokeCertificate(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 (c *CertificateProfilesServerTransport) dispatchBeginCreate(req *http.Request) (*http.Response, error) {
	if c.srv.BeginCreate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreate not implemented")}
	}
	beginCreate := c.beginCreate.get(req)
	if beginCreate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.CodeSigning/codeSigningAccounts/(?P<accountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/certificateProfiles/(?P<profileName>[!#&$-;=?-\[\]_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[armtrustedsigning.CertificateProfile](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
		}
		profileNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("profileName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := c.srv.BeginCreate(req.Context(), resourceGroupNameParam, accountNameParam, profileNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreate = &respr
		c.beginCreate.add(req, beginCreate)
	}

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

	if !contains([]int{http.StatusOK, http.StatusCreated}, resp.StatusCode) {
		c.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) {
		c.beginCreate.remove(req)
	}

	return resp, nil
}

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

	return resp, nil
}

func (c *CertificateProfilesServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if c.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\.CodeSigning/codeSigningAccounts/(?P<accountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/certificateProfiles/(?P<profileName>[!#&$-;=?-\[\]_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
	}
	profileNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("profileName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := c.srv.Get(req.Context(), resourceGroupNameParam, accountNameParam, profileNameParam, 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).CertificateProfile, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (c *CertificateProfilesServerTransport) dispatchNewListByCodeSigningAccountPager(req *http.Request) (*http.Response, error) {
	if c.srv.NewListByCodeSigningAccountPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByCodeSigningAccountPager not implemented")}
	}
	newListByCodeSigningAccountPager := c.newListByCodeSigningAccountPager.get(req)
	if newListByCodeSigningAccountPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.CodeSigning/codeSigningAccounts/(?P<accountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/certificateProfiles`
		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 := c.srv.NewListByCodeSigningAccountPager(resourceGroupNameParam, accountNameParam, nil)
		newListByCodeSigningAccountPager = &resp
		c.newListByCodeSigningAccountPager.add(req, newListByCodeSigningAccountPager)
		server.PagerResponderInjectNextLinks(newListByCodeSigningAccountPager, req, func(page *armtrustedsigning.CertificateProfilesClientListByCodeSigningAccountResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByCodeSigningAccountPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		c.newListByCodeSigningAccountPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByCodeSigningAccountPager) {
		c.newListByCodeSigningAccountPager.remove(req)
	}
	return resp, nil
}

func (c *CertificateProfilesServerTransport) dispatchRevokeCertificate(req *http.Request) (*http.Response, error) {
	if c.srv.RevokeCertificate == nil {
		return nil, &nonRetriableError{errors.New("fake for method RevokeCertificate not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.CodeSigning/codeSigningAccounts/(?P<accountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/certificateProfiles/(?P<profileName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/revokeCertificate`
	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[armtrustedsigning.RevokeCertificate](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
	}
	profileNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("profileName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := c.srv.RevokeCertificate(req.Context(), resourceGroupNameParam, accountNameParam, profileNameParam, body, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusNoContent}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusNoContent", respContent.HTTPStatus)}
	}
	resp, err := server.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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