// 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/resourcemanager/providerhub/armproviderhub/v2"
	"net/http"
	"net/url"
	"regexp"
)

// NewRegionFrontloadReleaseServer is a fake server for instances of the armproviderhub.NewRegionFrontloadReleaseClient type.
type NewRegionFrontloadReleaseServer struct {
	// CreateOrUpdate is the fake for method NewRegionFrontloadReleaseClient.CreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	CreateOrUpdate func(ctx context.Context, providerNamespace string, releaseName string, properties armproviderhub.FrontloadPayload, options *armproviderhub.NewRegionFrontloadReleaseClientCreateOrUpdateOptions) (resp azfake.Responder[armproviderhub.NewRegionFrontloadReleaseClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// GenerateManifest is the fake for method NewRegionFrontloadReleaseClient.GenerateManifest
	// HTTP status codes to indicate success: http.StatusOK
	GenerateManifest func(ctx context.Context, providerNamespace string, properties armproviderhub.FrontloadPayload, options *armproviderhub.NewRegionFrontloadReleaseClientGenerateManifestOptions) (resp azfake.Responder[armproviderhub.NewRegionFrontloadReleaseClientGenerateManifestResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method NewRegionFrontloadReleaseClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, providerNamespace string, releaseName string, options *armproviderhub.NewRegionFrontloadReleaseClientGetOptions) (resp azfake.Responder[armproviderhub.NewRegionFrontloadReleaseClientGetResponse], errResp azfake.ErrorResponder)

	// Stop is the fake for method NewRegionFrontloadReleaseClient.Stop
	// HTTP status codes to indicate success: http.StatusOK
	Stop func(ctx context.Context, providerNamespace string, releaseName string, options *armproviderhub.NewRegionFrontloadReleaseClientStopOptions) (resp azfake.Responder[armproviderhub.NewRegionFrontloadReleaseClientStopResponse], errResp azfake.ErrorResponder)
}

// NewNewRegionFrontloadReleaseServerTransport creates a new instance of NewRegionFrontloadReleaseServerTransport with the provided implementation.
// The returned NewRegionFrontloadReleaseServerTransport instance is connected to an instance of armproviderhub.NewRegionFrontloadReleaseClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewNewRegionFrontloadReleaseServerTransport(srv *NewRegionFrontloadReleaseServer) *NewRegionFrontloadReleaseServerTransport {
	return &NewRegionFrontloadReleaseServerTransport{srv: srv}
}

// NewRegionFrontloadReleaseServerTransport connects instances of armproviderhub.NewRegionFrontloadReleaseClient to instances of NewRegionFrontloadReleaseServer.
// Don't use this type directly, use NewNewRegionFrontloadReleaseServerTransport instead.
type NewRegionFrontloadReleaseServerTransport struct {
	srv *NewRegionFrontloadReleaseServer
}

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

func (n *NewRegionFrontloadReleaseServerTransport) 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 newRegionFrontloadReleaseServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = newRegionFrontloadReleaseServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "NewRegionFrontloadReleaseClient.CreateOrUpdate":
				res.resp, res.err = n.dispatchCreateOrUpdate(req)
			case "NewRegionFrontloadReleaseClient.GenerateManifest":
				res.resp, res.err = n.dispatchGenerateManifest(req)
			case "NewRegionFrontloadReleaseClient.Get":
				res.resp, res.err = n.dispatchGet(req)
			case "NewRegionFrontloadReleaseClient.Stop":
				res.resp, res.err = n.dispatchStop(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 (n *NewRegionFrontloadReleaseServerTransport) dispatchCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if n.srv.CreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method CreateOrUpdate not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ProviderHub/providerRegistrations/(?P<providerNamespace>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/newRegionFrontloadRelease/(?P<releaseName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 3 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	body, err := server.UnmarshalRequestAsJSON[armproviderhub.FrontloadPayload](req)
	if err != nil {
		return nil, err
	}
	providerNamespaceParam, err := url.PathUnescape(matches[regex.SubexpIndex("providerNamespace")])
	if err != nil {
		return nil, err
	}
	releaseNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("releaseName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := n.srv.CreateOrUpdate(req.Context(), providerNamespaceParam, releaseNameParam, body, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK, http.StatusCreated}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusCreated", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).DefaultRollout, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (n *NewRegionFrontloadReleaseServerTransport) dispatchGenerateManifest(req *http.Request) (*http.Response, error) {
	if n.srv.GenerateManifest == nil {
		return nil, &nonRetriableError{errors.New("fake for method GenerateManifest not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ProviderHub/providerRegistrations/(?P<providerNamespace>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/generateNewRegionFrontloadManifest`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 2 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	body, err := server.UnmarshalRequestAsJSON[armproviderhub.FrontloadPayload](req)
	if err != nil {
		return nil, err
	}
	providerNamespaceParam, err := url.PathUnescape(matches[regex.SubexpIndex("providerNamespace")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := n.srv.GenerateManifest(req.Context(), providerNamespaceParam, 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).ResourceProviderManifest, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (n *NewRegionFrontloadReleaseServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if n.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ProviderHub/providerRegistrations/(?P<providerNamespace>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/newRegionFrontloadRelease/(?P<releaseName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 3 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	providerNamespaceParam, err := url.PathUnescape(matches[regex.SubexpIndex("providerNamespace")])
	if err != nil {
		return nil, err
	}
	releaseNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("releaseName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := n.srv.Get(req.Context(), providerNamespaceParam, releaseNameParam, 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).DefaultRollout, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (n *NewRegionFrontloadReleaseServerTransport) dispatchStop(req *http.Request) (*http.Response, error) {
	if n.srv.Stop == nil {
		return nil, &nonRetriableError{errors.New("fake for method Stop not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ProviderHub/providerRegistrations/(?P<providerNamespace>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/newRegionFrontloadRelease/(?P<releaseName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/stop`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 3 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	providerNamespaceParam, err := url.PathUnescape(matches[regex.SubexpIndex("providerNamespace")])
	if err != nil {
		return nil, err
	}
	releaseNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("releaseName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := n.srv.Stop(req.Context(), providerNamespaceParam, releaseNameParam, 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.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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