// 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/databasewatcher/armdatabasewatcher"
	"net/http"
	"net/url"
	"regexp"
)

// HealthValidationsServer is a fake server for instances of the armdatabasewatcher.HealthValidationsClient type.
type HealthValidationsServer struct {
	// Get is the fake for method HealthValidationsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, watcherName string, healthValidationName string, options *armdatabasewatcher.HealthValidationsClientGetOptions) (resp azfake.Responder[armdatabasewatcher.HealthValidationsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListByParentPager is the fake for method HealthValidationsClient.NewListByParentPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByParentPager func(resourceGroupName string, watcherName string, options *armdatabasewatcher.HealthValidationsClientListByParentOptions) (resp azfake.PagerResponder[armdatabasewatcher.HealthValidationsClientListByParentResponse])

	// BeginStartValidation is the fake for method HealthValidationsClient.BeginStartValidation
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginStartValidation func(ctx context.Context, resourceGroupName string, watcherName string, healthValidationName string, options *armdatabasewatcher.HealthValidationsClientBeginStartValidationOptions) (resp azfake.PollerResponder[armdatabasewatcher.HealthValidationsClientStartValidationResponse], errResp azfake.ErrorResponder)
}

// NewHealthValidationsServerTransport creates a new instance of HealthValidationsServerTransport with the provided implementation.
// The returned HealthValidationsServerTransport instance is connected to an instance of armdatabasewatcher.HealthValidationsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewHealthValidationsServerTransport(srv *HealthValidationsServer) *HealthValidationsServerTransport {
	return &HealthValidationsServerTransport{
		srv:                  srv,
		newListByParentPager: newTracker[azfake.PagerResponder[armdatabasewatcher.HealthValidationsClientListByParentResponse]](),
		beginStartValidation: newTracker[azfake.PollerResponder[armdatabasewatcher.HealthValidationsClientStartValidationResponse]](),
	}
}

// HealthValidationsServerTransport connects instances of armdatabasewatcher.HealthValidationsClient to instances of HealthValidationsServer.
// Don't use this type directly, use NewHealthValidationsServerTransport instead.
type HealthValidationsServerTransport struct {
	srv                  *HealthValidationsServer
	newListByParentPager *tracker[azfake.PagerResponder[armdatabasewatcher.HealthValidationsClientListByParentResponse]]
	beginStartValidation *tracker[azfake.PollerResponder[armdatabasewatcher.HealthValidationsClientStartValidationResponse]]
}

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

func (h *HealthValidationsServerTransport) 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 healthValidationsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = healthValidationsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "HealthValidationsClient.Get":
				res.resp, res.err = h.dispatchGet(req)
			case "HealthValidationsClient.NewListByParentPager":
				res.resp, res.err = h.dispatchNewListByParentPager(req)
			case "HealthValidationsClient.BeginStartValidation":
				res.resp, res.err = h.dispatchBeginStartValidation(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 (h *HealthValidationsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if h.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\.DatabaseWatcher/watchers/(?P<watcherName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/healthValidations/(?P<healthValidationName>[!#&$-;=?-\[\]_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
	}
	watcherNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("watcherName")])
	if err != nil {
		return nil, err
	}
	healthValidationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("healthValidationName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := h.srv.Get(req.Context(), resourceGroupNameParam, watcherNameParam, healthValidationNameParam, 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).HealthValidation, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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

func (h *HealthValidationsServerTransport) dispatchBeginStartValidation(req *http.Request) (*http.Response, error) {
	if h.srv.BeginStartValidation == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginStartValidation not implemented")}
	}
	beginStartValidation := h.beginStartValidation.get(req)
	if beginStartValidation == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.DatabaseWatcher/watchers/(?P<watcherName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/healthValidations/(?P<healthValidationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/startValidation`
		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
		}
		watcherNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("watcherName")])
		if err != nil {
			return nil, err
		}
		healthValidationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("healthValidationName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := h.srv.BeginStartValidation(req.Context(), resourceGroupNameParam, watcherNameParam, healthValidationNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginStartValidation = &respr
		h.beginStartValidation.add(req, beginStartValidation)
	}

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

	if !contains([]int{http.StatusOK, http.StatusAccepted}, resp.StatusCode) {
		h.beginStartValidation.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginStartValidation) {
		h.beginStartValidation.remove(req)
	}

	return resp, nil
}

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