// 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"
)

// ResourceActionsServer is a fake server for instances of the armproviderhub.ResourceActionsClient type.
type ResourceActionsServer struct {
	// BeginDeleteResources is the fake for method ResourceActionsClient.BeginDeleteResources
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDeleteResources func(ctx context.Context, providerNamespace string, resourceActionName string, properties armproviderhub.ResourceManagementAction, options *armproviderhub.ResourceActionsClientBeginDeleteResourcesOptions) (resp azfake.PollerResponder[armproviderhub.ResourceActionsClientDeleteResourcesResponse], errResp azfake.ErrorResponder)
}

// NewResourceActionsServerTransport creates a new instance of ResourceActionsServerTransport with the provided implementation.
// The returned ResourceActionsServerTransport instance is connected to an instance of armproviderhub.ResourceActionsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewResourceActionsServerTransport(srv *ResourceActionsServer) *ResourceActionsServerTransport {
	return &ResourceActionsServerTransport{
		srv:                  srv,
		beginDeleteResources: newTracker[azfake.PollerResponder[armproviderhub.ResourceActionsClientDeleteResourcesResponse]](),
	}
}

// ResourceActionsServerTransport connects instances of armproviderhub.ResourceActionsClient to instances of ResourceActionsServer.
// Don't use this type directly, use NewResourceActionsServerTransport instead.
type ResourceActionsServerTransport struct {
	srv                  *ResourceActionsServer
	beginDeleteResources *tracker[azfake.PollerResponder[armproviderhub.ResourceActionsClientDeleteResourcesResponse]]
}

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

func (r *ResourceActionsServerTransport) 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 resourceActionsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = resourceActionsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "ResourceActionsClient.BeginDeleteResources":
				res.resp, res.err = r.dispatchBeginDeleteResources(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 (r *ResourceActionsServerTransport) dispatchBeginDeleteResources(req *http.Request) (*http.Response, error) {
	if r.srv.BeginDeleteResources == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDeleteResources not implemented")}
	}
	beginDeleteResources := r.beginDeleteResources.get(req)
	if beginDeleteResources == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.ProviderHub/providerRegistrations/(?P<providerNamespace>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceActions/(?P<resourceActionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/deleteResources`
		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.ResourceManagementAction](req)
		if err != nil {
			return nil, err
		}
		providerNamespaceParam, err := url.PathUnescape(matches[regex.SubexpIndex("providerNamespace")])
		if err != nil {
			return nil, err
		}
		resourceActionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceActionName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := r.srv.BeginDeleteResources(req.Context(), providerNamespaceParam, resourceActionNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDeleteResources = &respr
		r.beginDeleteResources.add(req, beginDeleteResources)
	}

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

	if !contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		r.beginDeleteResources.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(beginDeleteResources) {
		r.beginDeleteResources.remove(req)
	}

	return resp, nil
}

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